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