2 Copyright 2008, 2009 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
22 from Cheetah.Template import Template
24 from Constants import \
25 TOP_BLOCK_FILE_MODE, HIER_BLOCK_FILE_MODE, \
26 HIER_BLOCKS_LIB_DIR, PYEXEC, \
29 from .. gui import Messages
31 class Generator(object):
33 def __init__(self, flow_graph, file_path):
35 Initialize the generator object.
36 Determine the file to generate.
37 @param flow_graph the flow graph object
38 @param file_path the path to write the file to
40 self._flow_graph = flow_graph
41 self._generate_options = self._flow_graph.get_option('generate_options')
42 if self._generate_options == 'hb':
43 self._mode = HIER_BLOCK_FILE_MODE
44 dirname = HIER_BLOCKS_LIB_DIR
46 self._mode = TOP_BLOCK_FILE_MODE
47 dirname = os.path.dirname(file_path)
48 filename = self._flow_graph.get_option('id') + '.py'
49 self._file_path = os.path.join(dirname, filename)
51 def get_file_path(self): return self._file_path
55 all_keys = ' '.join(map(lambda b: b.get_key(), self._flow_graph.get_enabled_blocks()))
56 if ('usrp' not in all_keys) and ('audio' not in all_keys) and ('throttle' not in all_keys) and self._generate_options != 'hb':
57 Messages.send_warning('''\
58 This flow graph may not have flow control: no audio or usrp blocks found. \
59 Add a Misc->Throttle block to your flow graph to avoid CPU congestion.''')
61 open(self.get_file_path(), 'w').write(str(self))
62 if self._generate_options == 'hb':
63 #convert hier block to xml wrapper
64 convert_hier.convert_hier(self._flow_graph, self.get_file_path())
65 os.chmod(self.get_file_path(), self._mode)
69 Execute this python flow graph.
70 @return a popen object
73 cmds = [PYEXEC, '-u', self.get_file_path()] #-u is unbuffered stdio
74 if self._generate_options == 'no_gui':
75 cmds = ['xterm', '-e'] + cmds
76 p = subprocess.Popen(args=cmds, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, universal_newlines=True)
81 Convert the flow graph to python code.
82 @return a string of python code
84 title = self._flow_graph.get_option('title') or self._flow_graph.get_option('id').replace('_', ' ').title()
85 imports = self._flow_graph.get_imports()
86 variables = self._flow_graph.get_variables()
87 parameters = self._flow_graph.get_parameters()
88 #list of variables with controls
89 controls = filter(lambda v: v.get_make(), variables)
90 #list of blocks not including variables and imports and parameters and disabled
91 blocks = sorted(self._flow_graph.get_enabled_blocks(), lambda x, y: cmp(x.get_id(), y.get_id()))
92 probes = filter(lambda b: b.get_key().startswith('probe_'), blocks) #ensure probes are last in the block list
93 #get a list of notebooks and sort them according dependencies
94 notebooks = expr_utils.sort_objects(
95 filter(lambda b: b.get_key() == 'notebook', blocks),
96 lambda n: n.get_id(), lambda n: n.get_param('notebook').get_value(),
98 #list of regular blocks (all blocks minus the special ones)
99 blocks = filter(lambda b: b not in (imports + parameters + variables + probes + notebooks), blocks) + probes
100 #list of connections where each endpoint is enabled
101 connections = filter(lambda c: not c.is_msg(), self._flow_graph.get_enabled_connections())
102 messages = filter(lambda c: c.is_msg(), self._flow_graph.get_enabled_connections())
103 #list of variable names
104 var_ids = [var.get_id() for var in parameters + variables]
106 replace_dict = dict([(var_id, 'self.%s'%var_id) for var_id in var_ids])
109 expr_utils.expr_replace(cb, replace_dict)
110 for cb in sum([block.get_callbacks() for block in self._flow_graph.get_enabled_blocks()], [])
112 #map var id to callbacks
114 [(var_id, filter(lambda c: expr_utils.get_variable_dependencies(c, [var_id]), callbacks))
115 for var_id in var_ids]
121 'flow_graph': self._flow_graph,
122 'variables': variables,
123 'notebooks': notebooks,
124 'controls': controls,
125 'parameters': parameters,
127 'connections': connections,
128 'messages': messages,
129 'generate_options': self._generate_options,
130 'var_id2cbs': var_id2cbs,
133 t = Template(open(FLOW_GRAPH_TEMPLATE, 'r').read(), namespace)