Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[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
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 = utils.exists_or_else(n, 'vlen', '1')
36                 nports = utils.exists_or_else(n, 'nports', '')
37                 optional = utils.exists_or_else(n, 'optional', '')
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 get_vlen(self):
49                 """
50                 Get the vector length.
51                 If the evaluation of vlen cannot be cast to an integer, return 1.
52                 @return the vector length or 1
53                 """
54                 vlen = self.get_parent().resolve_dependencies(self._vlen)
55                 try: return int(self.get_parent().get_parent().evaluate(vlen))
56                 except: return 1
57
58         def get_nports(self):
59                 """
60                 Get the number of ports.
61                 If already blank, return a blank
62                 If the evaluation of nports cannot be cast to an integer, return 1.
63                 @return the number of ports or 1
64                 """
65                 nports = self.get_parent().resolve_dependencies(self._nports)
66                 #return blank if nports is blank
67                 if not nports: return ''
68                 try:
69                         nports = int(self.get_parent().get_parent().evaluate(nports))
70                         assert 0 < nports
71                         return nports
72                 except: return 1
73
74         def get_optional(self): return bool(self._optional)
75
76         def get_color(self):
77                 """
78                 Get the color that represents this port's type.
79                 Codes differ for ports where the vec length is 1 or greater than 1.
80                 @return a hex color code.
81                 """
82                 try:
83                         if self.get_vlen() == 1:
84                                 return {#vlen is 1
85                                         'complex': '#3399FF',
86                                         'float': '#FF8C69',
87                                         'int': '#00FF99',
88                                         'short': '#FFFF66',
89                                         'byte': '#FF66FF',
90                                 }[self.get_type()]
91                         return {#vlen is non 1
92                                 'complex': '#3399AA',
93                                 'float': '#CC8C69',
94                                 'int': '#00CC99',
95                                 'short': '#CCCC33',
96                                 'byte': '#CC66CC',
97                         }[self.get_type()]
98                 except: return _Port.get_color(self)
99
100         def is_empty(self):
101                 """
102                 Is this port empty?
103                 An empty port has no connections.
104                 Not empty of optional is set.
105                 @return true if empty
106                 """
107                 return not self.get_optional() and not self.get_connections()
108
109 class Source(Port):
110
111         def __init__(self, block, n):
112                 self._n = n #save n
113                 #key is port index
114                 n['key'] = str(block._source_count)
115                 block._source_count = block._source_count + 1
116                 Port.__init__(self, block, n)
117
118         def __del__(self):
119                 self.get_parent()._source_count = self.get_parent()._source_count - 1
120
121 class Sink(Port):
122
123         def __init__(self, block, n):
124                 self._n = n #save n
125                 #key is port index
126                 n['key'] = str(block._sink_count)
127                 block._sink_count = block._sink_count + 1
128                 Port.__init__(self, block, n)
129
130         def __del__(self):
131                 self.get_parent()._sink_count = self.get_parent()._sink_count - 1