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