summaryrefslogtreecommitdiff
path: root/grc/base/Connection.py
diff options
context:
space:
mode:
Diffstat (limited to 'grc/base/Connection.py')
-rw-r--r--grc/base/Connection.py183
1 files changed, 89 insertions, 94 deletions
diff --git a/grc/base/Connection.py b/grc/base/Connection.py
index 654eedb357..252b7deac1 100644
--- a/grc/base/Connection.py
+++ b/grc/base/Connection.py
@@ -22,104 +22,99 @@ from . import odict
class Connection(Element):
- def __init__(self, flow_graph, porta, portb):
- """
- Make a new connection given the parent and 2 ports.
-
- Args:
- flow_graph: the parent of this element
- porta: a port (any direction)
- portb: a port (any direction)
- @throws Error cannot make connection
-
- Returns:
- a new connection
- """
- Element.__init__(self, flow_graph)
- source = sink = None
- #separate the source and sink
- for port in (porta, portb):
- if port.is_source(): source = port
- if port.is_sink(): sink = port
- if not source: raise ValueError('Connection could not isolate source')
- if not sink: raise ValueError('Connection could not isolate sink')
+ def __init__(self, flow_graph, porta, portb):
+ """
+ Make a new connection given the parent and 2 ports.
+
+ Args:
+ flow_graph: the parent of this element
+ porta: a port (any direction)
+ portb: a port (any direction)
+ @throws Error cannot make connection
+
+ Returns:
+ a new connection
+ """
+ Element.__init__(self, flow_graph)
+ source = sink = None
+ #separate the source and sink
+ for port in (porta, portb):
+ if port.is_source(): source = port
+ if port.is_sink(): sink = port
+ if not source: raise ValueError('Connection could not isolate source')
+ if not sink: raise ValueError('Connection could not isolate sink')
+ busses = len(filter(lambda a: a.get_type() == 'bus', [source, sink]))%2
+ if not busses == 0: raise ValueError('busses must get with busses')
- busses = len(filter(lambda a: a.get_type() == 'bus', [source, sink]))%2
- if not busses == 0: raise ValueError('busses must get with busses')
+ if not len(source.get_associated_ports()) == len(sink.get_associated_ports()):
+ raise ValueError('port connections must have same cardinality');
+ #ensure that this connection (source -> sink) is unique
+ for connection in self.get_parent().get_connections():
+ if connection.get_source() is source and connection.get_sink() is sink:
+ raise Exception('This connection between source and sink is not unique.')
+ self._source = source
+ self._sink = sink
+ if source.get_type() == 'bus':
+
+ sources = source.get_associated_ports();
+ sinks = sink.get_associated_ports();
+
+ for i in range(len(sources)):
+ try:
+ flow_graph.connect(sources[i], sinks[i]);
+ except:
+ pass
- if not len(source.get_associated_ports()) == len(sink.get_associated_ports()):
- raise ValueError('port connections must have same cardinality');
- #ensure that this connection (source -> sink) is unique
- for connection in self.get_parent().get_connections():
- if connection.get_source() is source and connection.get_sink() is sink:
- raise Exception('This connection between source and sink is not unique.')
- self._source = source
- self._sink = sink
-
- if source.get_type() == 'bus':
-
- sources = source.get_associated_ports();
- sinks = sink.get_associated_ports();
-
- for i in range(len(sources)):
- try:
- flow_graph.connect(sources[i], sinks[i]);
- except:
- pass
-
-
- def __str__(self):
- return 'Connection (\n\t%s\n\t\t%s\n\t%s\n\t\t%s\n)'%(
- self.get_source().get_parent(),
- self.get_source(),
- self.get_sink().get_parent(),
- self.get_sink(),
- )
+ def __str__(self):
+ return 'Connection (\n\t%s\n\t\t%s\n\t%s\n\t\t%s\n)'%(
+ self.get_source().get_parent(),
+ self.get_source(),
+ self.get_sink().get_parent(),
+ self.get_sink(),
+ )
- def is_connection(self): return True
+ def is_connection(self): return True
- def validate(self):
- """
- Validate the connections.
- The ports must match in type.
- """
- Element.validate(self)
- source_type = self.get_source().get_type()
- sink_type = self.get_sink().get_type()
- if source_type != sink_type:
- self.add_error_message('Source type "%s" does not match sink type "%s".'%(source_type, sink_type))
+ def validate(self):
+ """
+ Validate the connections.
+ The ports must match in type.
+ """
+ Element.validate(self)
+ source_type = self.get_source().get_type()
+ sink_type = self.get_sink().get_type()
+ if source_type != sink_type:
+ self.add_error_message('Source type "%s" does not match sink type "%s".'%(source_type, sink_type))
-
+ def get_enabled(self):
+ """
+ Get the enabled state of this connection.
+
+ Returns:
+ true if source and sink blocks are enabled
+ """
+ return self.get_source().get_parent().get_enabled() and \
+ self.get_sink().get_parent().get_enabled()
- def get_enabled(self):
- """
- Get the enabled state of this connection.
-
- Returns:
- true if source and sink blocks are enabled
- """
- return self.get_source().get_parent().get_enabled() and \
- self.get_sink().get_parent().get_enabled()
+ #############################
+ # Access Ports
+ #############################
+ def get_sink(self): return self._sink
+ def get_source(self): return self._source
- #############################
- # Access Ports
- #############################
- def get_sink(self): return self._sink
- def get_source(self): return self._source
-
- ##############################################
- ## Import/Export Methods
- ##############################################
- def export_data(self):
- """
- Export this connection's info.
-
- Returns:
- a nested data odict
- """
- n = odict()
- n['source_block_id'] = self.get_source().get_parent().get_id()
- n['sink_block_id'] = self.get_sink().get_parent().get_id()
- n['source_key'] = self.get_source().get_key()
- n['sink_key'] = self.get_sink().get_key()
- return n
+ ##############################################
+ ## Import/Export Methods
+ ##############################################
+ def export_data(self):
+ """
+ Export this connection's info.
+
+ Returns:
+ a nested data odict
+ """
+ n = odict()
+ n['source_block_id'] = self.get_source().get_parent().get_id()
+ n['sink_block_id'] = self.get_sink().get_parent().get_id()
+ n['source_key'] = self.get_source().get_key()
+ n['sink_key'] = self.get_sink().get_key()
+ return n