From 8cf848b191545f4c509a76333b0c43bb103d57cc Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Sat, 30 Jun 2012 09:11:40 -0400 Subject: [PATCH] Handle the filename for capa_module in the xml_module code, rather than specially in capa_module --- common/lib/capa/capa_problem.py | 22 +++++++++------------- common/lib/xmodule/capa_module.py | 17 +++-------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/common/lib/capa/capa_problem.py b/common/lib/capa/capa_problem.py index a06ac1d7b6..2e3620021b 100644 --- a/common/lib/capa/capa_problem.py +++ b/common/lib/capa/capa_problem.py @@ -68,14 +68,13 @@ class LoncapaProblem(object): 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 - pointed to by fileobject. + Initializes capa Problem. 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) - state : student state (represented as a dict) - seed : random number generator seed (int) @@ -103,14 +102,11 @@ class LoncapaProblem(object): if not self.seed: self.seed = struct.unpack('i', os.urandom(4))[0] - self.fileobject = fileobject # save problem file object, so we can use for debugging information later - if getattr(system, 'DEBUG', False): # get the problem XML string from the problem file - log.info("[courseware.capa.capa_problem.lcp.init] fileobject = %s" % fileobject) - file_text = fileobject.read() - file_text = re.sub("startouttext\s*/", "text", file_text) # Convert startouttext and endouttext to proper - file_text = re.sub("endouttext\s*/", "/text", file_text) + problem_text = re.sub("startouttext\s*/", "text", problem_text) # Convert startouttext and endouttext to proper + problem_text = re.sub("endouttext\s*/", "/text", problem_text) + self.problem_text = problem_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 tags # construct script processor context (eg for customresponse problems) @@ -130,7 +126,7 @@ class LoncapaProblem(object): self.done = False def __unicode__(self): - return u"LoncapaProblem ({0})".format(self.fileobject) + return u"LoncapaProblem ({0})".format(self.problem_text) def get_state(self): ''' Stored per-user session data neeeded to: @@ -272,7 +268,7 @@ class LoncapaProblem(object): 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)) + log.debug('Included %s into %s' % (file, self.id)) def _extract_context(self, tree, seed=struct.unpack('i', os.urandom(4))[0]): # private ''' diff --git a/common/lib/xmodule/capa_module.py b/common/lib/xmodule/capa_module.py index 6a95789417..57c5fa88ce 100644 --- a/common/lib/xmodule/capa_module.py +++ b/common/lib/xmodule/capa_module.py @@ -117,8 +117,6 @@ class CapaModule(XModule): if instance_state != None and 'attempts' in instance_state: 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')) weight_string = only_one(dom2.xpath('/problem/@weight')) @@ -133,20 +131,11 @@ class CapaModule(XModule): seed = system.id else: seed = None + 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 file %s is missing' % 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: - msg = 'cannot create LoncapaProblem %s' % self.filename + msg = 'cannot create LoncapaProblem %s' % self.url log.exception(msg) if self.system.DEBUG: msg = '

%s

' % msg.replace('<', '<')