Imported Upstream version 3.2.2
[debian/gnuradio] / grc / python / 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 .. base import ParseXML
22 from .. base 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         if parameters: 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         else: block_n['make'] = '%s()'%block_key
46         #callback data
47         block_n['callback'] = ['set_%s($%s)'%(param.get_id(), param.get_id()) for param in parameters]
48         #param data
49         params_n = list()
50         for param in parameters:
51                 param_n = odict()
52                 param_n['name'] = param.get_param('label').get_value() or param.get_id()
53                 param_n['key'] = param.get_id()
54                 param_n['value'] = param.get_param('value').get_value()
55                 param_n['type'] = 'raw'
56                 params_n.append(param_n)
57         block_n['param'] = params_n
58         #sink data
59         if int(input_sig['nports']):
60                 sink_n = odict()
61                 sink_n['name'] = 'in'
62                 sink_n['type'] = input_sig['type']
63                 sink_n['vlen'] = input_sig['vlen']
64                 sink_n['nports'] = input_sig['nports']
65                 block_n['sink'] = sink_n
66         #source data
67         if int(output_sig['nports']):
68                 source_n = odict()
69                 source_n['name'] = 'out'
70                 source_n['type'] = output_sig['type']
71                 source_n['vlen'] = output_sig['vlen']
72                 source_n['nports'] = output_sig['nports']
73                 block_n['source'] = source_n
74         #doc data
75         block_n['doc'] = "%s\n%s\n%s"%(block_author, block_desc, python_file)
76         #write the block_n to file
77         xml_file = python_file + '.xml'
78         ParseXML.to_file({'block': block_n}, xml_file)
79         ParseXML.validate_dtd(xml_file, BLOCK_DTD)