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
26 TYPES = ['complex', 'float', 'int', 'short', 'byte', 'msg']
28 def __init__(self, block, n, dir):
30 Make a new port from nested data.
31 @param block the parent element
32 @param n the nested odict
33 @param dir the direction
36 if n['type'] == 'msg': n['key'] = 'msg'
37 if dir == 'source' and not n.find('key'):
38 n['key'] = str(block._source_count)
39 block._source_count += 1
40 if dir == 'sink' and not n.find('key'):
41 n['key'] = str(block._sink_count)
42 block._sink_count += 1
50 self._nports = n.find('nports') or ''
51 self._vlen = n.find('vlen') or ''
52 self._optional = bool(n.find('optional'))
56 try: assert self.get_enabled_connections() or self.get_optional()
57 except AssertionError: self.add_error_message('Port is not connected.')
58 try: assert self.is_source() or len(self.get_enabled_connections()) <= 1
59 except AssertionError: self.add_error_message('Port has too many connections.')
61 if self.get_type() == 'msg':
62 try: assert not self.get_nports()
63 except AssertionError: self.add_error_message('A port of type "msg" cannot have "nports" set.')
64 try: assert self.get_vlen() == 1
65 except AssertionError: self.add_error_message('A port of type "msg" must have a "vlen" of 1.')
69 Handle the port cloning for virtual blocks.
72 if self.get_parent().get_key() in ('virtual_sink', 'virtual_source'):
74 if self.get_parent().get_key() == 'virtual_sink':
75 source = self.get_enabled_connections()[0].get_source()
76 if self.get_parent().get_key() == 'virtual_source':
78 lambda vs: vs.get_param('stream_id').get_value() == self.get_parent().get_param('stream_id').get_value(),
80 lambda b: b.get_key() == 'virtual_sink',
81 self.get_parent().get_parent().get_enabled_blocks(),
83 )[0].get_sink('0').get_enabled_connections()[0].get_source()
85 self._type = str(source.get_type())
86 self._vlen = str(source.get_vlen())
94 Get the vector length.
95 If the evaluation of vlen cannot be cast to an integer, return 1.
96 @return the vector length or 1
98 vlen = self.get_parent().resolve_dependencies(self._vlen)
99 try: return int(self.get_parent().get_parent().evaluate(vlen))
102 def get_nports(self):
104 Get the number of ports.
105 If already blank, return a blank
106 If the evaluation of nports cannot be cast to an integer, return 1.
107 @return the number of ports or 1
109 nports = self.get_parent().resolve_dependencies(self._nports)
110 #return blank if nports is blank
111 if not nports: return ''
113 nports = int(self.get_parent().get_parent().evaluate(nports))
118 def get_optional(self): return bool(self._optional)
122 Get the color that represents this port's type.
123 Codes differ for ports where the vec length is 1 or greater than 1.
124 @return a hex color code.
127 if self.get_vlen() == 1:
129 'complex': Constants.COMPLEX_COLOR_SPEC,
130 'float': Constants.FLOAT_COLOR_SPEC,
131 'int': Constants.INT_COLOR_SPEC,
132 'short': Constants.SHORT_COLOR_SPEC,
133 'byte': Constants.BYTE_COLOR_SPEC,
134 'msg': Constants.MSG_COLOR_SPEC,
136 return {#vlen is non 1
137 'complex': Constants.COMPLEX_VECTOR_COLOR_SPEC,
138 'float': Constants.FLOAT_VECTOR_COLOR_SPEC,
139 'int': Constants.INT_VECTOR_COLOR_SPEC,
140 'short': Constants.SHORT_VECTOR_COLOR_SPEC,
141 'byte': Constants.BYTE_VECTOR_COLOR_SPEC,
143 except: return _Port.get_color(self)
145 def copy(self, new_key=None):
147 if new_key: n['key'] = new_key
148 return self.__class__(self.get_parent(), n, self._dir)