a0f3c2a85335dbc0642e433379a455c45fcd2622
[debian/gnuradio] / grc / src / grc_gnuradio / utils / convert_hier.py
1 """
2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
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.
9
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.
14
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
18 """
19 ##@package grc_gnuradio.utils.convert_hier
20 #Utility functions to convert a grc hier block to an xml wrapper
21 #@author Josh Blum
22
23 from grc_gnuradio.Constants import BLOCK_DTD
24 from grc import ParseXML
25 from grc.Utils import odict
26
27 def convert_hier(flow_graph, python_file):
28         #extract info from the flow graph
29         input_sig = flow_graph.get_input_signature()
30         output_sig = flow_graph.get_output_signature()
31         parameters = flow_graph.get_parameters()
32         block_key = flow_graph.get_option('id')
33         block_name = flow_graph.get_option('title')
34         block_category = flow_graph.get_option('category')
35         block_desc = flow_graph.get_option('description')
36         block_author = flow_graph.get_option('author')
37         #build the nested data
38         block_n = odict()
39         block_n['name'] = block_name
40         block_n['key'] = block_key
41         block_n['category'] = block_category
42         block_n['import'] = 'execfile("%s")'%python_file
43         #make data
44         block_n['make'] = '%s(\n\t%s,\n)'%(
45                 block_key,
46                 ',\n\t'.join(['%s=$%s'%(param.get_id(), param.get_id()) for param in parameters]),
47         )
48         #callback data
49         block_n['callback'] = ['set_%s($%s)'%(param.get_id(), param.get_id()) for param in parameters]
50         #param data
51         params_n = list()
52         for param in parameters:
53                 param_n = odict()
54                 param_n['name'] = param.get_param('label').get_value() or param.get_id()
55                 param_n['key'] = param.get_id()
56                 param_n['value'] = param.get_param('value').get_value()
57                 param_n['type'] = 'raw'
58                 params_n.append(param_n)
59         block_n['param'] = params_n
60         #sink data
61         if int(input_sig['nports']):
62                 sink_n = odict()
63                 sink_n['name'] = 'in'
64                 sink_n['type'] = input_sig['type']
65                 sink_n['vlen'] = input_sig['vlen']
66                 sink_n['nports'] = input_sig['nports']
67                 block_n['sink'] = sink_n
68         #source data
69         if int(output_sig['nports']):
70                 source_n = odict()
71                 source_n['name'] = 'out'
72                 source_n['type'] = output_sig['type']
73                 source_n['vlen'] = output_sig['vlen']
74                 source_n['nports'] = output_sig['nports']
75                 block_n['source'] = source_n
76         #doc data
77         block_n['doc'] = "%s\n%s\n%s"%(block_author, block_desc, python_file)
78         #write the block_n to file
79         xml_file = python_file + '.xml'
80         ParseXML.to_file({'block': block_n}, xml_file)
81         ParseXML.validate_dtd(xml_file, BLOCK_DTD)