commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] [gnuradio] 04/10: grc: moving xml pi into nested data


From: git
Subject: [Commit-gnuradio] [gnuradio] 04/10: grc: moving xml pi into nested data
Date: Tue, 26 Aug 2014 19:40:07 +0000 (UTC)

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

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 4abbcf925525035414f03af5366e2eddd49e85f7
Author: Sebastian Koslowski <address@hidden>
Date:   Sat Aug 23 19:54:41 2014 +0200

    grc: moving xml pi into nested data
---
 gnuradio-runtime/swig/constants.i |  2 +-
 grc/base/Constants.py             |  1 +
 grc/base/FlowGraph.py             | 11 ++++--
 grc/base/ParseXML.py              | 82 +++++++++++----------------------------
 grc/base/Platform.py              |  8 ++--
 grc/gui/ActionHandler.py          |  3 +-
 6 files changed, 37 insertions(+), 70 deletions(-)

diff --git a/gnuradio-runtime/swig/constants.i 
b/gnuradio-runtime/swig/constants.i
index d63c5e6..9b7b961 100644
--- a/gnuradio-runtime/swig/constants.i
+++ b/gnuradio-runtime/swig/constants.i
@@ -16,5 +16,5 @@ namespace gr {
   const std::string major_version();
   const std::string api_version();
   const std::string minor_version();
-  
+
 }
diff --git a/grc/base/Constants.py b/grc/base/Constants.py
index e5026d9..73f9293 100644
--- a/grc/base/Constants.py
+++ b/grc/base/Constants.py
@@ -23,6 +23,7 @@ import os
 DATA_DIR = os.path.dirname(__file__)
 FLOW_GRAPH_DTD = os.path.join(DATA_DIR, 'flow_graph.dtd')
 BLOCK_TREE_DTD = os.path.join(DATA_DIR, 'block_tree.dtd')
+FLOW_GRAPH_FILE_FORMAT_VERSION = 0
 
 # Param tabs
 DEFAULT_PARAM_TAB = "General"
diff --git a/grc/base/FlowGraph.py b/grc/base/FlowGraph.py
index 78e7020..583d7d4 100644
--- a/grc/base/FlowGraph.py
+++ b/grc/base/FlowGraph.py
@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 02110-1301, USA
 from . import odict
 from Element import Element
 from .. gui import Messages
+from . Constants import FLOW_GRAPH_FILE_FORMAT_VERSION
 
 class FlowGraph(Element):
 
@@ -246,7 +247,11 @@ class FlowGraph(Element):
         n['timestamp'] = time.ctime()
         n['block'] = [block.export_data() for block in self.get_blocks()]
         n['connection'] = [connection.export_data() for connection in 
self.get_connections()]
-        return odict({'flow_graph': n})
+        instructions = odict({
+            'created': self.get_parent().get_version_short(),
+            'format': FLOW_GRAPH_FILE_FORMAT_VERSION,
+        })
+        return odict({'flow_graph': n, '_instructions': instructions})
 
     def import_data(self, n=None):
         """
@@ -318,7 +323,7 @@ class FlowGraph(Element):
                 sink = sink_block.get_sink(sink_key)
                 #build the connection
                 self.connect(source, sink)
-            except LookupError, e: 
+            except LookupError, e:
                 Messages.send_error_load(
                     'Connection between %s(%s) and %s(%s) could not be 
made.\n\t%s'%(
                     source_block_id, source_key, sink_block_id, sink_key, e))
@@ -371,4 +376,4 @@ def _dummy_block_add_port(block, key, dir):
     if port.is_source():
         block.get_sources().append(port)
     else:
-        block.get_sinks().append(port)
\ No newline at end of file
+        block.get_sinks().append(port)
diff --git a/grc/base/ParseXML.py b/grc/base/ParseXML.py
index 78b96ab..7742e5f 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
diff --git a/grc/base/Platform.py b/grc/base/Platform.py
index eea381c..095b70c 100644
--- a/grc/base/Platform.py
+++ b/grc/base/Platform.py
@@ -53,7 +53,7 @@ class Platform(_Element):
         """
         _Element.__init__(self)
         self._name = name
-        # Save the verion string to the first 
+        # Save the verion string to the first
         self._version = version[0]
         self._version_major = version[1]
         self._version_api = version[2]
@@ -143,8 +143,7 @@ class Platform(_Element):
         flow_graph_file = flow_graph_file or self._default_flow_graph
         open(flow_graph_file, 'r')  # test open
         ParseXML.validate_dtd(flow_graph_file, FLOW_GRAPH_DTD)
-        (xml, instructions) = 
ParseXML.from_file_with_instructions(flow_graph_file)
-        return xml
+        return ParseXML.from_file(flow_graph_file)
 
     def load_block_tree(self, block_tree):
         """
@@ -190,7 +189,6 @@ class Platform(_Element):
 
     def get_generator(self): return self._generator
 
-
     ##############################################
     # Access Blocks
     ##############################################
@@ -205,7 +203,7 @@ class Platform(_Element):
     def get_version_api(self): return self._version_api
     def get_version_minor(self): return self._version_minor
     def get_version_short(self): return self._version_short
-    
+
     def get_key(self): return self._key
     def get_license(self): return self._license
     def get_website(self): return self._website
diff --git a/grc/gui/ActionHandler.py b/grc/gui/ActionHandler.py
index a8c91b4..60fa070 100644
--- a/grc/gui/ActionHandler.py
+++ b/grc/gui/ActionHandler.py
@@ -449,8 +449,7 @@ class ActionHandler:
             #otherwise try to save
             else:
                 try:
-                    instructions = {'created': 
self.platform.get_version_short(), 'compatible': '3.6.1', 'locked': True}
-                    
ParseXML.to_file_with_instructions(self.get_flow_graph().export_data(), 
self.get_page().get_file_path(), instructions)
+                    ParseXML.to_file(self.get_flow_graph().export_data(), 
self.get_page().get_file_path())
                     self.get_flow_graph().grc_file_path = 
self.get_page().get_file_path()
                     self.get_page().set_saved(True)
                 except IOError:



reply via email to

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