summaryrefslogtreecommitdiff
path: root/grc/base/ParseXML.py
diff options
context:
space:
mode:
Diffstat (limited to 'grc/base/ParseXML.py')
-rw-r--r--grc/base/ParseXML.py82
1 files changed, 23 insertions, 59 deletions
diff --git a/grc/base/ParseXML.py b/grc/base/ParseXML.py
index 78b96abfa1..7742e5fcb0 100644
--- a/grc/base/ParseXML.py
+++ b/grc/base/ParseXML.py
@@ -61,48 +61,31 @@ def validate_dtd(xml_file, dtd_file=None):
raise XMLSyntaxError(dtd.error_log)
-# Parse the file and also return any GRC processing instructions
-def from_file_with_instructions(xml_file):
+def from_file(xml_file):
"""
Create nested data from an xml file using the from xml helper.
- Also get the grc version information.
-
+ Also get the grc version information.
+
Args:
xml_file: the xml file path
-
+
Returns:
- the nested data, grc version information
+ the nested data with grc version information
"""
xml = etree.parse(xml_file)
+ nested_data = _from_file(xml.getroot())
# Get the embedded instructions and build a dictionary item
- instructions = None
+ nested_data['_instructions'] = {}
xml_instructions = xml.xpath('/processing-instruction()')
- for inst in xml_instructions:
- if (inst.target == 'grc'):
- instructions = inst.attrib
-
- nested_data = _from_file(xml.getroot())
- return (nested_data, instructions)
-
-
-def from_file(xml_file):
- """
- Create nested data from an xml file using the from xml helper.
-
- Args:
- xml_file: the xml file path
-
- Returns:
- the nested data
- """
- xml = etree.parse(xml_file).getroot()
- return _from_file(xml)
+ for inst in filter(lambda i: i.target == 'grc', xml_instructions):
+ nested_data['_instructions'] = inst.attrib
+ return nested_data
def _from_file(xml):
"""
- Recursivly parse the xml tree into nested data format.
+ Recursively parse the xml tree into nested data format.
Args:
xml: the xml tree
@@ -112,7 +95,7 @@ def _from_file(xml):
"""
tag = xml.tag
if not len(xml):
- return odict({tag: xml.text or ''}) #store empty tags (text is None) as empty string
+ return odict({tag: xml.text or ''}) # store empty tags (text is None) as empty string
nested_data = odict()
for elem in xml:
key, value = _from_file(elem).items()[0]
@@ -127,41 +110,26 @@ def _from_file(xml):
def to_file(nested_data, xml_file):
"""
- Write an xml file and use the to xml helper method to load it.
-
- Args:
- nested_data: the nested data
- xml_file: the xml file path
- """
- xml = _to_file(nested_data)[0]
- open(xml_file, 'w').write(etree.tostring(xml, xml_declaration=True, pretty_print=True))
-
-
-def to_file_with_instructions(nested_data, file_path, instructions):
- """
Write to an xml file and insert processing instructions for versioning
Args:
nested_data: the nested data
xml_file: the xml file path
- instructions: array of instructions to add to the xml
"""
- xml = _to_file(nested_data)[0]
-
# Create the processing instruction from the array
- pi_data = ""
- for key, value in instructions.iteritems():
- pi_data += str(key) + "=\'" + str(value) + "\' "
- pi = etree.ProcessingInstruction('grc', pi_data)
-
- xml_data = etree.tostring(pi, xml_declaration=True, pretty_print=True)
- xml_data += etree.tostring(xml, pretty_print=True)
- open(file_path, 'w').write(xml_data)
-
-
+ xml_data = ""
+ instructions = nested_data.pop('_instructions', None)
+ if instructions:
+ xml_data += etree.tostring(etree.ProcessingInstruction(
+ 'grc', ' '.join("{}='{}'".format(*item) for item in instructions.iteritems())
+ ), xml_declaration=True, pretty_print=True)
+ xml_data += etree.tostring(_to_file(nested_data)[0], pretty_print=True)
+ open(xml_file, 'w').write(xml_data)
+
+
def _to_file(nested_data):
"""
- Recursivly parse the nested data into xml tree format.
+ Recursively parse the nested data into xml tree format.
Args:
nested_data: the nested data
@@ -180,7 +148,3 @@ def _to_file(nested_data):
else: node.extend(_to_file(value))
nodes.append(node)
return nodes
-
-if __name__ == '__main__':
- """Use the main method to test parse xml's functions."""
- pass