switch source package format to 3.0 quilt
[debian/gnuradio] / grc / python / Port.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.Port import Port as _Port
21 from .. gui.Port import Port as _GUIPort
22 import Constants
23
24 def _get_source_from_virtual_sink_port(vsp):
25         """
26         Resolve the source port that is connected to the given virtual sink port.
27         Use the get source from virtual source to recursively resolve subsequent ports. 
28         """
29         try: return _get_source_from_virtual_source_port(
30                 vsp.get_enabled_connections()[0].get_source())
31         except: raise Exception, 'Could not resolve source for virtual sink port %s'%vsp
32
33 def _get_source_from_virtual_source_port(vsp, traversed=[]):
34         """
35         Recursively resolve source ports over the virtual connections.
36         Keep track of traversed sources to avoid recursive loops.
37         """
38         if not vsp.get_parent().is_virtual_source(): return vsp
39         if vsp in traversed: raise Exception, 'Loop found when resolving virtual source %s'%vsp
40         try: return _get_source_from_virtual_source_port(
41                 _get_source_from_virtual_sink_port(
42                         filter(#get all virtual sinks with a matching stream id
43                                 lambda vs: vs.get_param('stream_id').get_value() == vsp.get_parent().get_param('stream_id').get_value(),
44                                 filter(#get all enabled blocks that are also virtual sinks
45                                         lambda b: b.is_virtual_sink(),
46                                         vsp.get_parent().get_parent().get_enabled_blocks(),
47                                 ),
48                         )[0].get_sinks()[0]
49                 ), traversed + [vsp],
50         )
51         except: raise Exception, 'Could not resolve source for virtual source port %s'%vsp
52
53 class Port(_Port, _GUIPort):
54
55         def __init__(self, block, n, dir):
56                 """
57                 Make a new port from nested data.
58                 @param block the parent element
59                 @param n the nested odict
60                 @param dir the direction
61                 """
62                 self._n = n
63                 if n['type'] == 'msg': n['key'] = 'msg'
64                 if dir == 'source' and not n.find('key'):
65                         n['key'] = str(block._source_count)
66                         block._source_count += 1
67                 if dir == 'sink' and not n.find('key'):
68                         n['key'] = str(block._sink_count)
69                         block._sink_count += 1
70                 #build the port
71                 _Port.__init__(
72                         self,
73                         block=block,
74                         n=n,
75                         dir=dir,
76                 )
77                 _GUIPort.__init__(self)
78                 self._nports = n.find('nports') or ''
79                 self._vlen = n.find('vlen') or ''
80                 self._optional = bool(n.find('optional'))
81
82         def get_types(self): return ('complex', 'float', 'int', 'short', 'byte', 'msg', '')
83
84         def validate(self):
85                 _Port.validate(self)
86                 try: assert self.get_enabled_connections() or self.get_optional()
87                 except AssertionError: self.add_error_message('Port is not connected.')
88                 try: assert self.is_source() or len(self.get_enabled_connections()) <= 1
89                 except AssertionError: self.add_error_message('Port has too many connections.')
90                 #message port logic
91                 if self.get_type() == 'msg':
92                         try: assert not self.get_nports()
93                         except AssertionError: self.add_error_message('A port of type "msg" cannot have "nports" set.')
94                         try: assert self.get_vlen() == 1
95                         except AssertionError: self.add_error_message('A port of type "msg" must have a "vlen" of 1.')
96
97         def rewrite(self):
98                 """
99                 Handle the port cloning for virtual blocks.
100                 """
101                 _Port.rewrite(self)
102                 if self.get_parent().is_virtual_sink() or self.get_parent().is_virtual_source():
103                         try: #clone type and vlen
104                                 source = self.resolve_virtual_source()
105                                 self._type = str(source.get_type())
106                                 self._vlen = str(source.get_vlen())
107                         except: #reset type and vlen
108                                 self._type = ''
109                                 self._vlen = ''
110
111         def resolve_virtual_source(self):
112                 if self.get_parent().is_virtual_sink(): return _get_source_from_virtual_sink_port(self)
113                 if self.get_parent().is_virtual_source(): return _get_source_from_virtual_source_port(self)
114
115         def get_vlen(self):
116                 """
117                 Get the vector length.
118                 If the evaluation of vlen cannot be cast to an integer, return 1.
119                 @return the vector length or 1
120                 """
121                 vlen = self.get_parent().resolve_dependencies(self._vlen)
122                 try: return int(self.get_parent().get_parent().evaluate(vlen))
123                 except: return 1
124
125         def get_nports(self):
126                 """
127                 Get the number of ports.
128                 If already blank, return a blank
129                 If the evaluation of nports cannot be cast to an integer, return 1.
130                 @return the number of ports or 1
131                 """
132                 nports = self.get_parent().resolve_dependencies(self._nports)
133                 #return blank if nports is blank
134                 if not nports: return ''
135                 try:
136                         nports = int(self.get_parent().get_parent().evaluate(nports))
137                         assert 0 < nports
138                         return nports
139                 except: return 1
140
141         def get_optional(self): return bool(self._optional)
142
143         def get_color(self):
144                 """
145                 Get the color that represents this port's type.
146                 Codes differ for ports where the vec length is 1 or greater than 1.
147                 @return a hex color code.
148                 """
149                 try:
150                         if self.get_vlen() == 1:
151                                 return {#vlen is 1
152                                         'complex': Constants.COMPLEX_COLOR_SPEC,
153                                         'float': Constants.FLOAT_COLOR_SPEC,
154                                         'int': Constants.INT_COLOR_SPEC,
155                                         'short': Constants.SHORT_COLOR_SPEC,
156                                         'byte': Constants.BYTE_COLOR_SPEC,
157                                         'msg': Constants.MSG_COLOR_SPEC,
158                                 }[self.get_type()]
159                         return {#vlen is non 1
160                                 'complex': Constants.COMPLEX_VECTOR_COLOR_SPEC,
161                                 'float': Constants.FLOAT_VECTOR_COLOR_SPEC,
162                                 'int': Constants.INT_VECTOR_COLOR_SPEC,
163                                 'short': Constants.SHORT_VECTOR_COLOR_SPEC,
164                                 'byte': Constants.BYTE_VECTOR_COLOR_SPEC,
165                         }[self.get_type()]
166                 except: return _Port.get_color(self)
167
168         def copy(self, new_key=None):
169                 n = self._n.copy()
170                 if new_key: n['key'] = new_key
171                 return self.__class__(self.get_parent(), n, self._dir)