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.elements.Port import Port as _Port
00024 from grc import Utils
00025 from grc.Constants import MAX_NUM_PORTS
00026
00027 class Port(_Port):
00028
00029
00030 TYPES = ['complex', 'float', 'int', 'short', 'byte']
00031
00032 def __init__(self, block, n):
00033 """
00034 Make a new port from nested data.
00035 @param block the parent element
00036 @param n the nested odict
00037 @return a new port
00038 """
00039 vlen = Utils.exists_or_else(n, 'vlen', '1')
00040 nports = Utils.exists_or_else(n, 'nports', '')
00041 optional = Utils.exists_or_else(n, 'optional', '')
00042
00043 _Port.__init__(
00044 self,
00045 block=block,
00046 n=n,
00047 )
00048 self._nports = nports
00049 self._vlen = vlen
00050 self._optional = bool(optional)
00051
00052 def get_vlen(self):
00053 """
00054 Get the vector length.
00055 If the evaluation of vlen cannot be cast to an integer, return 1.
00056 @return the vector length or 1
00057 """
00058 vlen = self.get_parent().resolve_dependencies(self._vlen)
00059 try: return int(self.get_parent().get_parent().evaluate(vlen))
00060 except: return 1
00061
00062 def get_nports(self):
00063 """
00064 Get the number of ports.
00065 If already blank, return a blank
00066 If the evaluation of nports cannot be cast to an integer, return 1.
00067 @return the number of ports or 1
00068 """
00069 nports = self.get_parent().resolve_dependencies(self._nports)
00070
00071 if not nports: return ''
00072 try:
00073 nports = int(self.get_parent().get_parent().evaluate(nports))
00074 assert(0 < nports <= MAX_NUM_PORTS)
00075 return nports
00076 except: return 1
00077
00078 def get_optional(self): return bool(self._optional)
00079
00080 def get_color(self):
00081 """
00082 Get the color that represents this port's type.
00083 Codes differ for ports where the vec length is 1 or greater than 1.
00084 @return a hex color code.
00085 """
00086 try:
00087 if self.get_vlen() == 1:
00088 return {
00089 'complex': '#3399FF',
00090 'float': '#FF8C69',
00091 'int': '#00FF99',
00092 'short': '#FFFF66',
00093 'byte': '#FF66FF',
00094 }[self.get_type()]
00095 return {
00096 'complex': '#3399AA',
00097 'float': '#CC8C69',
00098 'int': '#00CC99',
00099 'short': '#CCCC33',
00100 'byte': '#CC66CC',
00101 }[self.get_type()]
00102 except: return _Port.get_color(self)
00103
00104 def is_empty(self):
00105 """!
00106 Is this port empty?
00107 An empty port has no connections.
00108 Not empty of optional is set.
00109 @return true if empty
00110 """
00111 return not self.get_optional() and not self.get_connections()
00112
00113 class Source(Port):
00114
00115 def __init__(self, block, n):
00116 self._n = n
00117
00118 n['key'] = str(block._source_count)
00119 block._source_count = block._source_count + 1
00120 Port.__init__(self, block, n)
00121
00122 def __del__(self):
00123 self.get_parent()._source_count = self.get_parent()._source_count - 1
00124
00125 class Sink(Port):
00126
00127 def __init__(self, block, n):
00128 self._n = n
00129
00130 n['key'] = str(block._sink_count)
00131 block._sink_count = block._sink_count + 1
00132 Port.__init__(self, block, n)
00133
00134 def __del__(self):
00135 self.get_parent()._sink_count = self.get_parent()._sink_count - 1