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