Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
ce74f977
Commit
ce74f977
authored
12 years ago
by
ichuang
Browse files
Options
Downloads
Patches
Plain Diff
modify capa_problem to add <include file="foo"> functionality
parent
788321be
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/lib/capa/capa_problem.py
+31
-0
31 additions, 0 deletions
common/lib/capa/capa_problem.py
with
31 additions
and
0 deletions
common/lib/capa/capa_problem.py
+
31
−
0
View file @
ce74f977
...
...
@@ -111,6 +111,7 @@ class LoncapaProblem(object):
file_text
=
re
.
sub
(
"
endouttext\s*/
"
,
"
/text
"
,
file_text
)
self
.
tree
=
etree
.
XML
(
file_text
)
# parse problem XML file into an element tree
self
.
_process_includes
()
# handle any <include file="foo"> tags
# construct script processor context (eg for customresponse problems)
self
.
context
=
self
.
_extract_context
(
self
.
tree
,
seed
=
self
.
seed
)
...
...
@@ -242,6 +243,36 @@ class LoncapaProblem(object):
# ======= Private Methods Below ========
def
_process_includes
(
self
):
'''
Handle any <include file=
"
foo
"
> tags by reading in the specified file and inserting it
into our XML tree. Fail gracefully if debugging.
'''
includes
=
self
.
tree
.
findall
(
'
.//include
'
)
for
inc
in
includes
:
file
=
inc
.
get
(
'
file
'
)
if
file
is
not
None
:
try
:
ifp
=
self
.
system
.
filestore
.
open
(
file
)
# open using I4xSystem OSFS filestore
except
Exception
,
err
:
log
.
error
(
'
Error %s in problem xml include: %s
'
%
(
err
,
etree
.
tostring
(
inc
,
pretty_print
=
True
)))
log
.
error
(
'
Cannot find file %s in %s
'
%
(
file
,
self
.
system
.
filestore
))
if
not
self
.
system
.
get
(
'
DEBUG
'
):
# if debugging, don't fail - just log error
raise
else
:
continue
try
:
incxml
=
etree
.
XML
(
ifp
.
read
())
# read in and convert to XML
except
Exception
,
err
:
log
.
error
(
'
Error %s in problem xml include: %s
'
%
(
err
,
etree
.
tostring
(
inc
,
pretty_print
=
True
)))
log
.
error
(
'
Cannot parse XML in %s
'
%
(
file
))
if
not
self
.
system
.
get
(
'
DEBUG
'
):
# if debugging, don't fail - just log error
raise
else
:
continue
parent
=
inc
.
getparent
()
# insert new XML into tree in place of inlcude
parent
.
insert
(
parent
.
index
(
inc
),
incxml
)
parent
.
remove
(
inc
)
log
.
debug
(
'
Included %s into %s
'
%
(
file
,
self
.
fileobject
))
def
_extract_context
(
self
,
tree
,
seed
=
struct
.
unpack
(
'
i
'
,
os
.
urandom
(
4
))[
0
]):
# private
'''
Extract content of <script>...</script> from the problem.xml file, and exec it in the
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment