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
19 ##@package grc_gnuradio.utils.convert_hier
20 #Utility functions to convert a grc hier block to an xml wrapper
22 from grc_gnuradio.Constants import BLOCK_DTD
23 from grc import ParseXML
24 from grc.Utils import odict
26 def convert_hier(flow_graph, python_file):
27 #extract info from the flow graph
28 input_sig = flow_graph.get_input_signature()
29 output_sig = flow_graph.get_output_signature()
30 parameters = flow_graph.get_parameters()
31 block_key = flow_graph.get_option('id')
32 block_name = flow_graph.get_option('title')
33 block_category = flow_graph.get_option('category')
34 block_desc = flow_graph.get_option('description')
35 block_author = flow_graph.get_option('author')
36 #build the nested data
38 block_n['name'] = block_name
39 block_n['key'] = block_key
40 block_n['category'] = block_category
41 block_n['import'] = 'execfile("%s")'%python_file
43 block_n['make'] = '%s(\n\t%s,\n)'%(
45 ',\n\t'.join(['%s=$%s'%(param.get_id(), param.get_id()) for param in parameters]),
48 block_n['callback'] = ['set_%s($%s)'%(param.get_id(), param.get_id()) for param in parameters]
51 for param in parameters:
53 param_n['name'] = param.get_param('label').get_value() or param.get_id()
54 param_n['key'] = param.get_id()
55 param_n['value'] = param.get_param('value').get_value()
56 param_n['type'] = 'raw'
57 params_n.append(param_n)
58 block_n['param'] = params_n
60 if int(input_sig['nports']):
63 sink_n['type'] = input_sig['type']
64 sink_n['vlen'] = input_sig['vlen']
65 sink_n['nports'] = input_sig['nports']
66 block_n['sink'] = sink_n
68 if int(output_sig['nports']):
70 source_n['name'] = 'out'
71 source_n['type'] = output_sig['type']
72 source_n['vlen'] = output_sig['vlen']
73 source_n['nports'] = output_sig['nports']
74 block_n['source'] = source_n
76 block_n['doc'] = "%s\n%s\n%s"%(block_author, block_desc, python_file)
77 #write the block_n to file
78 xml_file = python_file + '.xml'
79 ParseXML.to_file({'block': block_n}, xml_file)
80 ParseXML.validate_dtd(xml_file, BLOCK_DTD)