Statistics
| Branch: | Tag: | Revision:

root / grc / python / Port.py @ de213686

History | View | Annotate | Download (3.8 kB)

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):
29
                """
30
                Make a new port from nested data.
31
                @param block the parent element
32
                @param n the nested odict
33
                """
34
                #build the port
35
                _Port.__init__(
36
                        self,
37
                        block=block,
38
                        n=n,
39
                )
40
                self._nports = n.find('nports') or ''
41
                self._vlen = n.find('vlen') or '1'
42
                self._optional = bool(n.find('optional'))
43
44
        def validate(self):
45
                _Port.validate(self)
46
                try: assert self.get_enabled_connections() or self.get_optional()
47
                except AssertionError: self.add_error_message('Port is not connected.')
48
                try: assert self.is_source() or len(self.get_enabled_connections()) <= 1
49
                except AssertionError: self.add_error_message('Port has too many connections.')
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
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': Constants.COMPLEX_COLOR_SPEC,
89
                                        'float': Constants.FLOAT_COLOR_SPEC,
90
                                        'int': Constants.INT_COLOR_SPEC,
91
                                        'short': Constants.SHORT_COLOR_SPEC,
92
                                        'byte': Constants.BYTE_COLOR_SPEC,
93
                                        'msg': Constants.MSG_COLOR_SPEC,
94
                                }[self.get_type()]
95
                        return {#vlen is non 1
96
                                'complex': Constants.COMPLEX_VECTOR_COLOR_SPEC,
97
                                'float': Constants.FLOAT_VECTOR_COLOR_SPEC,
98
                                'int': Constants.INT_VECTOR_COLOR_SPEC,
99
                                'short': Constants.SHORT_VECTOR_COLOR_SPEC,
100
                                'byte': Constants.BYTE_VECTOR_COLOR_SPEC,
101
                        }[self.get_type()]
102
                except: return _Port.get_color(self)
103
104
class Source(Port):
105
106
        def __init__(self, block, n):
107
                self._n = n #save n
108
                if n['type'] == 'msg': n['key'] = 'msg'
109
                else:
110
                        n['key'] = str(block._source_count)
111
                        block._source_count = block._source_count + 1
112
                Port.__init__(self, block, n)
113
114
class Sink(Port):
115
116
        def __init__(self, block, n):
117
                self._n = n #save n
118
                if n['type'] == 'msg': n['key'] = 'msg'
119
                else:
120
                        n['key'] = str(block._sink_count)
121
                        block._sink_count = block._sink_count + 1
122
                Port.__init__(self, block, n)
123
124
#TODO check that nports and vlen is undefined when type is message
125
#TODO only allow up to one port of type msg