diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2014-08-23 19:54:41 +0200 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2014-08-26 13:14:21 +0200 |
commit | 4abbcf925525035414f03af5366e2eddd49e85f7 (patch) | |
tree | 360b58bbc639638a56f54b380f81e948a209e646 /grc | |
parent | a8c8af1d032f5622168c3b2636800f1e6647267b (diff) |
grc: moving xml pi into nested data
Diffstat (limited to 'grc')
-rw-r--r-- | grc/base/Constants.py | 1 | ||||
-rw-r--r-- | grc/base/FlowGraph.py | 11 | ||||
-rw-r--r-- | grc/base/ParseXML.py | 82 | ||||
-rw-r--r-- | grc/base/Platform.py | 8 | ||||
-rw-r--r-- | grc/gui/ActionHandler.py | 3 |
5 files changed, 36 insertions, 69 deletions
diff --git a/grc/base/Constants.py b/grc/base/Constants.py index e5026d9da7..73f92937cf 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 78e70208f3..583d7d4af7 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 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 diff --git a/grc/base/Platform.py b/grc/base/Platform.py index eea381c7b1..095b70cc16 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 a8c91b4978..60fa07050d 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: |