Commit 0a2fd0a1 by Calen Pennington

Move the resources_fs abstraction into the primary DescriptorSystem, as it is…

Move the resources_fs abstraction into the primary DescriptorSystem, as it is needed for more than just XMLParsing
parent 40337068
...@@ -31,26 +31,25 @@ class Command(BaseCommand): ...@@ -31,26 +31,25 @@ class Command(BaseCommand):
class ImportSystem(XMLParsingSystem): class ImportSystem(XMLParsingSystem):
def __init__(self): def __init__(self):
self.load_item = keystore().get_item def process_xml(xml):
self.fs = OSFS(data_dir) try:
xml_data = etree.fromstring(xml)
def process_xml(self, xml): except:
try: raise CommandError("Unable to parse xml: " + xml)
xml_data = etree.fromstring(xml)
except: if not xml_data.get('name'):
raise CommandError("Unable to parse xml: " + xml) global unnamed_modules
unnamed_modules += 1
if not xml_data.get('name'): xml_data.set('name', '{tag}_{count}'.format(tag=xml_data.tag, count=unnamed_modules))
global unnamed_modules
unnamed_modules += 1 module = XModuleDescriptor.load_from_xml(etree.tostring(xml_data), self, org, course, RawDescriptor)
xml_data.set('name', '{tag}_{count}'.format(tag=xml_data.tag, count=unnamed_modules)) keystore().create_item(module.url)
if 'data' in module.definition:
module = XModuleDescriptor.load_from_xml(etree.tostring(xml_data), self, org, course, RawDescriptor) keystore().update_item(module.url, module.definition['data'])
keystore().create_item(module.url) if 'children' in module.definition:
if 'data' in module.definition: keystore().update_children(module.url, module.definition['children'])
keystore().update_item(module.url, module.definition['data']) return module
if 'children' in module.definition:
keystore().update_children(module.url, module.definition['children']) XMLParsingSystem.__init__(self, keystore().get_item, OSFS(data_dir), process_xml)
return module
ImportSystem().process_xml(course_file.read()) ImportSystem().process_xml(course_file.read())
...@@ -51,8 +51,9 @@ class MongoModuleStore(ModuleStore): ...@@ -51,8 +51,9 @@ class MongoModuleStore(ModuleStore):
if item is None: if item is None:
raise ItemNotFoundError(location) raise ItemNotFoundError(location)
# TODO (cpennington): Pass a proper resources_fs to the system
return XModuleDescriptor.load_from_json( return XModuleDescriptor.load_from_json(
item, DescriptorSystem(self.get_item), self.default_class) item, DescriptorSystem(self.get_item, None), self.default_class)
def create_item(self, location): def create_item(self, location):
""" """
......
...@@ -23,7 +23,7 @@ def process_includes(fn): ...@@ -23,7 +23,7 @@ def process_includes(fn):
file = next_include.get('file') file = next_include.get('file')
if file is not None: if file is not None:
try: try:
ifp = system.fs.open(file) ifp = system.resources_fs.open(file)
except Exception: except Exception:
log.exception('Error in problem xml include: %s' % (etree.tostring(next_include, pretty_print=True))) log.exception('Error in problem xml include: %s' % (etree.tostring(next_include, pretty_print=True)))
log.exception('Cannot find file %s in %s' % (file, dir)) log.exception('Cannot find file %s in %s' % (file, dir))
......
...@@ -331,20 +331,21 @@ class XModuleDescriptor(Plugin): ...@@ -331,20 +331,21 @@ class XModuleDescriptor(Plugin):
class DescriptorSystem(object): class DescriptorSystem(object):
def __init__(self, load_item): def __init__(self, load_item, resources_fs):
""" """
load_item: Takes a Location and returns an XModuleDescriptor load_item: Takes a Location and returns an XModuleDescriptor
resources_fs: A Filesystem object that contains all of the
resources needed for the course
""" """
self.load_item = load_item self.load_item = load_item
self.resources_fs = resources_fs
class XMLParsingSystem(DescriptorSystem): class XMLParsingSystem(DescriptorSystem):
def __init__(self, load_item, process_xml, fs): def __init__(self, load_item, resources_fs, process_xml):
""" """
process_xml: Takes an xml string, and returns the the XModuleDescriptor created from that xml process_xml: Takes an xml string, and returns the the XModuleDescriptor created from that xml
fs: A Filesystem object that contains all of the xml resources needed to parse
the course
""" """
DescriptorSystem.__init__(self, load_item, resources_fs)
self.process_xml = process_xml self.process_xml = process_xml
self.fs = fs
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