temp fix for to_code error
[debian/gnuradio] / grc / src / grc_gnuradio / FlowGraph.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.FlowGraph
20 #Primative flow graph.
21
22 from utils import expr_utils
23 from grc.elements.FlowGraph import FlowGraph as _FlowGraph
24 from Block import Block
25 from Connection import Connection
26
27 class FlowGraph(_FlowGraph):
28
29         def _get_io_signature(self, pad_key):
30                 """!
31                 Get an io signature for this flow graph.
32                 The pad key determines the directionality of the io signature.
33                 @param pad_key a string of pad_source or pad_sink
34                 @return a dict with: type, nports, vlen, size
35                 """
36                 pads = filter(lambda b: b.get_key() == pad_key, self.get_enabled_blocks())
37                 if not pads: return {
38                         'nports': '0',
39                         'type': '',
40                         'vlen': '0',
41                         'size': '0',
42                 }
43                 pad = pads[0] #take only the first, user should not have more than 1
44                 #load io signature
45                 return {
46                         'nports': str(pad.get_param('nports').evaluate()),
47                         'type': str(pad.get_param('type').evaluate()),
48                         'vlen': str(pad.get_param('vlen').evaluate()),
49                         'size': pad.get_param('type').get_opt('size'),
50                 }
51
52         def get_input_signature(self):
53                 """!
54                 Get the io signature for the input side of this flow graph.
55                 The io signature with be "0", "0" if no pad source is present.
56                 @return a string tuple of type, num_ports, port_size
57                 """
58                 return self._get_io_signature('pad_source')
59
60         def get_output_signature(self):
61                 """!
62                 Get the io signature for the output side of this flow graph.
63                 The io signature with be "0", "0" if no pad sink is present.
64                 @return a string tuple of type, num_ports, port_size
65                 """
66                 return self._get_io_signature('pad_sink')
67
68         def get_imports(self):
69                 """!
70                 Get a set of all import statments in this flow graph namespace.
71                 @return a set of import statements
72                 """
73                 imports = sum([block.get_imports() for block in self.get_enabled_blocks()], [])
74                 imports = sorted(set(imports))
75                 return imports
76
77         def get_variables(self):
78                 """!
79                 Get a list of all variables in this flow graph namespace.
80                 Exclude paramterized variables.
81                 @return a sorted list of variable blocks in order of dependency (indep -> dep)
82                 """
83                 variables = filter(lambda b: b.get_key() in (
84                         'variable', 'variable_slider', 'variable_chooser', 'variable_text_box'
85                 ), self.get_enabled_blocks())
86                 #map var id to variable block
87                 id2var = dict([(var.get_id(), var) for var in variables])
88                 #map var id to variable code
89                 #variable code is a concatenation of all param code (without the id param)
90                 id2expr = dict([(var.get_id(), 
91                         ' '.join([param.to_code() for param in filter(lambda p: p.get_key() != 'id', var.get_params())])
92                 ) for var in variables])
93                 #sort according to dependency
94                 sorted_ids = expr_utils.sort_variables(id2expr)
95                 #create list of sorted variable blocks
96                 variables = [id2var[id] for id in sorted_ids]
97                 return variables
98
99         def get_parameters(self):
100                 """!
101                 Get a list of all paramterized variables in this flow graph namespace.
102                 @return a list of paramterized variables
103                 """
104                 parameters = filter(lambda b: b.get_key() == 'parameter', self.get_enabled_blocks())
105                 return parameters
106
107         def evaluate(self, expr):
108                 """!
109                 Evaluate the expression.
110                 @param expr the string expression
111                 @throw Exception bad expression
112                 @return the evaluated data
113                 """
114                 if self.is_flagged():
115                         self.deflag()
116                         #reload namespace
117                         n = dict()
118                         #load imports
119                         for imp in self.get_imports():
120                                 try: exec imp in n
121                                 except: pass
122                         #load parameters
123                         np = dict()
124                         for parameter in self.get_parameters():
125                                 try:
126                                         e = eval(parameter.get_param('value').to_code(), n, n)
127                                         np[parameter.get_id()] = e
128                                 except: pass
129                         n.update(np) #merge param namespace
130                         #load variables
131                         for variable in self.get_variables():
132                                 try:
133                                         if variable.get_key() == 'variable_chooser':
134                                                 choices = variable.get_param('choices').to_code()
135                                                 value_index = variable.get_param('value_index').to_code()
136                                                 e = eval("%s[%s]"%(choices, value_index), n, n)
137                                         else:
138                                                 e = eval(variable.get_param('value').to_code(), n, n)
139                                         n[variable.get_id()] = e
140                                 except: pass
141                         #make namespace public
142                         self.n = n
143                 #evaluate
144                 e = eval(expr, self.n, self.n)
145                 return e
146