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