00001 """
00002 Copyright 2008 Free Software Foundation, Inc.
00003 This file is part of GNU Radio
00004
00005 GNU Radio Companion is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009
00010 GNU Radio Companion is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00018 """
00019
00020
00021
00022
00023 from grc import Utils
00024 from grc.elements.Element import Element
00025
00026 class Port(Element):
00027
00028
00029 TYPES = []
00030
00031 def __init__(self, block, n):
00032 """
00033 Make a new port from nested data.
00034 @param block the parent element
00035 @param n the nested odict
00036 @return a new port
00037 """
00038
00039 name = n['name']
00040 key = n['key']
00041 type = n['type']
00042
00043 Element.__init__(self, block)
00044 self._name = name
00045 self._key = key
00046 self._type = type
00047
00048 def validate(self):
00049 """!
00050 Validate the port.
00051 The port must be non-empty and type must a possible type.
00052 """
00053 try: assert(not self.is_empty())
00054 except AssertionError: self._add_error_message('is empty.')
00055 try: assert(self.get_type() in self.TYPES)
00056 except AssertionError: self._add_error_message('Type "%s" is not a possible type.'%self.get_type())
00057
00058 def __str__(self):
00059 if self.is_source():
00060 return 'Source - %s(%s)'%(self.get_name(), self.get_key())
00061 if self.is_sink():
00062 return 'Sink - %s(%s)'%(self.get_name(), self.get_key())
00063
00064 def is_port(self): return True
00065
00066 def get_color(self): return '#FFFFFF'
00067
00068 def get_name(self): return self._name
00069
00070 def get_key(self): return self._key
00071
00072 def is_sink(self): return self in self.get_parent().get_sinks()
00073
00074 def is_source(self): return self in self.get_parent().get_sources()
00075
00076 def get_type(self): return self.get_parent().resolve_dependencies(self._type)
00077
00078 def get_connections(self):
00079 """!
00080 Get all connections that use this port.
00081 @return a list of connection objects
00082 """
00083 connections = self.get_parent().get_parent().get_connections()
00084 connections = filter(lambda c: c.get_source() is self or c.get_sink() is self, connections)
00085 return connections
00086
00087 def is_connected(self):
00088 """!
00089 Is this port connected?
00090 @return true if at least one connection
00091 """
00092 return bool(self.get_connections())
00093
00094 def is_full(self):
00095 """!
00096 Is this port full of connections?
00097 Generally a sink can handle one connection and a source can handle many.
00098 @return true if the port is full
00099 """
00100 if self.is_source(): return False
00101 if self.is_sink(): return bool(self.get_connections())
00102
00103 def is_empty(self):
00104 """!
00105 Is this port empty?
00106 An empty port has no connections.
00107 @return true if empty
00108 """
00109 return not self.get_connections()