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.Port import Port as _Port
21 from .. gui.Port import Port as _GUIPort
24 def _get_source_from_virtual_sink_port(vsp):
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.
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
33 def _get_source_from_virtual_source_port(vsp, traversed=[]):
35 Recursively resolve source ports over the virtual connections.
36 Keep track of traversed sources to avoid recursive loops.
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(),
51 except: raise Exception, 'Could not resolve source for virtual source port %s'%vsp
53 class Port(_Port, _GUIPort):
55 def __init__(self, block, n, dir):
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
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
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'))
82 def get_types(self): return ('complex', 'float', 'int', 'short', 'byte', 'msg', '')
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.')
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.')
99 Handle the port cloning for virtual blocks.
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
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)
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
121 vlen = self.get_parent().resolve_dependencies(self._vlen)
122 try: return int(self.get_parent().get_parent().evaluate(vlen))
125 def get_nports(self):
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
132 nports = self.get_parent().resolve_dependencies(self._nports)
133 #return blank if nports is blank
134 if not nports: return ''
136 nports = int(self.get_parent().get_parent().evaluate(nports))
141 def get_optional(self): return bool(self._optional)
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.
150 if self.get_vlen() == 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,
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,
166 except: return _Port.get_color(self)
168 def copy(self, new_key=None):
170 if new_key: n['key'] = new_key
171 return self.__class__(self.get_parent(), n, self._dir)