Imported Upstream version 3.2.2
[debian/gnuradio] / grc / base / 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 Element import Element
21
22 class Port(Element):
23
24         ##possible port types
25         TYPES = []
26
27         def __init__(self, block, n):
28                 """
29                 Make a new port from nested data.
30                 @param block the parent element
31                 @param n the nested odict
32                 @return a new port
33                 """
34                 #grab the data
35                 name = n['name']
36                 key = n['key']
37                 type = n['type']
38                 #build the port
39                 Element.__init__(self, block)
40                 self._name = name
41                 self._key = key
42                 self._type = type
43
44         def validate(self):
45                 """
46                 Validate the port.
47                 The port must be non-empty and type must a possible type.
48                 """
49                 Element.validate(self)
50                 try: assert self.get_type() in self.TYPES
51                 except AssertionError: self.add_error_message('Type "%s" is not a possible type.'%self.get_type())
52
53         def __str__(self):
54                 if self.is_source():
55                         return 'Source - %s(%s)'%(self.get_name(), self.get_key())
56                 if self.is_sink():
57                         return 'Sink - %s(%s)'%(self.get_name(), self.get_key())
58
59         def is_port(self): return True
60         def get_color(self): return '#FFFFFF'
61         def get_name(self): return self._name
62         def get_key(self): return self._key
63         def is_sink(self): return self in self.get_parent().get_sinks()
64         def is_source(self): return self in self.get_parent().get_sources()
65         def get_type(self): return self.get_parent().resolve_dependencies(self._type)
66
67         def get_connections(self):
68                 """
69                 Get all connections that use this port.
70                 @return a list of connection objects
71                 """
72                 connections = self.get_parent().get_parent().get_connections()
73                 connections = filter(lambda c: c.get_source() is self or c.get_sink() is self, connections)
74                 return connections
75
76         def get_enabled_connections(self):
77                 """
78                 Get all enabled connections that use this port.
79                 @return a list of connection objects
80                 """
81                 return filter(lambda c: c.get_enabled(), self.get_connections())