1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
"""
Copyright 2008, 2009 Free Software Foundation, Inc.
This file is part of GNU Radio
GNU Radio Companion is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
GNU Radio Companion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
from .. base.Port import Port as _Port
import Constants
class Port(_Port):
##possible port types
TYPES = ['complex', 'float', 'int', 'short', 'byte']
def __init__(self, block, n):
"""
Make a new port from nested data.
@param block the parent element
@param n the nested odict
@return a new port
"""
vlen = n.find('vlen') or '1'
nports = n.find('nports') or ''
optional = n.find('optional') or ''
#build the port
_Port.__init__(
self,
block=block,
n=n,
)
self._nports = nports
self._vlen = vlen
self._optional = bool(optional)
def validate(self):
_Port.validate(self)
try: assert self.get_enabled_connections() or self.get_optional()
except AssertionError: self.add_error_message('Port is not connected.')
try: assert self.is_source() or len(self.get_enabled_connections()) <= 1
except AssertionError: self.add_error_message('Port has too many connections.')
def get_vlen(self):
"""
Get the vector length.
If the evaluation of vlen cannot be cast to an integer, return 1.
@return the vector length or 1
"""
vlen = self.get_parent().resolve_dependencies(self._vlen)
try: return int(self.get_parent().get_parent().evaluate(vlen))
except: return 1
def get_nports(self):
"""
Get the number of ports.
If already blank, return a blank
If the evaluation of nports cannot be cast to an integer, return 1.
@return the number of ports or 1
"""
nports = self.get_parent().resolve_dependencies(self._nports)
#return blank if nports is blank
if not nports: return ''
try:
nports = int(self.get_parent().get_parent().evaluate(nports))
assert 0 < nports
return nports
except: return 1
def get_optional(self): return bool(self._optional)
def get_color(self):
"""
Get the color that represents this port's type.
Codes differ for ports where the vec length is 1 or greater than 1.
@return a hex color code.
"""
try:
if self.get_vlen() == 1:
return {#vlen is 1
'complex': Constants.COMPLEX_COLOR_SPEC,
'float': Constants.FLOAT_COLOR_SPEC,
'int': Constants.INT_COLOR_SPEC,
'short': Constants.SHORT_COLOR_SPEC,
'byte': Constants.BYTE_COLOR_SPEC,
}[self.get_type()]
return {#vlen is non 1
'complex': Constants.COMPLEX_VECTOR_COLOR_SPEC,
'float': Constants.FLOAT_VECTOR_COLOR_SPEC,
'int': Constants.INT_VECTOR_COLOR_SPEC,
'short': Constants.SHORT_VECTOR_COLOR_SPEC,
'byte': Constants.BYTE_VECTOR_COLOR_SPEC,
}[self.get_type()]
except: return _Port.get_color(self)
class Source(Port):
def __init__(self, block, n):
self._n = n #save n
#key is port index
n['key'] = str(block._source_count)
block._source_count = block._source_count + 1
Port.__init__(self, block, n)
def __del__(self):
self.get_parent()._source_count = self.get_parent()._source_count - 1
class Sink(Port):
def __init__(self, block, n):
self._n = n #save n
#key is port index
n['key'] = str(block._sink_count)
block._sink_count = block._sink_count + 1
Port.__init__(self, block, n)
def __del__(self):
self.get_parent()._sink_count = self.get_parent()._sink_count - 1
|