[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] r7715 - grc/branches/grc_reloaded/src/grc/platforms/gn
From: |
jblum |
Subject: |
[Commit-gnuradio] r7715 - grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python |
Date: |
Fri, 15 Feb 2008 18:25:37 -0700 (MST) |
Author: jblum
Date: 2008-02-15 18:25:37 -0700 (Fri, 15 Feb 2008)
New Revision: 7715
Added:
grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py
Log:
forgot the gnuradio python param
Added: grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py
===================================================================
--- grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py
(rev 0)
+++ grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python/Param.py
2008-02-16 01:25:37 UTC (rev 7715)
@@ -0,0 +1,216 @@
+"""
+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
+"""
address@hidden grc.platforms.gnuradio_python.Param
+#Flow graph block parameters.
address@hidden Josh Blum
+
+#TODO call eval on set value to handle listify and stringify flags
+
+from grc.elements.Param import Param as _Param
+import os
+
+class Param(_Param):
+
+ ##possible param types
+ TYPES = _Param.TYPES + [
+ 'complex', 'real', 'int',
+ 'complex_vector', 'real_vector', 'int_vector',
+ 'hex', 'string',
+ 'file_open', 'file_save',
+ 'id',
+ 'source_pad_key', 'sink_pad_key',
+ ]
+
+ def evaluate(self):
+ """
+ Evaluate the value.
+ @return evaluated type
+ """
+ self._lisitify_flag = False
+ self._stringify_flag = False
+
+ def eval_string(v):
+ try:
+ e = self.get_parent().get_parent().evaluate(v)
+ assert(isinstance(e, str))
+ return e
+ except:
+ self._stringify_flag = True
+ return v
+ t = self.get_type()
+ v = self.get_value()
+ if self.is_enum():
+ return self.get_option(self.get_value()).get_name()
+ elif t in ('raw', 'complex', 'real', 'int', 'complex_vector',
'real_vector', 'int_vector', 'hex'):
+ #raise exception if python cannot evaluate this value
+ try: e = self.get_parent().get_parent().evaluate(v)
+ except:
+ self._add_error_message('Value "%s" cannot be
evaluated.'%v)
+ raise Exception
+ #raise an exception if the data is invalid
+ if t == 'raw':
+ return e
+ elif t == 'complex':
+ try: assert(isinstance(e, (complex, float, int,
long)))
+ except AssertionError:
+ self._add_error_message('Expression
"%s" is invalid for type complex.'%str(e))
+ raise Exception
+ return e
+ elif t == 'real':
+ try: assert(isinstance(e, (float, int, long)))
+ except AssertionError:
+ self._add_error_message('Expression
"%s" is invalid for type real.'%str(e))
+ raise Exception
+ return e
+ elif t == 'int':
+ try: assert(isinstance(e, (int, long)))
+ except AssertionError:
+ self._add_error_message('Expression
"%s" is invalid for type integer.'%str(e))
+ raise Exception
+ return e
+ elif t == 'complex_vector':
+ if not isinstance(e, (tuple, list, set)):
+ self._lisitify_flag = True
+ e = [e]
+ try:
+ for ei in e:
+ assert(isinstance(ei, (complex,
float, int, long)))
+ except AssertionError:
+ self._add_error_message('Expression
"%s" is invalid for type complex vector.'%str(e))
+ raise Exception
+ return e
+ elif t == 'real_vector':
+ if not isinstance(e, (tuple, list, set)):
+ self._lisitify_flag = True
+ e = [e]
+ try:
+ for ei in e:
+ assert(isinstance(ei, (float,
int, long)))
+ except AssertionError:
+ self._add_error_message('Expression
"%s" is invalid for type real vector.'%str(e))
+ raise Exception
+ return e
+ elif t == 'int_vector':
+ if not isinstance(e, (tuple, list, set)):
+ self._lisitify_flag = True
+ e = [e]
+ try:
+ for ei in e:
+ assert(isinstance(ei, (int,
long)))
+ except AssertionError:
+ self._add_error_message('Expression
"%s" is invalid for type integer vector.'%str(e))
+ raise Exception
+ return e
+ elif t == 'hex':
+ return hex(e)
+ else: raise TypeError, 'Type "%s" not handled'%t
+ elif t == 'string':
+ e = eval_string(v)
+ return str(e)
+ elif t == 'file_open':
+ e = eval_string(v)
+ e = str(e)
+ assert(os.path.isfile(e))
+ return e
+ elif t == 'file_save':
+ e = eval_string(v)
+ e = str(e)
+ assert(os.path.exists(os.path.dirname(e)))
+ assert(not os.path.isdir(e))
+ assert(path.basename(e))
+ return e
+ elif t == 'id':
+ #can python use this as a variable?
+ try:
+ assert(len(v) > 0)
+ assert(v[0].isalpha())
+ for c in v: assert(c.isalnum() or c in ('_',))
+ except:
+ self._add_error_message('ID "%s" must be
alpha-numeric or underscored, and begin with a letter.'%v)
+ raise Exception
+ params = self.get_all_params('var_key')
+ keys = [param.get_value() for param in params]
+ try: assert(len(keys) == len(set(keys)))
+ except:
+ self._add_error_message('ID "%s" is not
unique.'%v)
+ raise Exception
+ return v
+ elif t == 'source_pad_key':
+ #should be an integer
+ try: e = int(v)
+ except:
+ self._add_error_message('Source Pad Key "%s" is
not an integer.'%v)
+ raise Exception
+ params = filter(lambda p: p.is_valid(),
self.get_all_params('source_pad_key'))
+ keys = [int(param.get_value()) for param in params]
+ try: assert(len(keys) == len(set(keys)))
+ except:
+ self._add_error_message('Source Pad Key "%s" is
not unique.'%v)
+ raise Exception
+ return e
+ elif t == 'sink_pad_key':
+ #should be an integer
+ try: e = int(v)
+ except:
+ self._add_error_message('Sink Pad Key "%s" is
not an integer.'%v)
+ raise Exception
+ params = filter(lambda p: p.is_valid(),
self.get_all_params('sink_pad_key'))
+ keys = [int(param.get_value()) for param in params]
+ try: assert(len(keys) == len(set(keys)))
+ except:
+ self._add_error_message('Sink Pad Key "%s" is
not unique.'%v)
+ raise Exception
+ return e
+ else: raise TypeError, 'Type "%s" not handled'%t
+
+ def to_code(self):
+ """!
+ Convert the value to code.
+ @return a string representing the code
+ """
+ v = self.get_value()
+ t = self.get_type()
+ if t in ('string', 'file_open', 'file_save'):
+ if self._stringify_flag:
+ return '"%s"'%v
+ else:
+ return v
+ elif t in ('complex_vector', 'real_vector', 'int_vector'):
+ if self._lisitify_flag:
+ return '(%s, )'%v
+ else:
+ return '(%s)'%v
+ else:
+ return v
+
+ def get_all_params(self, type):
+ """!
+ Get all the params from the flowgraph that have the given type.
+ @param type the specified type
+ @return a list of params
+ """
+ all_params = list()
+ #get all blocks
+ blocks = self.get_parent().get_parent().get_blocks()
+ for block in blocks:
+ #filter params of other types
+ params = filter(lambda p: p.get_type() == type,
block.get_params())
+ all_params.extend(params)
+ return all_params
+
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Commit-gnuradio] r7715 - grc/branches/grc_reloaded/src/grc/platforms/gnuradio_python,
jblum <=