Commit b3ab9c23 by Calen Pennington

Allow importing of courses that don't have the expected path/filename structure…

Allow importing of courses that don't have the expected path/filename structure for problems and html
parent 926fdd01
...@@ -503,3 +503,10 @@ class CapaDescriptor(RawDescriptor): ...@@ -503,3 +503,10 @@ class CapaDescriptor(RawDescriptor):
""" """
module_class = CapaModule module_class = CapaModule
# TODO (cpennington): Delete this method once all fall 2012 course are being
# edited in the cms
@classmethod
def backcompat_path(cls, path):
if path.startswith('problem'):
return 'problems/' + path[7:]
...@@ -28,6 +28,16 @@ class HtmlDescriptor(RawDescriptor): ...@@ -28,6 +28,16 @@ class HtmlDescriptor(RawDescriptor):
js = {'coffee': [resource_string(__name__, 'js/module/html.coffee')]} js = {'coffee': [resource_string(__name__, 'js/module/html.coffee')]}
js_module = 'HTML' js_module = 'HTML'
# TODO (cpennington): Delete this method once all fall 2012 course are being
# edited in the cms
@classmethod
def backcompat_path(cls, path):
if path.startswith('html/html'):
path = path[5:]
if path.endswith('.html.html'):
path = path[:-5]
return path
@classmethod @classmethod
def file_to_xml(cls, file_object): def file_to_xml(cls, file_object):
parser = etree.HTMLParser() parser = etree.HTMLParser()
......
...@@ -4,6 +4,8 @@ from lxml import etree ...@@ -4,6 +4,8 @@ from lxml import etree
import copy import copy
import logging import logging
from collections import namedtuple from collections import namedtuple
from fs.errors import ResourceNotFoundError
import os
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -154,13 +156,28 @@ class XmlDescriptor(XModuleDescriptor): ...@@ -154,13 +156,28 @@ class XmlDescriptor(XModuleDescriptor):
definition_xml = copy.deepcopy(xml_object) definition_xml = copy.deepcopy(xml_object)
else: else:
filepath = cls._format_filepath(xml_object.tag, filename) filepath = cls._format_filepath(xml_object.tag, filename)
log.debug('filepath=%s, resources_fs=%s' % (filepath,system.resources_fs))
with system.resources_fs.open(filepath) as file: # TODO (cpennington): If the file doesn't exist at the right path,
try: # give the class a chance to fix it up. The file will be written out again
definition_xml = cls.file_to_xml(file) # in the correct format.
except: # This should go away once the CMS is online and has imported all current (fall 2012)
log.exception("Failed to parse xml in file %s" % filepath) # courses from xml
raise if not system.resources_fs.exists(filepath) and hasattr(cls, 'backcompat_path'):
new_filepath = cls.backcompat_path(filepath)
if new_filepath is not None and system.resources_fs.exists(new_filepath):
filepath = new_filepath
log.debug('filepath=%s, resources_fs=%s' % (filepath, system.resources_fs))
try:
with system.resources_fs.open(filepath) as file:
try:
definition_xml = cls.file_to_xml(file)
except:
log.exception("Failed to parse xml in file %s" % filepath)
raise
except ResourceNotFoundError:
log.exception('Unable to load file contents at path %s' % filepath)
return {'data': 'Error loading file contents at path %s' % filepath}
cls.clean_metadata_from_xml(definition_xml) cls.clean_metadata_from_xml(definition_xml)
return cls.definition_from_xml(definition_xml, system) return cls.definition_from_xml(definition_xml, system)
...@@ -200,7 +217,7 @@ class XmlDescriptor(XModuleDescriptor): ...@@ -200,7 +217,7 @@ class XmlDescriptor(XModuleDescriptor):
if len(list(xml_object.iter())) > 5: if len(list(xml_object.iter())) > 5:
filepath = self.__class__._format_filepath(self.category, self.name) filepath = self.__class__._format_filepath(self.category, self.name)
resource_fs.makedir(self.category, allow_recreate=True) resource_fs.makedir(os.path.dirname(filepath), allow_recreate=True)
with resource_fs.open(filepath, 'w') as file: with resource_fs.open(filepath, 'w') as file:
file.write(etree.tostring(xml_object, pretty_print=True)) file.write(etree.tostring(xml_object, pretty_print=True))
......
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