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