added include <cstdio> statements in several files to make it compatible with g+...
[debian/gnuradio] / grc / src / platforms / python / 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
20 from .. Constants import BLOCK_DTD
21 from .... utils import ParseXML
22 from .... utils import odict
23
24 def convert_hier(flow_graph, python_file):
25         #extract info from the flow graph
26         input_sig = flow_graph.get_input_signature()
27         output_sig = flow_graph.get_output_signature()
28         parameters = flow_graph.get_parameters()
29         block_key = flow_graph.get_option('id')
30         block_name = flow_graph.get_option('title')
31         block_category = flow_graph.get_option('category')
32         block_desc = flow_graph.get_option('description')
33         block_author = flow_graph.get_option('author')
34         #build the nested data
35         block_n = odict()
36         block_n['name'] = block_name
37         block_n['key'] = block_key
38         block_n['category'] = block_category
39         block_n['import'] = 'execfile("%s")'%python_file
40         #make data
41         block_n['make'] = '%s(\n\t%s,\n)'%(
42                 block_key,
43                 ',\n\t'.join(['%s=$%s'%(param.get_id(), param.get_id()) for param in parameters]),
44         )
45         #callback data
46         block_n['callback'] = ['set_%s($%s)'%(param.get_id(), param.get_id()) for param in parameters]
47         #param data
48         params_n = list()
49         for param in parameters:
50                 param_n = odict()
51                 param_n['name'] = param.get_param('label').get_value() or param.get_id()
52                 param_n['key'] = param.get_id()
53                 param_n['value'] = param.get_param('value').get_value()
54                 param_n['type'] = 'raw'
55                 params_n.append(param_n)
56         block_n['param'] = params_n
57         #sink data
58         if int(input_sig['nports']):
59                 sink_n = odict()
60                 sink_n['name'] = 'in'
61                 sink_n['type'] = input_sig['type']
62                 sink_n['vlen'] = input_sig['vlen']
63                 sink_n['nports'] = input_sig['nports']
64                 block_n['sink'] = sink_n
65         #source data
66         if int(output_sig['nports']):
67                 source_n = odict()
68                 source_n['name'] = 'out'
69                 source_n['type'] = output_sig['type']
70                 source_n['vlen'] = output_sig['vlen']
71                 source_n['nports'] = output_sig['nports']
72                 block_n['source'] = source_n
73         #doc data
74         block_n['doc'] = "%s\n%s\n%s"%(block_author, block_desc, python_file)
75         #write the block_n to file
76         xml_file = python_file + '.xml'
77         ParseXML.to_file({'block': block_n}, xml_file)
78         ParseXML.validate_dtd(xml_file, BLOCK_DTD)