summaryrefslogtreecommitdiff
path: root/grc/src/platforms/base/Port.py
blob: 672b2e4db5b9a36a14a9bfce2dd48ddb3619c7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Copyright 2008 Free Software Foundation, Inc.
This file is part of GNU Radio

GNU Radio Companion is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

GNU Radio Companion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
"""

from ... import utils
from Element import Element

class Port(Element):

	##possible port types
	TYPES = []

	def __init__(self, block, n):
		"""
		Make a new port from nested data.
		@param block the parent element
		@param n the nested odict
		@return a new port
		"""
		#grab the data
		name = n['name']
		key = n['key']
		type = n['type']
		#build the port
		Element.__init__(self, block)
		self._name = name
		self._key = key
		self._type = type

	def validate(self):
		"""
		Validate the port.
		The port must be non-empty and type must a possible type.
		"""
		try: assert(not self.is_empty())
		except AssertionError: self._add_error_message('Port is not connected.')
		try: assert(self.get_type() in self.TYPES)
		except AssertionError: self._add_error_message('Type "%s" is not a possible type.'%self.get_type())

	def __str__(self):
		if self.is_source():
			return 'Source - %s(%s)'%(self.get_name(), self.get_key())
		if self.is_sink():
			return 'Sink - %s(%s)'%(self.get_name(), self.get_key())

	def is_port(self): return True

	def get_color(self): return '#FFFFFF'

	def get_name(self): return self._name

	def get_key(self): return self._key

	def is_sink(self): return self in self.get_parent().get_sinks()

	def is_source(self): return self in self.get_parent().get_sources()

	def get_type(self): return self.get_parent().resolve_dependencies(self._type)

	def get_connections(self):
		"""
		Get all connections that use this port.
		@return a list of connection objects
		"""
		connections = self.get_parent().get_parent().get_connections()
		connections = filter(lambda c: c.get_source() is self or c.get_sink() is self, connections)
		return connections

	def is_connected(self):
		"""
		Is this port connected?
		@return true if at least one connection
		"""
		return bool(self.get_connections())

	def is_full(self):
		"""
		Is this port full of connections?
		Generally a sink can handle one connection and a source can handle many.
		@return true if the port is full
		"""
		if self.is_source(): return False
		if self.is_sink(): return bool(self.get_connections())

	def is_empty(self):
		"""
		Is this port empty?
		An empty port has no connections.
		@return true if empty
		"""
		return not self.get_connections()