Commit 8cf848b1 by Calen Pennington

Handle the filename for capa_module in the xml_module code, rather than specially in capa_module

parent 552c1997
...@@ -68,14 +68,13 @@ class LoncapaProblem(object): ...@@ -68,14 +68,13 @@ class LoncapaProblem(object):
Main class for capa Problems. Main class for capa Problems.
''' '''
def __init__(self, fileobject, id, state=None, seed=None, system=None): def __init__(self, problem_text, id, state=None, seed=None, system=None):
''' '''
Initializes capa Problem. The problem itself is defined by the XML file Initializes capa Problem.
pointed to by fileobject.
Arguments: Arguments:
- filesobject : an OSFS instance: see fs.osfs - problem_text : xml defining the problem
- id : string used as the identifier for this problem; often a filename (no spaces) - id : string used as the identifier for this problem; often a filename (no spaces)
- state : student state (represented as a dict) - state : student state (represented as a dict)
- seed : random number generator seed (int) - seed : random number generator seed (int)
...@@ -103,14 +102,11 @@ class LoncapaProblem(object): ...@@ -103,14 +102,11 @@ class LoncapaProblem(object):
if not self.seed: if not self.seed:
self.seed = struct.unpack('i', os.urandom(4))[0] self.seed = struct.unpack('i', os.urandom(4))[0]
self.fileobject = fileobject # save problem file object, so we can use for debugging information later problem_text = re.sub("startouttext\s*/", "text", problem_text) # Convert startouttext and endouttext to proper <text></text>
if getattr(system, 'DEBUG', False): # get the problem XML string from the problem file problem_text = re.sub("endouttext\s*/", "/text", problem_text)
log.info("[courseware.capa.capa_problem.lcp.init] fileobject = %s" % fileobject) self.problem_text = problem_text
file_text = fileobject.read()
file_text = re.sub("startouttext\s*/", "text", file_text) # Convert startouttext and endouttext to proper <text></text>
file_text = re.sub("endouttext\s*/", "/text", file_text)
self.tree = etree.XML(file_text) # parse problem XML file into an element tree self.tree = etree.XML(problem_text) # parse problem XML file into an element tree
self._process_includes() # handle any <include file="foo"> tags self._process_includes() # handle any <include file="foo"> tags
# construct script processor context (eg for customresponse problems) # construct script processor context (eg for customresponse problems)
...@@ -130,7 +126,7 @@ class LoncapaProblem(object): ...@@ -130,7 +126,7 @@ class LoncapaProblem(object):
self.done = False self.done = False
def __unicode__(self): def __unicode__(self):
return u"LoncapaProblem ({0})".format(self.fileobject) return u"LoncapaProblem ({0})".format(self.problem_text)
def get_state(self): def get_state(self):
''' Stored per-user session data neeeded to: ''' Stored per-user session data neeeded to:
...@@ -272,7 +268,7 @@ class LoncapaProblem(object): ...@@ -272,7 +268,7 @@ class LoncapaProblem(object):
parent = inc.getparent() # insert new XML into tree in place of inlcude parent = inc.getparent() # insert new XML into tree in place of inlcude
parent.insert(parent.index(inc),incxml) parent.insert(parent.index(inc),incxml)
parent.remove(inc) parent.remove(inc)
log.debug('Included %s into %s' % (file,self.fileobject)) log.debug('Included %s into %s' % (file, self.id))
def _extract_context(self, tree, seed=struct.unpack('i', os.urandom(4))[0]): # private def _extract_context(self, tree, seed=struct.unpack('i', os.urandom(4))[0]): # private
''' '''
......
...@@ -117,8 +117,6 @@ class CapaModule(XModule): ...@@ -117,8 +117,6 @@ class CapaModule(XModule):
if instance_state != None and 'attempts' in instance_state: if instance_state != None and 'attempts' in instance_state:
self.attempts = instance_state['attempts'] self.attempts = instance_state['attempts']
# TODO: Should be: self.filename=only_one(dom2.xpath('/problem/@filename'))
self.filename = "problems/" + only_one(dom2.xpath('/problem/@filename')) + ".xml"
self.name = only_one(dom2.xpath('/problem/@name')) self.name = only_one(dom2.xpath('/problem/@name'))
weight_string = only_one(dom2.xpath('/problem/@weight')) weight_string = only_one(dom2.xpath('/problem/@weight'))
...@@ -133,20 +131,11 @@ class CapaModule(XModule): ...@@ -133,20 +131,11 @@ class CapaModule(XModule):
seed = system.id seed = system.id
else: else:
seed = None seed = None
try: try:
fp = self.system.filestore.open(self.filename) self.lcp = LoncapaProblem(self.definition['data'], self.location.html_id(), instance_state, seed=seed, system=self.system)
except Exception:
log.exception('cannot open file %s' % self.filename)
if self.system.DEBUG:
# create a dummy problem instead of failing
fp = StringIO.StringIO('<problem><text><font color="red" size="+2">Problem file %s is missing</font></text></problem>' % self.filename)
fp.name = "StringIO"
else:
raise
try:
self.lcp = LoncapaProblem(fp, self.location.html_id(), instance_state, seed=seed, system=self.system)
except Exception: except Exception:
msg = 'cannot create LoncapaProblem %s' % self.filename msg = 'cannot create LoncapaProblem %s' % self.url
log.exception(msg) log.exception(msg)
if self.system.DEBUG: if self.system.DEBUG:
msg = '<p>%s</p>' % msg.replace('<', '&lt;') msg = '<p>%s</p>' % msg.replace('<', '&lt;')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment