Imported Upstream version 3.2.2
[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 import Constants
22
23 class Port(_Port):
24
25         ##possible port types
26         TYPES = ['complex', 'float', 'int', 'short', 'byte']
27
28         def __init__(self, block, n):
29                 """
30                 Make a new port from nested data.
31                 @param block the parent element
32                 @param n the nested odict
33                 @return a new port
34                 """
35                 vlen = n.find('vlen') or '1'
36                 nports = n.find('nports') or ''
37                 optional = n.find('optional') or ''
38                 #build the port
39                 _Port.__init__(
40                         self,
41                         block=block,
42                         n=n,
43                 )
44                 self._nports = nports
45                 self._vlen = vlen
46                 self._optional = bool(optional)
47
48         def validate(self):
49                 _Port.validate(self)
50                 try: assert self.get_enabled_connections() or self.get_optional()
51                 except AssertionError: self.add_error_message('Port is not connected.')
52                 try: assert self.is_source() or len(self.get_enabled_connections()) <= 1
53                 except AssertionError: self.add_error_message('Port has too many connections.')
54
55         def get_vlen(self):
56                 """
57                 Get the vector length.
58                 If the evaluation of vlen cannot be cast to an integer, return 1.
59                 @return the vector length or 1
60                 """
61                 vlen = self.get_parent().resolve_dependencies(self._vlen)
62                 try: return int(self.get_parent().get_parent().evaluate(vlen))
63                 except: return 1
64
65         def get_nports(self):
66                 """
67                 Get the number of ports.
68                 If already blank, return a blank
69                 If the evaluation of nports cannot be cast to an integer, return 1.
70                 @return the number of ports or 1
71                 """
72                 nports = self.get_parent().resolve_dependencies(self._nports)
73                 #return blank if nports is blank
74                 if not nports: return ''
75                 try:
76                         nports = int(self.get_parent().get_parent().evaluate(nports))
77                         assert 0 < nports
78                         return nports
79                 except: return 1
80
81         def get_optional(self): return bool(self._optional)
82
83         def get_color(self):
84                 """
85                 Get the color that represents this port's type.
86                 Codes differ for ports where the vec length is 1 or greater than 1.
87                 @return a hex color code.
88                 """
89                 try:
90                         if self.get_vlen() == 1:
91                                 return {#vlen is 1
92                                         'complex': Constants.COMPLEX_COLOR_SPEC,
93                                         'float': Constants.FLOAT_COLOR_SPEC,
94                                         'int': Constants.INT_COLOR_SPEC,
95                                         'short': Constants.SHORT_COLOR_SPEC,
96                                         'byte': Constants.BYTE_COLOR_SPEC,
97                                 }[self.get_type()]
98                         return {#vlen is non 1
99                                 'complex': Constants.COMPLEX_VECTOR_COLOR_SPEC,
100                                 'float': Constants.FLOAT_VECTOR_COLOR_SPEC,
101                                 'int': Constants.INT_VECTOR_COLOR_SPEC,
102                                 'short': Constants.SHORT_VECTOR_COLOR_SPEC,
103                                 'byte': Constants.BYTE_VECTOR_COLOR_SPEC,
104                         }[self.get_type()]
105                 except: return _Port.get_color(self)
106
107 class Source(Port):
108
109         def __init__(self, block, n):
110                 self._n = n #save n
111                 #key is port index
112                 n['key'] = str(block._source_count)
113                 block._source_count = block._source_count + 1
114                 Port.__init__(self, block, n)
115
116         def __del__(self):
117                 self.get_parent()._source_count = self.get_parent()._source_count - 1
118
119 class Sink(Port):
120
121         def __init__(self, block, n):
122                 self._n = n #save n
123                 #key is port index
124                 n['key'] = str(block._sink_count)
125                 block._sink_count = block._sink_count + 1
126                 Port.__init__(self, block, n)
127
128         def __del__(self):
129                 self.get_parent()._sink_count = self.get_parent()._sink_count - 1