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
20 from .. base.Block import Block as _Block
21 from .. gui.Block import Block as _GUIBlock
23 import extract_category
25 class Block(_Block, _GUIBlock):
27 def is_virtual_sink(self): return self.get_key() == 'virtual_sink'
28 def is_virtual_source(self): return self.get_key() == 'virtual_source'
30 ##for make source to keep track of indexes
32 ##for make sink to keep track of indexes
35 def __init__(self, flow_graph, n):
37 Make a new block from nested data.
38 @param flow graph the parent element
39 @param n the nested odict
40 @return block a new block
43 self._doc = n.find('doc') or ''
44 self._imports = map(lambda i: i.strip(), n.findall('import'))
45 self._make = n.find('make')
46 self._var_make = n.find('var_make')
47 self._checks = n.findall('check')
48 self._callbacks = n.findall('callback')
52 flow_graph=flow_graph,
55 _GUIBlock.__init__(self)
60 Call the base class validate.
61 Evaluate the checks: each check must evaluate to True.
65 for check in self._checks:
66 check_res = self.resolve_dependencies(check)
68 check_eval = self.get_parent().evaluate(check_res)
69 try: assert check_eval
70 except AssertionError: self.add_error_message('Check "%s" failed.'%check)
71 except: self.add_error_message('Check "%s" did not evaluate.'%check)
75 Add and remove ports to adjust for the nports.
79 for get_ports, get_port in (
80 (self.get_sources, self.get_source),
81 (self.get_sinks, self.get_sink),
83 #how many streaming (non-message) ports?
84 num_ports = len(filter(lambda p: p.get_type() != 'msg', get_ports()))
85 #do nothing for 0 ports
86 if not num_ports: continue
87 #get the nports setting
88 port0 = get_port(str(0))
89 nports = port0.get_nports()
90 #do nothing for no nports
91 if not nports: continue
92 #do nothing if nports is already num ports
93 if nports == num_ports: continue
94 #remove excess ports and connections
95 if nports < num_ports:
96 #remove the connections
97 for key in map(str, range(nports, num_ports)):
99 for connection in port.get_connections():
100 self.get_parent().remove_element(connection)
102 for key in map(str, range(nports, num_ports)):
103 get_ports().remove(get_port(key))
106 if nports > num_ports:
107 for key in map(str, range(num_ports, nports)):
108 prev_port = get_port(str(int(key)-1))
110 get_ports().index(prev_port)+1,
111 prev_port.copy(new_key=key),
115 def port_controller_modify(self, direction):
117 Change the port controller.
118 @param direction +1 or -1
119 @return true for change
122 #concat the nports string from the private nports settings of both port0
124 (self.get_sinks() and self.get_sinks()[0]._nports or '') + \
125 (self.get_sources() and self.get_sources()[0]._nports or '')
126 #modify all params whose keys appear in the nports string
127 for param in self.get_params():
128 if param.is_enum() or param.get_key() not in nports_str: continue
129 #try to increment the port controller by direction
131 value = param.get_evaluated()
132 value = value + direction
134 param.set_value(value)
140 doc = self._doc.strip('\n').replace('\\\n', '')
141 #merge custom doc with doxygen docs
142 return '\n'.join([doc, extract_docs.extract(self.get_key())]).strip('\n')
144 def get_category(self):
145 category = extract_category.extract(self.get_key())
146 #if category: return category
147 return _Block.get_category(self)
149 def get_imports(self):
151 Resolve all import statements.
152 Split each import statement at newlines.
153 Combine all import statments into a list.
154 Filter empty imports.
155 @return a list of import statements
157 return filter(lambda i: i, sum(map(lambda i: self.resolve_dependencies(i).split('\n'), self._imports), []))
159 def get_make(self): return self.resolve_dependencies(self._make)
160 def get_var_make(self): return self.resolve_dependencies(self._var_make)
162 def get_callbacks(self):
164 Get a list of function callbacks for this block.
165 @return a list of strings
167 def make_callback(callback):
168 callback = self.resolve_dependencies(callback)
169 if 'self.' in callback: return callback
170 return 'self.%s.%s'%(self.get_id(), callback)
171 return map(make_callback, self._callbacks)