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