Imported Upstream version 3.2.2
[debian/gnuradio] / grc / base / Block.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 . import odict
21 from Element import Element
22 from Param import Param
23 from Port import Port
24
25 from Cheetah.Template import Template
26 from UserDict import UserDict
27
28 class TemplateArg(UserDict):
29         """
30         A cheetah template argument created from a param.
31         The str of this class evaluates to the param's to code method.
32         The use of this class as a dictionary (enum only) will reveal the enum opts.
33         The __call__ or () method can return the param evaluated to a raw python data type.
34         """
35
36         def __init__(self, param):
37                 UserDict.__init__(self)
38                 self._param = param
39                 if param.is_enum():
40                         for key in param.get_opt_keys():
41                                 self[key] = str(param.get_opt(key))
42
43         def __str__(self):
44                 return str(self._param.to_code())
45
46         def __call__(self):
47                 return self._param.get_evaluated()
48
49 class Block(Element):
50
51         def __init__(self, flow_graph, n):
52                 """
53                 Make a new block from nested data.
54                 @param flow graph the parent element
55                 @param n the nested odict
56                 @return block a new block
57                 """
58                 #build the block
59                 Element.__init__(self, flow_graph)
60                 #grab the data
61                 params = n.findall('param')
62                 sources = n.findall('source')
63                 sinks = n.findall('sink')
64                 self._name = n.find('name')
65                 self._key = n.find('key')
66                 self._category = n.find('category') or ''
67                 self._block_wrapper_path = n.find('block_wrapper_path')
68                 #create the param objects
69                 self._params = odict()
70                 #add the id param
71                 self._params['id'] = self.get_parent().get_parent().Param(
72                         self,
73                         odict({
74                                 'name': 'ID',
75                                 'key': 'id',
76                                 'type': 'id',
77                         })
78                 )
79                 self._params['_enabled'] = self.get_parent().get_parent().Param(
80                         self,
81                         odict({
82                                 'name': 'Enabled',
83                                 'key': '_enabled',
84                                 'type': 'raw',
85                                 'value': 'True',
86                                 'hide': 'all',
87                         })
88                 )
89                 for param in map(lambda n: self.get_parent().get_parent().Param(self, n), params):
90                         key = param.get_key()
91                         #test against repeated keys
92                         try: assert key not in self.get_param_keys()
93                         except AssertionError: raise Exception, 'Key "%s" already exists in params'%key
94                         #store the param
95                         self._params[key] = param
96                 #create the source objects
97                 self._sources = odict()
98                 for source in map(lambda n: self.get_parent().get_parent().Source(self, n), sources):
99                         key = source.get_key()
100                         #test against repeated keys
101                         try: assert key not in self.get_source_keys()
102                         except AssertionError: raise Exception, 'Key "%s" already exists in sources'%key
103                         #store the port
104                         self._sources[key] = source
105                 #create the sink objects
106                 self._sinks = odict()
107                 for sink in map(lambda n: self.get_parent().get_parent().Sink(self, n), sinks):
108                         key = sink.get_key()
109                         #test against repeated keys
110                         try: assert key not in self.get_sink_keys()
111                         except AssertionError: raise Exception, 'Key "%s" already exists in sinks'%key
112                         #store the port
113                         self._sinks[key] = sink
114                 #begin the testing
115                 self.test()
116
117         def test(self):
118                 """
119                 Call test on all children.
120                 """
121                 map(lambda c: c.test(), self.get_params() + self.get_sinks() + self.get_sources())
122
123         def get_enabled(self):
124                 """
125                 Get the enabled state of the block.
126                 @return true for enabled
127                 """
128                 try: return eval(self.get_param('_enabled').get_value())
129                 except: return True
130
131         def set_enabled(self, enabled):
132                 """
133                 Set the enabled state of the block.
134                 @param enabled true for enabled
135                 """
136                 self.get_param('_enabled').set_value(str(enabled))
137
138         def validate(self):
139                 """
140                 Validate the block.
141                 All ports and params must be valid.
142                 All checks must evaluate to true.
143                 """
144                 Element.validate(self)
145                 for c in self.get_params() + self.get_ports() + self.get_connections():
146                         try:
147                                 c.validate()
148                                 assert c.is_valid()
149                         except AssertionError:
150                                 for msg in c.get_error_messages():
151                                         self.add_error_message('>>> %s:\n\t%s'%(c, msg))
152
153         def __str__(self): return 'Block - %s - %s(%s)'%(self.get_id(), self.get_name(), self.get_key())
154
155         def get_id(self): return self.get_param('id').get_value()
156         def is_block(self): return True
157         def get_name(self): return self._name
158         def get_key(self): return self._key
159         def get_category(self): return self._category
160         def get_doc(self): return ''
161         def get_ports(self): return self.get_sources() + self.get_sinks()
162         def get_block_wrapper_path(self): return self._block_wrapper_path
163
164         ##############################################
165         # Access Params
166         ##############################################
167         def get_param_keys(self): return self._params.keys()
168         def get_param(self, key): return self._params[key]
169         def get_params(self): return self._params.values()
170
171         ##############################################
172         # Access Sinks
173         ##############################################
174         def get_sink_keys(self): return self._sinks.keys()
175         def get_sink(self, key): return self._sinks[key]
176         def get_sinks(self): return self._sinks.values()
177
178         ##############################################
179         # Access Sources
180         ##############################################
181         def get_source_keys(self): return self._sources.keys()
182         def get_source(self, key): return self._sources[key]
183         def get_sources(self): return self._sources.values()
184
185         def get_connections(self):
186                 return sum([port.get_connections() for port in self.get_ports()], [])
187
188         def resolve_dependencies(self, tmpl):
189                 """
190                 Resolve a paramater dependency with cheetah templates.
191                 @param tmpl the string with dependencies
192                 @return the resolved value
193                 """
194                 tmpl = str(tmpl)
195                 if '$' not in tmpl: return tmpl
196                 n = dict((p.get_key(), TemplateArg(p)) for p in self.get_params())
197                 try: return str(Template(tmpl, n))
198                 except Exception, e: return "-------->\n%s: %s\n<--------"%(e, tmpl)
199
200         ##############################################
201         # Controller Modify
202         ##############################################
203         def type_controller_modify(self, direction):
204                 """
205                 Change the type controller.
206                 @param direction +1 or -1
207                 @return true for change
208                 """
209                 changed = False
210                 type_param = None
211                 for param in filter(lambda p: p.is_enum(), self.get_params()):
212                         children = self.get_ports() + self.get_params()
213                         #priority to the type controller
214                         if param.get_key() in ' '.join(map(lambda p: p._type, children)): type_param = param
215                         #use param if type param is unset
216                         if not type_param: type_param = param
217                 if type_param:
218                         #try to increment the enum by direction
219                         try:
220                                 keys = type_param.get_option_keys()
221                                 old_index = keys.index(type_param.get_value())
222                                 new_index = (old_index + direction + len(keys))%len(keys)
223                                 type_param.set_value(keys[new_index])
224                                 changed = True
225                         except: pass
226                 return changed
227
228         def port_controller_modify(self, direction):
229                 """
230                 Change the port controller.
231                 @param direction +1 or -1
232                 @return true for change
233                 """
234                 return False
235
236         ##############################################
237         ## Import/Export Methods
238         ##############################################
239         def export_data(self):
240                 """
241                 Export this block's params to nested data.
242                 @return a nested data odict
243                 """
244                 n = odict()
245                 n['key'] = self.get_key()
246                 n['param'] = map(lambda p: p.export_data(), self.get_params())
247                 return n
248
249         def import_data(self, n):
250                 """
251                 Import this block's params from nested data.
252                 Any param keys that do not exist will be ignored.
253                 @param n the nested data odict
254                 """
255                 params_n = n.findall('param')
256                 for param_n in params_n:
257                         key = param_n.find('key')
258                         value = param_n.find('value')
259                         #the key must exist in this block's params
260                         if key in self.get_param_keys():
261                                 self.get_param(key).set_value(value)