747d2f445a9f4a901ea3bf816d53bc736e468cb1
[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
22 from grc_gnuradio.Constants import BLOCK_DTD
23 from grc import ParseXML
24 from grc.Utils import odict
25
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
37         block_n = odict()
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
42         #make data
43         block_n['make'] = '%s(\n\t%s,\n)'%(
44                 block_key,
45                 ',\n\t'.join(['%s=$%s'%(param.get_id(), param.get_id()) for param in parameters]),
46         )
47         #callback data
48         block_n['callback'] = ['set_%s($%s)'%(param.get_id(), param.get_id()) for param in parameters]
49         #param data
50         params_n = list()
51         for param in parameters:
52                 param_n = odict()
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
59         #sink data
60         if int(input_sig['nports']):
61                 sink_n = odict()
62                 sink_n['name'] = 'in'
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
67         #source data
68         if int(output_sig['nports']):
69                 source_n = odict()
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
75         #doc data
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)