84909e76305055a00c7a9d9f070c7522455e80c5
[debian/gnuradio] / grc / src / grc_gnuradio / 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 ##@package grc_gnuradio.Block
20 #Flow graph block.
21
22 from grc.elements.Block import Block as _Block
23 from grc import Utils
24 from utils import extract_docs
25
26 class Block(_Block):
27
28         ##for make source to keep track of indexes
29         _source_count = 0
30         ##for make sink to keep track of indexes
31         _sink_count = 0
32
33         def __init__(self, flow_graph, n):
34                 """
35                 Make a new block from nested data.
36                 @param flow graph the parent element
37                 @param n the nested odict
38                 @return block a new block
39                 """
40                 #grab the data
41                 doc = Utils.exists_or_else(n, 'doc', '')
42                 imports = map(lambda i: i.strip(), Utils.listify(n, 'import'))
43                 make = n['make']
44                 checks = Utils.listify(n, 'check')
45                 callbacks = Utils.listify(n, 'callback')
46                 #build the block
47                 _Block.__init__(
48                         self,
49                         flow_graph=flow_graph,
50                         n=n,
51                 )
52                 self._doc = doc
53                 self._imports = imports
54                 self._make = make
55                 self._callbacks = callbacks
56                 self._checks = checks
57
58         def validate(self):
59                 """!
60                 Validate this block.
61                 Call the base class validate.
62                 Evaluate the checks: each check must evaluate to True.
63                 Adjust the nports.
64                 """
65                 _Block.validate(self)
66                 #evaluate the checks
67                 for check in self._checks:
68                         check_res = self.resolve_dependencies(check)
69                         try:
70                                 check_eval = self.get_parent().evaluate(check_res)
71                                 try: assert check_eval
72                                 except AssertionError: self._add_error_message('Check "%s" failed.'%check)
73                         except: self._add_error_message('Check "%s" did not evaluate.'%check)
74                 for ports, Port in (
75                         (self._sources, self.get_parent().get_parent().Source),
76                         (self._sinks, self.get_parent().get_parent().Sink),
77                 ):
78                         #how many ports?
79                         num_ports = len(ports)
80                         #do nothing for 0 ports
81                         if not num_ports: continue
82                         #get the nports setting
83                         port0 = ports[str(0)]
84                         nports = port0.get_nports()
85                         #do nothing for no nports
86                         if not nports: continue
87                         #do nothing if nports is already num ports
88                         if nports == num_ports: continue
89                         #remove excess ports and connections
90                         if nports < num_ports:
91                                 #remove the connections
92                                 for key in map(str, range(nports, num_ports)):
93                                         port = ports[key]
94                                         for connection in port.get_connections():
95                                                 self.get_parent().remove_element(connection)
96                                 #remove the ports
97                                 for key in map(str, range(nports, num_ports)): ports.pop(key)
98                                 continue
99                         #add more ports
100                         if nports > num_ports:
101                                 for key in map(str, range(num_ports, nports)):
102                                         n = port0._n
103                                         n['key'] = key
104                                         port = Port(self, n)
105                                         ports[key] = port
106                                 continue
107
108         def get_doc(self):
109                 doc = self._doc.strip('\n').replace('\\\n', '')
110                 #merge custom doc with doxygen docs
111                 return '\n'.join([doc, extract_docs.extract(self.get_key())]).strip('\n')
112
113         def get_imports(self):
114                 """!
115                 Resolve all import statements.
116                 Split each import statement at newlines.
117                 Combine all import statments into a list.
118                 Filter empty imports.
119                 @return a list of import statements
120                 """
121                 return filter(lambda i: i, sum(map(lambda i: self.resolve_dependencies(i).split('\n'), self._imports), []))
122
123         def get_make(self): return self.resolve_dependencies(self._make)
124
125         def get_callbacks(self):
126                 """!
127                 Get a list of function callbacks for this block.
128                 @return a list of strings
129                 """
130                 return map(lambda c: self.get_id() + '.' + self.resolve_dependencies(c), self._callbacks)