Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / platforms / base / Connection.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 Element import Element
21 from ... utils import odict
22
23 class Connection(Element):
24
25         def __init__(self, flow_graph, porta, portb):
26                 """
27                 Make a new connection given the parent and 2 ports.
28                 @param flow_graph the parent of this element
29                 @param porta a port (any direction)
30                 @param portb a port (any direction)
31                 @throws Error cannot make connection
32                 @return a new connection
33                 """
34                 Element.__init__(self, flow_graph)
35                 source = sink = None
36                 #separate the source and sink
37                 for port in (porta, portb):
38                         if port.is_source(): source = port
39                         if port.is_sink(): sink = port
40                 #verify the source and sink
41                 assert(source and sink)
42                 assert(not source.is_full())
43                 assert(not sink.is_full())
44                 self._source = source
45                 self._sink = sink
46
47         def __str__(self): return 'Connection (%s -> %s)'%(self.get_source(), self.get_sink())
48
49         def is_connection(self): return True
50
51         def validate(self):
52                 """
53                 Validate the connections.
54                 The ports must match in type.
55                 """
56                 source_type = self.get_source().get_type()
57                 sink_type = self.get_sink().get_type()
58                 try: assert source_type == sink_type
59                 except AssertionError: self._add_error_message('Source type "%s" does not match sink type "%s".'%(source_type, sink_type))
60
61         def get_enabled(self):
62                 """
63                 Get the enabled state of this connection.
64                 @return true if source and sink blocks are enabled
65                 """
66                 return self.get_source().get_parent().get_enabled() and \
67                         self.get_sink().get_parent().get_enabled()
68
69         #############################
70         # Access Ports
71         #############################
72         def get_sink(self): return self._sink
73         def get_source(self): return self._source
74
75         ##############################################
76         ## Import/Export Methods
77         ##############################################
78         def export_data(self):
79                 """
80                 Export this connection's info.
81                 @return a nested data odict
82                 """
83                 n = odict()
84                 n['source_block_id'] = self.get_source().get_parent().get_id()
85                 n['sink_block_id'] = self.get_sink().get_parent().get_id()
86                 n['source_key'] = self.get_source().get_key()
87                 n['sink_key'] = self.get_sink().get_key()
88                 return n