c8e899781d184ca67cd724d15896f4680df22a5d
[debian/gnuradio] / grc / src / grc_gnuradio / 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 ##@package grc_gnuradio.Port
20 #Flow graph block port (source or sink).
21 #@author Josh Blum
22
23 from grc.elements.Port import Port as _Port
24 from grc import Utils
25 from grc.Constants import MAX_NUM_PORTS
26
27 class Port(_Port):
28
29         ##possible port types
30         TYPES = ['complex', 'float', 'int', 'short', 'byte']
31
32         def __init__(self, block, n):
33                 """
34                 Make a new port from nested data.
35                 @param block the parent element
36                 @param n the nested odict
37                 @return a new port
38                 """
39                 vlen = Utils.exists_or_else(n, 'vlen', '1')
40                 nports = Utils.exists_or_else(n, 'nports', '')
41                 optional = Utils.exists_or_else(n, 'optional', '')
42                 #build the port
43                 _Port.__init__(
44                         self,
45                         block=block,
46                         n=n,
47                 )
48                 self._nports = nports
49                 self._vlen = vlen
50                 self._optional = bool(optional)
51
52         def get_vlen(self):
53                 """
54                 Get the vector length.
55                 If the evaluation of vlen cannot be cast to an integer, return 1.
56                 @return the vector length or 1
57                 """
58                 vlen = self.get_parent().resolve_dependencies(self._vlen)
59                 try: return int(self.get_parent().get_parent().evaluate(vlen))
60                 except: return 1
61
62         def get_nports(self):
63                 """
64                 Get the number of ports.
65                 If already blank, return a blank
66                 If the evaluation of nports cannot be cast to an integer, return 1.
67                 @return the number of ports or 1
68                 """
69                 nports = self.get_parent().resolve_dependencies(self._nports)
70                 #return blank if nports is blank
71                 if not nports: return ''
72                 try:
73                         nports = int(self.get_parent().get_parent().evaluate(nports))
74                         assert(0 < nports <= MAX_NUM_PORTS)
75                         return nports
76                 except: return 1
77
78         def get_optional(self): return bool(self._optional)
79
80         def get_color(self):
81                 """
82                 Get the color that represents this port's type.
83                 Codes differ for ports where the vec length is 1 or greater than 1.
84                 @return a hex color code.
85                 """
86                 try:
87                         if self.get_vlen() == 1:
88                                 return {#vlen is 1
89                                         'complex': '#3399FF',
90                                         'float': '#FF8C69',
91                                         'int': '#00FF99',
92                                         'short': '#FFFF66',
93                                         'byte': '#FF66FF',
94                                 }[self.get_type()]
95                         return {#vlen is non 1
96                                 'complex': '#3399AA',
97                                 'float': '#CC8C69',
98                                 'int': '#00CC99',
99                                 'short': '#CCCC33',
100                                 'byte': '#CC66CC',
101                         }[self.get_type()]
102                 except: return _Port.get_color(self)
103
104         def is_empty(self):
105                 """!
106                 Is this port empty?
107                 An empty port has no connections.
108                 Not empty of optional is set.
109                 @return true if empty
110                 """
111                 return not self.get_optional() and not self.get_connections()
112
113 class Source(Port):
114
115         def __init__(self, block, n):
116                 self._n = n #save n
117                 #key is port index
118                 n['key'] = str(block._source_count)
119                 block._source_count = block._source_count + 1
120                 Port.__init__(self, block, n)
121
122         def __del__(self):
123                 self.get_parent()._source_count = self.get_parent()._source_count - 1
124
125 class Sink(Port):
126
127         def __init__(self, block, n):
128                 self._n = n #save n
129                 #key is port index
130                 n['key'] = str(block._sink_count)
131                 block._sink_count = block._sink_count + 1
132                 Port.__init__(self, block, n)
133
134         def __del__(self):
135                 self.get_parent()._sink_count = self.get_parent()._sink_count - 1