Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
Missed modifications from two commits back...
Browse files Browse the repository at this point in the history
  • Loading branch information
Esmail committed Oct 8, 2014
1 parent 0263c32 commit 69cf43c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
4 changes: 2 additions & 2 deletions pyxform/tests/test_import_xform.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def test_import_from_file_obj(self):
survey_from_path= survey_from.xform(path=xform_file_path)

with open(xform_file_path) as f:
survey_from_file= survey_from.xform(filelike_obj=f)
survey_from_file_obj= survey_from.xform(filelike_obj=f)

self.assertEqual(survey_from_file, survey_from_path)
self.assertEqual(survey_from_file_obj, survey_from_path)


if __name__ == "__main__":
Expand Down
49 changes: 23 additions & 26 deletions pyxform/xform2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pyxform import constants
from pyxform import builder


## {{{ http://code.activestate.com/recipes/573463/ (r7)
class XmlDictObject(dict):
"""
Expand Down Expand Up @@ -161,25 +160,6 @@ def ConvertXmlToDict(root, dictclass=XmlDictObject):
## end of http://code.activestate.com/recipes/573463/ }}}


class XFormToDict:
def __init__(self, root):
if isinstance(root, basestring):
parser = etree.XMLParser(remove_comments=True)
if os.path.exists(root):
self._root = etree.parse(root, parser=parser).getroot()
else:
self._root = etree.fromstring(root, parser)
self._dict = ConvertXmlToDict(self._root)
elif not isinstance(root, etree._Element):
raise TypeError('Expected ElementTree.Element or file path string')

def get_dict(self):
json_str = json.dumps(self._dict)
for k in self._root.nsmap:
json_str = json_str.replace('{%s}' % self._root.nsmap[k], '')
return json.loads(json_str)


def create_survey_element_from_xml(xml_file):
sb = XFormToDictBuilder(xml_file)
return sb.survey()
Expand All @@ -195,12 +175,16 @@ class XFormToDictBuilder:
'string': 'text'
}

def __init__(self, xml_file_or_string):
# TODO: File or string...
assert os.path.isfile(xml_file_or_string) or isinstance(xml_file_or_string, basestring)

doc_as_dict = XFormToDict(xml_file_or_string).get_dict()
self._xmldict = doc_as_dict
def __init__(self, path=None, filelike_obj=None):
if path:
assert os.path.isfile(path)
with open(path) as f:
doc_as_dict= self.get_dict_from_xml(f)
elif filelike_obj:
doc_as_dict= self.get_dict_from_xml(filelike_obj)
else:
raise RuntimeError('\'XFormToDictBuilder()\' requires either the '\
+ '\'path\' or the \'filelike_obj\' parameter.')

assert 'html' in doc_as_dict
assert 'body' in doc_as_dict['html']
Expand Down Expand Up @@ -250,6 +234,19 @@ def __init__(self, xml_file_or_string):
self._cleanup_children()
self.new_doc[constants.CHILDREN] = self.children


@staticmethod
def get_dict_from_xml(xml_file_object):
parser = etree.XMLParser(remove_comments=True)
xml_root= etree.parse(xml_file_object, parser=parser).getroot()
xml_dict= ConvertXmlToDict(xml_root)

json_str = json.dumps(xml_dict)
for k in xml_root.nsmap:
json_str = json_str.replace('{%s}' % xml_root.nsmap[k], '')
return json.loads(json_str)


def _set_binding_order(self):
self.ordered_binding_refs = []
for bind in self.bindings:
Expand Down

0 comments on commit 69cf43c

Please sign in to comment.