commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r11253 - gnuradio/branches/developers/jblum/grc/grc/py


From: jblum
Subject: [Commit-gnuradio] r11253 - gnuradio/branches/developers/jblum/grc/grc/python
Date: Sun, 21 Jun 2009 01:29:28 -0600 (MDT)

Author: jblum
Date: 2009-06-21 01:29:28 -0600 (Sun, 21 Jun 2009)
New Revision: 11253

Modified:
   gnuradio/branches/developers/jblum/grc/grc/python/Param.py
Log:
Strict type checking on bool: must eval to bool instance (1 or 0 wont work).
Modified evaluate to pass error messages up through Exceptions/Errors.
Regexp for matching id parameters. And some evaluate code cleanup.



Modified: gnuradio/branches/developers/jblum/grc/grc/python/Param.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/python/Param.py  2009-06-21 
06:52:01 UTC (rev 11252)
+++ gnuradio/branches/developers/jblum/grc/grc/python/Param.py  2009-06-21 
07:29:28 UTC (rev 11253)
@@ -29,6 +29,7 @@
 import re
 from gnuradio import gr
 
+_check_id_matcher = re.compile('^[a-z|A-Z]\w*$')
 _show_id_matcher = re.compile('^(variable\w*|parameter|options)$')
 
 class FileParam(EntryParam):
@@ -210,8 +211,7 @@
                _Param.validate(self) #checks type
                self._evaluated = None
                try: self._evaluated = self.evaluate()
-               except: #if the evaluate failed but added no error messages, 
add the generic one below
-                       if not self.get_error_messages(): 
self.add_error_message('Value "%s" cannot be evaluated.'%self.get_value())
+               except Exception, e: self.add_error_message(str(e))
 
        def get_evaluated(self): return self._evaluated
 
@@ -244,28 +244,20 @@
                elif t in ('raw', 'complex', 'real', 'int', 'complex_vector', 
'real_vector', 'int_vector', 'hex', 'bool'):
                        #raise exception if python cannot evaluate this value
                        try: e = self.get_parent().get_parent().evaluate(v)
