Imported Upstream version 3.2.2
[debian/gnuradio] / grc / python / 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 .. base.Block import Block as _Block
21 import extract_docs
22 import extract_category
23
24 class Block(_Block):
25
26         ##for make source to keep track of indexes
27         _source_count = 0
28         ##for make sink to keep track of indexes
29         _sink_count = 0
30
31         def __init__(self, flow_graph, n):
32                 """
33                 Make a new block from nested data.
34                 @param flow graph the parent element
35                 @param n the nested odict
36                 @return block a new block
37                 """
38                 #grab the data
39                 self._doc = n.find('doc') or ''
40                 self._imports = map(lambda i: i.strip(), n.findall('import'))
41                 self._make = n.find('make')
42                 self._var_make = n.find('var_make')
43                 self._checks = n.findall('check')
44                 self._callbacks = n.findall('callback')
45                 #build the block
46                 _Block.__init__(
47                         self,
48                         flow_graph=flow_graph,
49                         n=n,
50                 )
51
52         def validate(self):
53                 """
54                 Validate this block.
55                 Call the base class validate.
56                 Evaluate the checks: each check must evaluate to True.
57                 Adjust the nports.
58                 """
59                 _Block.validate(self)
60                 #evaluate the checks
61                 for check in self._checks:
62                         check_res = self.resolve_dependencies(check)
63                         try:
64                                 check_eval = self.get_parent().evaluate(check_res)
65                                 try: assert check_eval
66                                 except AssertionError: self.add_error_message('Check "%s" failed.'%check)
67                         except: self.add_error_message('Check "%s" did not evaluate.'%check)
68                 #adjust nports
69                 for ports, Port in (
70                         (self._sources, self.get_parent().get_parent().Source),
71                         (self._sinks, self.get_parent().get_parent().Sink),
72                 ):
73                         #how many ports?
74                         num_ports = len(ports)
75                         #do nothing for 0 ports
76                         if not num_ports: continue
77                         #get the nports setting
78                         port0 = ports[str(0)]
79                         nports = port0.get_nports()
80                         #do nothing for no nports
81                         if not nports: continue
82                         #do nothing if nports is already num ports
83                         if nports == num_ports: continue
84                         #remove excess ports and connections
85                         if nports < num_ports:
86                                 #remove the connections
87                                 for key in map(str, range(nports, num_ports)):
88                                         port = ports[key]
89                                         for connection in port.get_connections():
90                                                 self.get_parent().remove_element(connection)
91                                 #remove the ports
92                                 for key in map(str, range(nports, num_ports)): ports.pop(key)
93                                 continue
94                         #add more ports
95                         if nports > num_ports:
96                                 for key in map(str, range(num_ports, nports)):
97                                         n = port0._n
98                                         n['key'] = key
99                                         port = Port(self, n)
100                                         ports[key] = port
101                                 continue
102
103         def port_controller_modify(self, direction):
104                 """
105                 Change the port controller.
106                 @param direction +1 or -1
107                 @return true for change
108                 """
109                 changed = False
110                 #concat the nports string from the private nports settings of both port0
111                 nports_str = \
112                         (self.get_sinks() and self.get_sinks()[0]._nports or '') + \
113                         (self.get_sources() and self.get_sources()[0]._nports or '')
114                 #modify all params whose keys appear in the nports string
115                 for param in self.get_params():
116                         if param.is_enum() or param.get_key() not in nports_str: continue
117                         #try to increment the port controller by direction
118                         try:
119                                 value = param.get_evaluated()
120                                 value = value + direction
121                                 assert 0 < value
122                                 param.set_value(value)
123                                 changed = True
124                         except: pass
125                 return changed
126
127         def get_doc(self):
128                 doc = self._doc.strip('\n').replace('\\\n', '')
129                 #merge custom doc with doxygen docs
130                 return '\n'.join([doc, extract_docs.extract(self.get_key())]).strip('\n')
131
132         def get_category(self):
133                 category = extract_category.extract(self.get_key())
134                 #if category: return category
135                 return _Block.get_category(self)
136
137         def get_imports(self):
138                 """
139                 Resolve all import statements.
140                 Split each import statement at newlines.
141                 Combine all import statments into a list.
142                 Filter empty imports.
143                 @return a list of import statements
144                 """
145                 return filter(lambda i: i, sum(map(lambda i: self.resolve_dependencies(i).split('\n'), self._imports), []))
146
147         def get_make(self): return self.resolve_dependencies(self._make)
148         def get_var_make(self): return self.resolve_dependencies(self._var_make)
149
150         def get_callbacks(self):
151                 """
152                 Get a list of function callbacks for this block.
153                 @return a list of strings
154                 """
155                 def make_callback(callback):
156                         callback = self.resolve_dependencies(callback)
157                         if 'self.' in callback: return callback
158                         return 'self.%s.%s'%(self.get_id(), callback)
159                 return map(make_callback, self._callbacks)