grc: better handling of disabled elements
[debian/gnuradio] / grc / src / platforms / python / Block.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
20 from .. base.Block import Block as _Block
21 from utils import extract_docs
22 from ... import utils
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                 doc = utils.exists_or_else(n, 'doc', '')
40                 imports = map(lambda i: i.strip(), utils.listify(n, 'import'))
41                 make = n['make']
42                 checks = utils.listify(n, 'check')
43                 callbacks = utils.listify(n, 'callback')
44                 #build the block
45                 _Block.__init__(
46                         self,
47                         flow_graph=flow_graph,
48                         n=n,
49                 )
50                 self._doc = doc
51                 self._imports = imports
52                 self._make = make
53                 self._callbacks = callbacks
54                 self._checks = checks
55
56         def validate(self):
57                 """
58                 Validate this block.
59                 Call the base class validate.
60                 Evaluate the checks: each check must evaluate to True.
61                 Adjust the nports.
62                 """
63                 _Block.validate(self)
64                 #evaluate the checks
65                 for check in self._checks:
66                         check_res = self.resolve_dependencies(check)
67                         try:
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)
72                 #adjust nports
73                 for ports, Port in (
74                         (self._sources, self.get_parent().get_parent().Source),
75                         (self._sinks, self.get_parent().get_parent().Sink),
76                 ):
77                         #how many ports?
78                         num_ports = len(ports)
79                         #do nothing for 0 ports
80                         if not num_ports: continue
81                         #get the nports setting
82                         port0 = ports[str(0)]
83                         nports = port0.get_nports()
84                         #do nothing for no nports
85                         if not nports: continue
86                         #do nothing if nports is already num ports
87                         if nports == num_ports: continue
88                         #remove excess ports and connections
89                         if nports < num_ports:
90                                 #remove the connections
91                                 for key in map(str, range(nports, num_ports)):
92                                         port = ports[key]
93                                         for connection in port.get_connections():
94                                                 self.get_parent().remove_element(connection)
95                                 #remove the ports
96                                 for key in map(str, range(nports, num_ports)): ports.pop(key)
97                                 continue
98                         #add more ports
99                         if nports > num_ports:
100                                 for key in map(str, range(num_ports, nports)):
101                                         n = port0._n
102                                         n['key'] = key
103                                         port = Port(self, n)
104                                         ports[key] = port
105                                 continue
106
107         def get_doc(self):
108                 doc = self._doc.strip('\n').replace('\\\n', '')
109                 #merge custom doc with doxygen docs
110                 return '\n'.join([doc, extract_docs.extract(self.get_key())]).strip('\n')
111
112         def get_imports(self):
113                 """
114                 Resolve all import statements.
115                 Split each import statement at newlines.
116                 Combine all import statments into a list.
117                 Filter empty imports.
118                 @return a list of import statements
119                 """
120                 return filter(lambda i: i, sum(map(lambda i: self.resolve_dependencies(i).split('\n'), self._imports), []))
121
122         def get_make(self): return self.resolve_dependencies(self._make)
123
124         def get_callbacks(self):
125                 """
126                 Get a list of function callbacks for this block.
127                 @return a list of strings
128                 """
129                 return map(lambda c: self.get_id() + '.' + self.resolve_dependencies(c), self._callbacks)