diff options
Diffstat (limited to 'grc/src/utils')
-rw-r--r-- | grc/src/utils/Makefile.am | 30 | ||||
-rw-r--r-- | grc/src/utils/ParseXML.py | 102 | ||||
-rw-r--r-- | grc/src/utils/__init__.py | 20 | ||||
-rw-r--r-- | grc/src/utils/converter.py | 248 | ||||
-rw-r--r-- | grc/src/utils/odict.py | 93 |
5 files changed, 0 insertions, 493 deletions
diff --git a/grc/src/utils/Makefile.am b/grc/src/utils/Makefile.am deleted file mode 100644 index a48904796..000000000 --- a/grc/src/utils/Makefile.am +++ /dev/null @@ -1,30 +0,0 @@ -# -# Copyright 2008, 2009 Free Software Foundation, Inc. -# -# This file is part of GNU Radio -# -# GNU Radio is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3, or (at your option) -# any later version. -# -# GNU Radio is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with GNU Radio; see the file COPYING. If not, write to -# the Free Software Foundation, Inc., 51 Franklin Street, -# Boston, MA 02110-1301, USA. -# - -include $(top_srcdir)/grc/Makefile.inc - -ourpythondir = $(grc_src_prefix)/utils - -ourpython_PYTHON = \ - converter.py \ - odict.py \ - ParseXML.py \ - __init__.py diff --git a/grc/src/utils/ParseXML.py b/grc/src/utils/ParseXML.py deleted file mode 100644 index d05b7b8b5..000000000 --- a/grc/src/utils/ParseXML.py +++ /dev/null @@ -1,102 +0,0 @@ -""" -Copyright 2008 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" - -from lxml import etree -from .. utils import odict - -class XMLSyntaxError(Exception): - def __init__(self, error_log): - self._error_log = error_log - def __str__(self): - return '\n'.join(map(str, self._error_log.filter_from_errors())) - -def validate_dtd(xml_file, dtd_file=None): - """ - Validate an xml file against its dtd. - @param xml_file the xml file - @param dtd_file the optional dtd file - @throws Exception validation fails - """ - #perform parsing, use dtd validation if dtd file is not specified - parser = etree.XMLParser(dtd_validation=not dtd_file) - xml = etree.parse(xml_file, parser=parser) - if parser.error_log: raise XMLSyntaxError(parser.error_log) - #perform dtd validation if the dtd file is specified - if not dtd_file: return - dtd = etree.DTD(dtd_file) - if not dtd.validate(xml.getroot()): raise XMLSyntaxError(dtd.error_log) - -def from_file(xml_file): - """ - Create nested data from an xml file using the from xml helper. - @param xml_file the xml file path - @return the nested data - """ - xml = etree.parse(xml_file).getroot() - return _from_file(xml) - -def _from_file(xml): - """ - Recursivly parse the xml tree into nested data format. - @param xml the xml tree - @return the nested data - """ - tag = xml.tag - if not len(xml): - 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] - if nested_data.has_key(key): nested_data[key].append(value) - else: nested_data[key] = [value] - #delistify if the length of values is 1 - for key, values in nested_data.iteritems(): - if len(values) == 1: nested_data[key] = values[0] - return odict({tag: nested_data}) - -def to_file(nested_data, xml_file): - """ - Write an xml file and use the to xml helper method to load it. - @param nested_data the nested data - @param 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(nested_data): - """ - Recursivly parse the nested data into xml tree format. - @param nested_data the nested data - @return the xml tree filled with child nodes - """ - nodes = list() - for key, values in nested_data.iteritems(): - #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)) - nodes.append(node) - return nodes - -if __name__ == '__main__': - """Use the main method to test parse xml's functions.""" - pass diff --git a/grc/src/utils/__init__.py b/grc/src/utils/__init__.py deleted file mode 100644 index 2682db812..000000000 --- a/grc/src/utils/__init__.py +++ /dev/null @@ -1,20 +0,0 @@ -""" -Copyright 2009 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" - -from odict import odict diff --git a/grc/src/utils/converter.py b/grc/src/utils/converter.py deleted file mode 100644 index 34fc438fd..000000000 --- a/grc/src/utils/converter.py +++ /dev/null @@ -1,248 +0,0 @@ -""" -Copyright 2008 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" - -from .. platforms.base.Constants import FLOW_GRAPH_DTD -import ParseXML -from .. import utils -from .. utils import odict -from lxml import etree -import difflib -import os - -def _make_param(key, value): - """ - Make a paramater dict from the key/value pair. - @param key the key - @param value the value - @return a dictionary object - """ - param = odict() - param['key'] = key - param['value'] = value - return param - -def _get_blocks(blocks, tag): - """ - Get a list of blocks with the tag. - @param blocks the old block list - @param tag the tag name - @retun a list of matching blocks - """ - return filter(lambda b: b['tag'] == tag, blocks) - -def _get_params(block): - """ - Get a list of params. - @param block the old block - @retun a list of params - """ - params = utils.exists_or_else(block, 'params', {}) or {} - params = utils.listify(params, 'param') - return params - -def _convert_id(id): - """ - Convert an old id to a new safe id. - Replace spaces with underscores. - Lower case the odl id. - @return the reformatted id - """ - return id.lower().replace(' ', '_') - -def convert(file_path, platform): - """ - Convert the flow graph to the new format. - Make a backup of the old file. - Save a reformated flow graph to the file path. - If this is a new format flow graph, do nothing. - @param file_path the path to the saved flow graph - @param platform the grc gnuradio platform - """ - try: #return if file passes validation - ParseXML.validate_dtd(file_path, FLOW_GRAPH_DTD) - try: - changed = False - #convert instances of gui_coordinate and gui_rotation - xml = etree.parse(file_path) - for find, replace in ( - ('gr_add_vxx', 'gr_add_xx'), - ('gr_multiply_vxx', 'gr_multiply_xx'), - ): - keys = xml.xpath('/flow_graph/block[key="%s"]/key'%find) - for key in keys: - key.text = replace - changed = True - if not changed: return - #backup after successful conversion - os.rename(file_path, file_path+'.bak') - #save new flow graph to file path - xml.write(file_path, xml_declaration=True, pretty_print=True) - except Exception, e: print e - return - except: pass #convert - ############################################################ - # extract window size, variables, blocks, and connections - ############################################################ - old_n = ParseXML.from_file(file_path)['flow_graph'] - try: window_width = min(3*int(old_n['window_width'])/2, 2048) - except: window_width = 2048 - try: window_height = min(3*int(old_n['window_height'])/2, 2048) - except: window_height = 2048 - window_size = '%d, %d'%(window_width, window_height) - variables = utils.exists_or_else(old_n, 'vars', {}) or {} - variables = utils.listify(variables, 'var') - blocks = utils.exists_or_else(old_n, 'signal_blocks', {}) or {} - blocks = utils.listify(blocks, 'signal_block') - connections = utils.exists_or_else(old_n, 'connections', {}) or {} - connections = utils.listify(connections, 'connection') - #initialize new nested data - new_n = odict() - new_n['block'] = list() - new_n['connection'] = list() - ############################################################ - # conversion - options block - ############################################################ - #get name - about_blocks = _get_blocks(blocks, 'About') - if about_blocks: title = _get_params(about_blocks[0])[0] - else: title = 'Untitled' - #get author - if about_blocks: author = _get_params(about_blocks[0])[1] - else: author = '' - #get desc - note_blocks = _get_blocks(blocks, 'Note') - if note_blocks: desc = _get_params(note_blocks[0])[0] - else: desc = '' - #create options block - options_block = odict() - options_block['key'] = 'options' - options_block['param'] = [ - _make_param('id', 'top_block'), - _make_param('title', title), - _make_param('author', author), - _make_param('description', desc), - _make_param('window_size', window_size), - _make_param('_coordinate', '(10, 10)'), - ] - #append options block - new_n['block'].append(options_block) - ############################################################ - # conversion - variables - ############################################################ - x = 100 - for variable in variables: - key = variable['key'] - value = variable['value'] - minimum = utils.exists_or_else(variable, 'min', '') - maximum = utils.exists_or_else(variable, 'max', '') - step = utils.exists_or_else(variable, 'step', '') - x = x + 150 - coor = '(%d, %d)'%(x, 10) - var_block = odict() - if minimum and maximum: #slider varible - #determine num steps - try: num_steps = str(int((float(maximum) - float(minimum))/float(step))) - except: num_steps = '100' - var_block['key'] = 'variable_slider' - var_block['param'] = [ - _make_param('id', key), - _make_param('value', value), - _make_param('min', minimum), - _make_param('max', maximum), - _make_param('num_steps', num_steps), - _make_param('_coordinate', coor), - ] - else: #regular variable - var_block['key'] = 'variable' - var_block['param'] = [ - _make_param('id', key), - _make_param('value', value), - _make_param('_coordinate', coor), - ] - #append variable block - new_n['block'].append(var_block) - ############################################################ - # conversion - blocks - ############################################################ - #create name to key map for all blocks in platform - name_to_key = dict((b.get_name(), b.get_key()) for b in platform.get_blocks()) - for block in blocks: - #extract info - tag = block['tag'] - #ignore list - if tag in ('Note', 'About'): continue - id = _convert_id(block['id']) - coor = '(%s, %s + 100)'%( - utils.exists_or_else(block, 'x_coordinate', '0'), - utils.exists_or_else(block, 'y_coordinate', '0'), - ) - rot = utils.exists_or_else(block, 'rotation', '0') - params = _get_params(block) - #new block - new_block = odict() - matches = difflib.get_close_matches(tag, name_to_key.keys(), 1) - if not matches: continue - #match found - key = name_to_key[matches[0]] - new_block['key'] = key - new_block['param'] = [ - _make_param('id', id), - _make_param('_coordinate', coor), - _make_param('_rotation', rot), - ] - #handle specific blocks - if key == 'wxgui_fftsink2': - params = params[0:3] + ['0'] + params[3:4] + ['8'] + params[4:] - #append params - for i, param in enumerate(params): - platform_block = platform.get_block(key) - try: platform_param = platform_block.get_params()[i+2] - except IndexError: break - if platform_param.is_enum(): - try: param_value = platform_param.get_option_keys()[int(param)] - except: param_value = platform_param.get_option_keys()[0] - else: - param_value = param.replace('$', '').replace('^', '**') - new_block['param'].append(_make_param(platform_param.get_key(), param_value)) - #append block - new_n['block'].append(new_block) - ############################################################ - # conversion - connections - ############################################################ - for connection in connections: - #extract info - input_signal_block_id = connection['input_signal_block_id'] - input_socket_index = connection['input_socket_index'] - output_signal_block_id = connection['output_signal_block_id'] - output_socket_index = connection['output_socket_index'] - #new connection - new_conn = odict() - new_conn['source_block_id'] = _convert_id(output_signal_block_id) - new_conn['sink_block_id'] = _convert_id(input_signal_block_id) - new_conn['source_key'] = output_socket_index - new_conn['sink_key'] = input_socket_index - #append connection - new_n['connection'].append(new_conn) - ############################################################ - # backup and replace - ############################################################ - #backup after successful conversion - os.rename(file_path, file_path+'.bak') - #save new flow graph to file path - ParseXML.to_file({'flow_graph': new_n}, file_path) diff --git a/grc/src/utils/odict.py b/grc/src/utils/odict.py deleted file mode 100644 index ac3cb2070..000000000 --- a/grc/src/utils/odict.py +++ /dev/null @@ -1,93 +0,0 @@ -""" -Copyright 2008, 2009 Free Software Foundation, Inc. -This file is part of GNU Radio - -GNU Radio Companion is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -GNU Radio Companion is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -""" - -from UserDict import DictMixin - -class odict(DictMixin): - - def __init__(self, d={}): - self._keys = list(d.keys()) - self._data = dict(d.copy()) - - def __setitem__(self, key, value): - if key not in self._data: - self._keys.append(key) - self._data[key] = value - - def __getitem__(self, key): - return self._data[key] - - def __delitem__(self, key): - del self._data[key] - self._keys.remove(key) - - def keys(self): - return list(self._keys) - - def copy(self): - copy_dict = odict() - copy_dict._data = self._data.copy() - copy_dict._keys = list(self._keys) - return copy_dict - - def insert_after(self, pos_key, key, val): - """ - Insert the new key, value entry after the entry given by the position key. - If the positional key is None, insert at the end. - @param pos_key the positional key - @param key the key for the new entry - @param val the value for the new entry - """ - index = (pos_key is None) and len(self._keys) or self._keys.index(pos_key) - assert key not in self._keys - self._keys.insert(index+1, key) - self._data[key] = val - - def insert_before(self, pos_key, key, val): - """ - Insert the new key, value entry before the entry given by the position key. - If the positional key is None, insert at the begining. - @param pos_key the positional key - @param key the key for the new entry - @param val the value for the new entry - """ - index = (pos_key is not None) and self._keys.index(pos_key) or 0 - assert key not in self._keys - self._keys.insert(index, key) - self._data[key] = val - - def find(self, key): - """ - Get the value for this key if exists. - @param key the key to search for - @return the value or None - """ - if self.has_key(key): return self[key] - return None - - def findall(self, key): - """ - Get a list of values for this key. - @param key the key to search for - @return a list of values or empty list - """ - obj = self.find(key) - if obj is None: obj = list() - if isinstance(obj, list): return obj - return [obj] |