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