Add xml export infrastructure for all existing modules

This commit is contained in:
Calen Pennington
2012-06-29 16:08:15 -04:00
parent a94e4d2f1b
commit f375258b38
4 changed files with 60 additions and 0 deletions

View File

@@ -103,3 +103,21 @@ class ABTestDescriptor(RawDescriptor, XmlDescriptor):
definition['data']['group_portions'][DEFAULT] = default_portion
return definition
def definition_to_xml(self, resource_fs):
xml_object = etree.Element('abtest')
xml_object.set('experiment', self.definition['data']['experiment'])
for name, group in self.definition['data']['group_content'].items():
if name == DEFAULT:
group_elem = etree.SubElement(xml_object, 'default')
else:
group_elem = etree.SubElement(xml_object, 'group', attrib={
'portion': str(self.definition['data']['group_portions'][name]),
'name': name,
})
for child_loc in group:
child = self.system.load_item(child_loc)
group_elem.append(etree.fromstring(child.export_to_xml(resource_fs)))
return xml_object

View File

@@ -21,3 +21,6 @@ class RawDescriptor(MakoModuleDescriptor, XmlDescriptor):
@classmethod
def definition_from_xml(cls, xml_object, system):
return {'data': etree.tostring(xml_object)}
def definition_to_xml(self, resource_fs):
return etree.fromstring(self.definition['data'])

View File

@@ -108,3 +108,9 @@ class SequenceDescriptor(MakoModuleDescriptor, XmlDescriptor):
system.process_xml(etree.tostring(child_module)).location.url()
for child_module in xml_object
]}
def definition_to_xml(self, resource_fs):
xml_object = etree.Element('sequential')
for child in self.get_children():
xml_object.append(etree.fromstring(child.export_to_xml(resource_fs)))
return xml_object

View File

@@ -51,3 +51,36 @@ class XmlDescriptor(XModuleDescriptor):
xml_object.get('slug')],
metadata=metadata,
)
def export_to_xml(self, resource_fs):
"""
Returns an xml string representing this module, and all modules underneath it.
May also write required resources out to resource_fs
Assumes that modules have single parantage (that no module appears twice in the same course),
and that it is thus safe to nest modules as xml children as appropriate.
The returned XML should be able to be parsed back into an identical XModuleDescriptor
using the from_xml method with the same system, org, and course
"""
xml_object = self.definition_to_xml(resource_fs)
xml_object.set('slug', self.name)
xml_object.tag = self.type
for attr in ('format', 'graceperiod', 'showanswer', 'rerandomize', 'due'):
if attr in self.metadata:
xml_object.set(attr, self.metadata[attr])
if 'graded' in self.metadata:
xml_object.set('graded', str(self.metadata['graded']).lower())
if 'display_name' in self.metadata:
xml_object.set('name', self.metadata['display_name'])
return etree.tostring(xml_object, pretty_print=True)
def definition_to_xml(self, resource_fs):
"""
Return a new etree Element object created from this modules definition.
"""
raise NotImplementedError("%s does not implement definition_to_xml" % self.__class__.__name__)