mirror of
https://github.com/gnuradio/gnuradio-companion.git
synced 2025-12-10 17:46:12 -06:00
All of the removed `from __future__ import` were needed in older versions of Python (mostly 2.5.x and below) but later became mandatory in most versions of Python 3 hence are not necessary anymore. More specifically, according to __future__.py[1]: - unicode_literals is part of Python since versions 2.6.0 and 3.0.0; - print_function is part of Python since versions 2.6.0 and 3.0.0; - absolute_import is part of Python since versions 2.5.0 and 3.0.0; - division is part of Python since versions 2.2.0 and 3.0.0; Get rid of those unnecessary imports to slightly clean up the codebase. [1] https://github.com/python/cpython/blob/master/Lib/__future__.py
112 lines
3.1 KiB
Python
112 lines
3.1 KiB
Python
"""
|
|
Copyright 2008-2015 Free Software Foundation, Inc.
|
|
This file is part of GNU Radio
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
"""
|
|
|
|
|
|
from .base import Element
|
|
from .utils.descriptors import lazy_property
|
|
|
|
|
|
class Connection(Element):
|
|
|
|
is_connection = True
|
|
|
|
def __init__(self, parent, source, sink):
|
|
"""
|
|
Make a new connection given the parent and 2 ports.
|
|
|
|
Args:
|
|
flow_graph: the parent of this element
|
|
source: a port (any direction)
|
|
sink: a port (any direction)
|
|
@throws Error cannot make connection
|
|
|
|
Returns:
|
|
a new connection
|
|
"""
|
|
Element.__init__(self, parent)
|
|
|
|
if not source.is_source:
|
|
source, sink = sink, source
|
|
if not source.is_source:
|
|
raise ValueError('Connection could not isolate source')
|
|
if not sink.is_sink:
|
|
raise ValueError('Connection could not isolate sink')
|
|
|
|
self.source_port = source
|
|
self.sink_port = sink
|
|
|
|
def __str__(self):
|
|
return 'Connection (\n\t{}\n\t\t{}\n\t{}\n\t\t{}\n)'.format(
|
|
self.source_block, self.source_port, self.sink_block, self.sink_port,
|
|
)
|
|
|
|
def __eq__(self, other):
|
|
if not isinstance(other, self.__class__):
|
|
return NotImplemented
|
|
return self.source_port == other.source_port and self.sink_port == other.sink_port
|
|
|
|
def __hash__(self):
|
|
return hash((self.source_port, self.sink_port))
|
|
|
|
def __iter__(self):
|
|
return iter((self.source_port, self.sink_port))
|
|
|
|
@lazy_property
|
|
def source_block(self):
|
|
return self.source_port.parent_block
|
|
|
|
@lazy_property
|
|
def sink_block(self):
|
|
return self.sink_port.parent_block
|
|
|
|
@lazy_property
|
|
def type(self):
|
|
return self.source_port.domain, self.sink_port.domain
|
|
|
|
@property
|
|
def enabled(self):
|
|
"""
|
|
Get the enabled state of this connection.
|
|
|
|
Returns:
|
|
true if source and sink blocks are enabled
|
|
"""
|
|
return self.source_block.enabled and self.sink_block.enabled
|
|
|
|
def validate(self):
|
|
"""
|
|
Validate the connections.
|
|
The ports must match in io size.
|
|
"""
|
|
Element.validate(self)
|
|
platform = self.parent_platform
|
|
|
|
if self.type not in platform.connection_templates:
|
|
self.add_error_message('No connection known between domains "{}" and "{}"'
|
|
''.format(*self.type))
|
|
|
|
source_size = self.source_port.item_size
|
|
sink_size = self.sink_port.item_size
|
|
if source_size != sink_size:
|
|
self.add_error_message('Source IO size "{}" does not match sink IO size "{}".'.format(source_size, sink_size))
|
|
|
|
##############################################
|
|
# Import/Export Methods
|
|
##############################################
|
|
def export_data(self):
|
|
"""
|
|
Export this connection's info.
|
|
|
|
Returns:
|
|
a nested data odict
|
|
"""
|
|
return (
|
|
self.source_block.name, self.source_port.key,
|
|
self.sink_block.name, self.sink_port.key
|
|
)
|