commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 04/06: grc: clean-up ParseXML


From: git
Subject: [Commit-gnuradio] [gnuradio] 04/06: grc: clean-up ParseXML
Date: Sun, 14 Jun 2015 16:34:14 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch maint
in repository gnuradio.

commit 6c34b527cb4e083b4806c1e34e5503639591f8b7
Author: Sebastian Koslowski <address@hidden>
Date:   Tue Jun 2 16:13:36 2015 +0200

    grc: clean-up ParseXML
---
 grc/base/ParseXML.py | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/grc/base/ParseXML.py b/grc/base/ParseXML.py
index a2cede1..c9dbccf 100644
--- a/grc/base/ParseXML.py
+++ b/grc/base/ParseXML.py
@@ -41,9 +41,9 @@ def validate_dtd(xml_file, dtd_file=None):
         dtd_file: the optional dtd file
     @throws Exception validation fails
     """
-    #perform parsing, use dtd validation if dtd file is not specified
+    # perform parsing, use dtd validation if dtd file is not specified
+    parser = etree.XMLParser(dtd_validation=not dtd_file)
     try:
-        parser = etree.XMLParser(dtd_validation=not dtd_file)
         xml = etree.parse(xml_file, parser=parser)
     except etree.LxmlError:
         pass
@@ -101,9 +101,10 @@ def _from_file(xml):
         key, value = _from_file(elem).items()[0]
         if nested_data.has_key(key): nested_data[key].append(value)
         else: nested_data[key] = [value]
-    #delistify if the length of values is 1
+    # delistify if the length of values is 1
     for key, values in nested_data.iteritems():
-        if len(values) == 1: nested_data[key] = values[0]
+        if len(values) == 1:
+            nested_data[key] = values[0]
 
     return odict({tag: nested_data})
 
@@ -116,15 +117,16 @@ def to_file(nested_data, xml_file):
         nested_data: the nested data
         xml_file: the xml file path
     """
-    # Create the processing instruction from the array
     xml_data = ""
     instructions = nested_data.pop('_instructions', None)
-    if instructions:
+    if instructions:  # create the processing instruction from the array
         xml_data += etree.tostring(etree.ProcessingInstruction(
-            'grc', ' '.join("{0}='{1}'".format(*item) for item in 
instructions.iteritems())
+            'grc', ' '.join(
+                "{0}='{1}'".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)
+    with open(xml_file, 'w') as fp:
+        fp.write(xml_data)
 
 
 def _to_file(nested_data):
@@ -139,12 +141,14 @@ def _to_file(nested_data):
     """
     nodes = list()
     for key, values in nested_data.iteritems():
-        #listify the values if not a list
+        # listify the values if not a list
         if not isinstance(values, (list, set, tuple)):
             values = [values]
         for value in values:
             node = etree.Element(key)
-            if isinstance(value, (str, unicode)): node.text = value
-            else: node.extend(_to_file(value))
+            if isinstance(value, (str, unicode)):
+                node.text = value
+            else:
+                node.extend(_to_file(value))
             nodes.append(node)
     return nodes



reply via email to

[Prev in Thread] Current Thread [Next in Thread]