2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 ##@package grc_gnuradio.Param
20 #Flow graph block parameters.
22 from utils import expr_utils
23 from grc.elements.Param import Param as _Param
29 _hostage_cells = list()
31 ##possible param types
32 TYPES = _Param.TYPES + [
33 'complex', 'real', 'int',
34 'complex_vector', 'real_vector', 'int_vector',
36 'file_open', 'file_save',
43 Get the hide value from the base class.
44 If hide was empty, and this is a type controller, set hide to part.
45 If hide was empty, and this is an id of a non variable, set hide to part.
46 @return hide the hide property string
48 hide = _Param.get_hide(self)
49 #hide IO controlling params
50 if not hide and self.get_key() in (
51 'type', 'vlen', 'num_inputs', 'num_outputs'
53 #hide ID in non variable blocks
54 elif not hide and self.get_key() == 'id' and self.get_parent().get_key() not in (
55 'variable', 'variable_slider', 'variable_chooser', 'variable_text_box', 'parameter', 'options'
62 @return evaluated type
64 self._lisitify_flag = False
65 self._stringify_flag = False
66 self._hostage_cells = list()
69 e = self.get_parent().get_parent().evaluate(v)
70 assert(isinstance(e, str))
73 self._stringify_flag = True
77 #########################
79 #########################
80 if self.is_enum(): return self.get_value()
81 #########################
83 #########################
84 elif t in ('raw', 'complex', 'real', 'int', 'complex_vector', 'real_vector', 'int_vector', 'hex'):
85 #raise exception if python cannot evaluate this value
86 try: e = self.get_parent().get_parent().evaluate(v)
88 self._add_error_message('Value "%s" cannot be evaluated.'%v)
90 #raise an exception if the data is invalid
91 if t == 'raw': return e
93 try: assert(isinstance(e, (complex, float, int, long)))
94 except AssertionError:
95 self._add_error_message('Expression "%s" is invalid for type complex.'%str(e))
99 try: assert(isinstance(e, (float, int, long)))
100 except AssertionError:
101 self._add_error_message('Expression "%s" is invalid for type real.'%str(e))
105 try: assert(isinstance(e, (int, long)))
106 except AssertionError:
107 self._add_error_message('Expression "%s" is invalid for type integer.'%str(e))
110 elif t == 'complex_vector':
111 if not isinstance(e, (tuple, list, set)):
112 self._lisitify_flag = True
116 assert(isinstance(ei, (complex, float, int, long)))
117 except AssertionError:
118 self._add_error_message('Expression "%s" is invalid for type complex vector.'%str(e))
121 elif t == 'real_vector':
122 if not isinstance(e, (tuple, list, set)):
123 self._lisitify_flag = True
127 assert(isinstance(ei, (float, int, long)))
128 except AssertionError:
129 self._add_error_message('Expression "%s" is invalid for type real vector.'%str(e))
132 elif t == 'int_vector':
133 if not isinstance(e, (tuple, list, set)):
134 self._lisitify_flag = True
138 assert(isinstance(ei, (int, long)))
139 except AssertionError:
140 self._add_error_message('Expression "%s" is invalid for type integer vector.'%str(e))
145 else: raise TypeError, 'Type "%s" not handled'%t
146 #########################
148 #########################
149 elif t in ('string', 'file_open', 'file_save'):
150 #do not check if file/directory exists, that is a runtime issue
153 #########################
155 #########################
157 #can python use this as a variable?
160 assert(v[0].isalpha())
161 for c in v: assert(c.isalnum() or c in ('_',))
162 except AssertionError:
163 self._add_error_message('ID "%s" must be alpha-numeric or underscored, and begin with a letter.'%v)
165 params = self.get_all_params('id')
166 keys = [param.get_value() for param in params]
167 try: assert(len(keys) == len(set(keys)))
169 self._add_error_message('ID "%s" is not unique.'%v)
172 #########################
174 #########################
175 elif t == 'grid_pos':
176 if not v: return '' #allow for empty grid pos
177 e = self.get_parent().get_parent().evaluate(v)
179 assert(isinstance(e, (list, tuple)) and len(e) == 4)
180 for ei in e: assert(isinstance(ei, int))
181 except AssertionError:
182 self._add_error_message('A grid position must be a list of 4 integers.')
184 row, col, row_span, col_span = e
186 try: assert(row >= 0 and col >= 0)
187 except AssertionError:
188 self._add_error_message('Row and column must be non-negative.')
190 #check row span, col span
191 try: assert(row_span > 0 and col_span > 0)
192 except AssertionError:
193 self._add_error_message('Row and column span must be greater than zero.')
195 #calculate hostage cells
196 for r in range(row_span):
197 for c in range(col_span):
198 self._hostage_cells.append((row+r, col+c))
200 params = filter(lambda p: p is not self, self.get_all_params('grid_pos'))
202 for cell in param._hostage_cells:
203 if cell in self._hostage_cells:
204 self._add_error_message('Another graphical element is using cell "%s".'%str(cell))
207 #########################
209 #########################
211 n = dict() #new namespace
214 self._add_error_message('Import "%s" failed.'%v)
217 self._add_error_message('Bad import syntax: "%s".'%v)
219 return filter(lambda k: str(k) != '__builtins__', n.keys())
220 #########################
221 else: raise TypeError, 'Type "%s" not handled'%t
225 Convert the value to code.
226 @return a string representing the code
228 #run init tasks in evaluate
234 if t in ('string', 'file_open', 'file_save'): #string types
235 if self._stringify_flag:
236 return '"%s"'%v.replace('"', '\"')
239 elif t in ('complex_vector', 'real_vector', 'int_vector'): #vector types
240 if self._lisitify_flag:
247 def get_all_params(self, type):
249 Get all the params from the flowgraph that have the given type.
250 @param type the specified type
251 @return a list of params
253 return sum([filter(lambda p: p.get_type() == type, block.get_params()) for block in self.get_parent().get_parent().get_blocks()], [])