Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / platforms / base / 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
20 from ... import utils
21 from Element import Element
22
23 class Port(Element):
24
25         ##possible port types
26         TYPES = []
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                 @return a new port
34                 """
35                 #grab the data
36                 name = n['name']
37                 key = n['key']
38                 type = n['type']
39                 #build the port
40                 Element.__init__(self, block)
41                 self._name = name
42                 self._key = key
43                 self._type = type
44
45         def validate(self):
46                 """
47                 Validate the port.
48                 The port must be non-empty and type must a possible type.
49                 """
50                 try: assert(not self.is_empty())
51                 except AssertionError: self._add_error_message('is empty.')
52                 try: assert(self.get_type() in self.TYPES)
53                 except AssertionError: self._add_error_message('Type "%s" is not a possible type.'%self.get_type())
54
55         def __str__(self):
56                 if self.is_source():
57                         return 'Source - %s(%s)'%(self.get_name(), self.get_key())
58                 if self.is_sink():
59                         return 'Sink - %s(%s)'%(self.get_name(), self.get_key())
60
61         def is_port(self): return True
62
63         def get_color(self): return '#FFFFFF'
64
65         def get_name(self): return self._name
66
67         def get_key(self): return self._key
68
69         def is_sink(self): return self in self.get_parent().get_sinks()
70
71         def is_source(self): return self in self.get_parent().get_sources()
72
73         def get_type(self): return self.get_parent().resolve_dependencies(self._type)
74
75         def get_connections(self):
76                 """
77                 Get all connections that use this port.
78                 @return a list of connection objects
79                 """
80                 connections = self.get_parent().get_parent().get_connections()
81                 connections = filter(lambda c: c.get_source() is self or c.get_sink() is self, connections)
82                 return connections
83
84         def is_connected(self):
85                 """
86                 Is this port connected?
87                 @return true if at least one connection
88                 """
89                 return bool(self.get_connections())
90
91         def is_full(self):
92                 """
93                 Is this port full of connections?
94                 Generally a sink can handle one connection and a source can handle many.
95                 @return true if the port is full
96                 """
97                 if self.is_source(): return False
98                 if self.is_sink(): return bool(self.get_connections())
99
100         def is_empty(self):
101                 """
102                 Is this port empty?
103                 An empty port has no connections.
104                 @return true if empty
105                 """
106                 return not self.get_connections()