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