-                       except Exception, e:
-                               self.add_error_message('Value "%s" cannot be 
evaluated: %s'%(v, e))
-                               raise Exception
+                       except Exception, e: raise Exception, 'Value "%s" 
cannot be evaluated: %s'%(v, e)
                        #raise an exception if the data is invalid
                        if t == 'raw': return e
                        elif t == 'complex':
-                               try: assert(isinstance(e, COMPLEX_TYPES))
-                               except AssertionError:
-                                       self.add_error_message('Expression "%s" 
is invalid for type complex.'%str(e))
-                                       raise Exception
+                               try: assert isinstance(e, COMPLEX_TYPES)
+                               except AssertionError: raise Exception, 
'Expression "%s" is invalid for type complex.'%str(e)
                                return e
                        elif t == 'real':
-                               try: assert(isinstance(e, REAL_TYPES))
-                               except AssertionError:
-                                       self.add_error_message('Expression "%s" 
is invalid for type real.'%str(e))
-                                       raise Exception
+                               try: assert isinstance(e, REAL_TYPES)
+                               except AssertionError: raise Exception, 
'Expression "%s" is invalid for type real.'%str(e)
                                return e
                        elif t == 'int':
-                               try: assert(isinstance(e, INT_TYPES))
-                               except AssertionError:
-                                       self.add_error_message('Expression "%s" 
is invalid for type integer.'%str(e))
-                                       raise Exception
+                               try: assert isinstance(e, INT_TYPES)
+                               except AssertionError: raise Exception, 
'Expression "%s" is invalid for type integer.'%str(e)
                                return e
                        #########################
                        # Numeric Vector Types
@@ -275,41 +267,30 @@
                                        self._lisitify_flag = True
                                        e = [e]
                                try:
-                                       for ei in e:
-                                               assert(isinstance(ei, 
COMPLEX_TYPES))
-                               except AssertionError:
-                                       self.add_error_message('Expression "%s" 
is invalid for type complex vector.'%str(e))
-                                       raise Exception
+                                       for ei in e: assert isinstance(ei, 
COMPLEX_TYPES)
+                               except AssertionError: raise Exception, 
'Expression "%s" is invalid for type complex vector.'%str(e)
                                return e
                        elif t == 'real_vector':
                                if not isinstance(e, VECTOR_TYPES):
                                        self._lisitify_flag = True
                                        e = [e]
                                try:
-                                       for ei in e:
-                                               assert(isinstance(ei, 
REAL_TYPES))
-                               except AssertionError:
-                                       self.add_error_message('Expression "%s" 
is invalid for type real vector.'%str(e))
-                                       raise Exception
+                                       for ei in e: assert isinstance(ei, 
REAL_TYPES)
+                               except AssertionError: raise Exception, 
'Expression "%s" is invalid for type real vector.'%str(e)
                                return e
                        elif t == 'int_vector':
                                if not isinstance(e, VECTOR_TYPES):
                                        self._lisitify_flag = True
                                        e = [e]
                                try:
-                                       for ei in e:
-                                               assert(isinstance(ei, 
INT_TYPES))
-                               except AssertionError:
-                                       self.add_error_message('Expression "%s" 
is invalid for type integer vector.'%str(e))
-                                       raise Exception
+                                       for ei in e: assert isinstance(ei, 
INT_TYPES)
+                               except AssertionError: raise Exception, 
'Expression "%s" is invalid for type integer vector.'%str(e)
                                return e
                        elif t == 'hex': return hex(e)
                        elif t == 'bool':
-                               try: assert e in (True, False)
-                               except AssertionError:
-                                       self.add_error_message('Expression "%s" 
is invalid for type bool.'%str(e))
-                                       raise Exception
-                               return bool(e)
+                               try: assert isinstance(e, bool)
+                               except AssertionError: raise Exception, 
'Expression "%s" is invalid for type bool.'%str(e)
+                               return e
                        else: raise TypeError, 'Type "%s" not handled'%t
                #########################
                # String Types
@@ -323,23 +304,14 @@
                #########################
                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 AssertionError:
-                               self.add_error_message('ID "%s" must be 
alpha-numeric or underscored, and begin with a letter.'%v)
-                               raise Exception
+                       try: assert _check_id_matcher.match(v)
+                       except AssertionError: raise Exception, 'ID "%s" must 
begin with a letter and may contain letters, numbers, and underscores.'%v
                        params = self.get_all_params('id')
                        keys = [param.get_value() for param in params]
                        try: assert keys.count(v) <= 1 #id should only appear 
once, or zero times if block is disabled
-                       except:
-                               self.add_error_message('ID "%s" is not 
unique.'%v)
-                               raise Exception
+                       except: raise Exception, 'ID "%s" is not unique.'%v
                        try: assert v not in ID_BLACKLIST
-                       except:
-                               self.add_error_message('ID "%s" is 
blacklisted.'%v)
-                               raise Exception
+                       except: raise Exception, 'ID "%s" is blacklisted.'%v
                        return v
                #########################
                # Grid Position Type
@@ -348,22 +320,16 @@
                        if not v: return '' #allow for empty grid pos
                        e = self.get_parent().get_parent().evaluate(v)
                        try:
-                               assert(isinstance(e, (list, tuple)) and len(e) 
== 4)
-                               for ei in e: assert(isinstance(ei, int))
-                       except AssertionError:
-                               self.add_error_message('A grid position must be 
a list of 4 integers.')
-                               raise Exception
+                               assert isinstance(e, (list, tuple)) and len(e) 
== 4
+                               for ei in e: assert isinstance(ei, int)
+                       except AssertionError: raise Exception, 'A grid 
position must be a list of 4 integers.'
                        row, col, row_span, col_span = e
                        #check row, col
-                       try: assert(row >= 0 and col >= 0)
-                       except AssertionError:
-                               self.add_error_message('Row and column must be 
non-negative.')
-                               raise Exception
+                       try: assert row >= 0 and col >= 0
+                       except AssertionError: raise Exception, 'Row and column 
must be non-negative.'
                        #check row span, col span
-                       try: assert(row_span > 0 and col_span > 0)
-                       except AssertionError:
-                               self.add_error_message('Row and column span 
must be greater than zero.')
-                               raise Exception
+                       try: assert row_span > 0 and col_span > 0
+                       except AssertionError: raise Exception, 'Row and column 
span must be greater than zero.'
                        #calculate hostage cells
                        for r in range(row_span):
                                for c in range(col_span):
@@ -372,9 +338,7 @@
                        params = filter(lambda p: p is not self, 
self.get_all_params('grid_pos'))
                        for param in params:
                                for cell in param._hostage_cells:
-                                       if cell in self._hostage_cells:
-                                               self.add_error_message('Another 
graphical element is using cell "%s".'%str(cell))
-                                               raise Exception
+                                       if cell in self._hostage_cells: raise 
Exception, 'Another graphical element is using cell "%s".'%str(cell)
                        return e
                #########################
                # Import Type
@@ -382,12 +346,8 @@
                elif t == 'import':
                        n = dict() #new namespace
                        try: exec v in n
-                       except ImportError:
-                               self.add_error_message('Import "%s" failed.'%v)
-                               raise Exception
-                       except Exception:
-                               self.add_error_message('Bad import syntax: 
"%s".'%v)
-                               raise Exception
+                       except ImportError: raise Exception, 'Import "%s" 
failed.'%v
+                       except Exception: raise Exception, 'Bad import syntax: 
"%s".'%v
                        return filter(lambda k: str(k) != '__builtins__', 
n.keys())
                #########################
                else: raise TypeError, 'Type "%s" not handled'%t





reply via email to

[Prev in Thread] Current Thread [Next in Thread]