2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 from .. platforms.base.Constants import FLOW_GRAPH_DTD
22 from .. utils import odict
23 from lxml import etree
27 def _make_param(key, value):
29 Make a paramater dict from the key/value pair.
31 @param value the value
32 @return a dictionary object
36 param['value'] = value
39 def _get_blocks(blocks, tag):
41 Get a list of blocks with the tag.
42 @param blocks the old block list
43 @param tag the tag name
44 @retun a list of matching blocks
46 return filter(lambda b: b['tag'] == tag, blocks)
48 def _get_params(block):
51 @param block the old block
52 @retun a list of params
54 params = Utils.exists_or_else(block, 'params', {}) or {}
55 params = Utils.listify(params, 'param')
60 Convert an old id to a new safe id.
61 Replace spaces with underscores.
62 Lower case the odl id.
63 @return the reformatted id
65 return id.lower().replace(' ', '_')
67 def convert(file_path, platform):
69 Convert the flow graph to the new format.
70 Make a backup of the old file.
71 Save a reformated flow graph to the file path.
72 If this is a new format flow graph, do nothing.
73 @param file_path the path to the saved flow graph
74 @param platform the grc gnuradio platform
76 try: #return if file passes validation
77 ParseXML.validate_dtd(file_path, FLOW_GRAPH_DTD)
80 #convert instances of gui_coordinate and gui_rotation
81 xml = etree.parse(file_path)
82 for find, replace in (
83 ('gui_coordinate', '_coordinate'),
84 ('gui_rotation', '_rotation'),
86 keys = xml.xpath('/flow_graph/block/param[key="%s"]/key'%find)
90 if not changed: return
91 #backup after successful conversion
92 os.rename(file_path, file_path+'.bak')
93 #save new flow graph to file path
94 xml.write(file_path, xml_declaration=True, pretty_print=True)
95 except Exception, e: print e
98 ############################################################
99 # extract window size, variables, blocks, and connections
100 ############################################################
101 old_n = ParseXML.from_file(file_path)['flow_graph']
102 try: window_width = min(3*int(old_n['window_width'])/2, 2048)
103 except: window_width = 2048
104 try: window_height = min(3*int(old_n['window_height'])/2, 2048)
105 except: window_height = 2048
106 window_size = '%d, %d'%(window_width, window_height)
107 variables = Utils.exists_or_else(old_n, 'vars', {}) or {}
108 variables = Utils.listify(variables, 'var')
109 blocks = Utils.exists_or_else(old_n, 'signal_blocks', {}) or {}
110 blocks = Utils.listify(blocks, 'signal_block')
111 connections = Utils.exists_or_else(old_n, 'connections', {}) or {}
112 connections = Utils.listify(connections, 'connection')
113 #initialize new nested data
115 new_n['block'] = list()
116 new_n['connection'] = list()
117 ############################################################
118 # conversion - options block
119 ############################################################
121 about_blocks = _get_blocks(blocks, 'About')
122 if about_blocks: title = _get_params(about_blocks[0])[0]
123 else: title = 'Untitled'
125 if about_blocks: author = _get_params(about_blocks[0])[1]
128 note_blocks = _get_blocks(blocks, 'Note')
129 if note_blocks: desc = _get_params(note_blocks[0])[0]
131 #create options block
132 options_block = odict()
133 options_block['key'] = 'options'
134 options_block['param'] = [
135 _make_param('id', 'top_block'),
136 _make_param('title', title),
137 _make_param('author', author),
138 _make_param('description', desc),
139 _make_param('window_size', window_size),
140 _make_param('_coordinate', '(10, 10)'),
142 #append options block
143 new_n['block'].append(options_block)
144 ############################################################
145 # conversion - variables
146 ############################################################
148 for variable in variables:
149 key = variable['key']
150 value = variable['value']
151 minimum = Utils.exists_or_else(variable, 'min', '')
152 maximum = Utils.exists_or_else(variable, 'max', '')
153 step = Utils.exists_or_else(variable, 'step', '')
155 coor = '(%d, %d)'%(x, 10)
157 if minimum and maximum: #slider varible
159 try: num_steps = str(int((float(maximum) - float(minimum))/float(step)))
160 except: num_steps = '100'
161 var_block['key'] = 'variable_slider'
162 var_block['param'] = [
163 _make_param('id', key),
164 _make_param('value', value),
165 _make_param('min', minimum),
166 _make_param('max', maximum),
167 _make_param('num_steps', num_steps),
168 _make_param('_coordinate', coor),
170 else: #regular variable
171 var_block['key'] = 'variable'
172 var_block['param'] = [
173 _make_param('id', key),
174 _make_param('value', value),
175 _make_param('_coordinate', coor),
177 #append variable block
178 new_n['block'].append(var_block)
179 ############################################################
180 # conversion - blocks
181 ############################################################
182 #create name to key map for all blocks in platform
183 name_to_key = dict((b.get_name(), b.get_key()) for b in platform.get_blocks())
188 if tag in ('Note', 'About'): continue
189 id = _convert_id(block['id'])
190 coor = '(%s, %s + 100)'%(
191 Utils.exists_or_else(block, 'x_coordinate', '0'),
192 Utils.exists_or_else(block, 'y_coordinate', '0'),
194 rot = Utils.exists_or_else(block, 'rotation', '0')
195 params = _get_params(block)
198 matches = difflib.get_close_matches(tag, name_to_key.keys(), 1)
199 if not matches: continue
201 key = name_to_key[matches[0]]
202 new_block['key'] = key
203 new_block['param'] = [
204 _make_param('id', id),
205 _make_param('_coordinate', coor),
206 _make_param('_rotation', rot),
208 #handle specific blocks
209 if key == 'wxgui_fftsink2':
210 params = params[0:3] + ['0'] + params[3:4] + ['8'] + params[4:]
212 for i, param in enumerate(params):
213 platform_block = platform.get_block(key)
214 try: platform_param = platform_block.get_params()[i+2]
215 except IndexError: break
216 if platform_param.is_enum():
217 try: param_value = platform_param.get_option_keys()[int(param)]
218 except: param_value = platform_param.get_option_keys()[0]
220 param_value = param.replace('$', '').replace('^', '**')
221 new_block['param'].append(_make_param(platform_param.get_key(), param_value))
223 new_n['block'].append(new_block)
224 ############################################################
225 # conversion - connections
226 ############################################################
227 for connection in connections:
229 input_signal_block_id = connection['input_signal_block_id']
230 input_socket_index = connection['input_socket_index']
231 output_signal_block_id = connection['output_signal_block_id']
232 output_socket_index = connection['output_socket_index']
235 new_conn['source_block_id'] = _convert_id(output_signal_block_id)
236 new_conn['sink_block_id'] = _convert_id(input_signal_block_id)
237 new_conn['source_key'] = output_socket_index
238 new_conn['sink_key'] = input_socket_index
240 new_n['connection'].append(new_conn)
241 ############################################################
243 ############################################################
244 #backup after successful conversion
245 os.rename(file_path, file_path+'.bak')
246 #save new flow graph to file path
247 ParseXML.to_file({'flow_graph': new_n}, file_path)