[GRASS-SVN] r61183 - in grass/trunk/lib/python/pygrass/modules/interface: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jul 8 01:24:08 PDT 2014


Author: zarch
Date: 2014-07-08 01:24:08 -0700 (Tue, 08 Jul 2014)
New Revision: 61183

Modified:
   grass/trunk/lib/python/pygrass/modules/interface/parameter.py
   grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py
Log:
pygrass: Better handling of multiple and keydescvalues parameters

Modified: grass/trunk/lib/python/pygrass/modules/interface/parameter.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/parameter.py	2014-07-08 07:10:51 UTC (rev 61182)
+++ grass/trunk/lib/python/pygrass/modules/interface/parameter.py	2014-07-08 08:24:08 UTC (rev 61183)
@@ -46,22 +46,25 @@
     if value is None:
         return param.default, param.default
 
-    # find errors with multiple parmaeters
-    if param.multiple or param.keydescvalues:
-        if not isinstance(value, (list, tuple)):
-            msg = ('The Parameter <%s> require multiple inputs,'
-                   ' give: %r instaed')
-            raise TypeError(msg % (param.name, value))
-        # everything looks fine, so check each value
-        try:
-            return [param.type(check_string(val)) for val in value], value
-        except Exception as exc:
-            raiseexcpet(exc, param, param.type, value)
-    elif isinstance(value, (list, tuple)):
-        # this check is needed to avoid a list is converted to string silently
-        msg = 'The Parameter <%s> does not accept multiple inputs'
-        raise TypeError(msg % param.name)
+    # find errors with multiple parmeters
+    if isinstance(value, (list, tuple)):
+        if param.keydescvalues:
+            return (([value, ], value) if isinstance(value, tuple)
+                    else (value, value))
+        if param.multiple:
+            # everything looks fine, so check each value
+            try:
+                return [param.type(check_string(val)) for val in value], value
+            except Exception as exc:
+                raiseexcpet(exc, param, param.type, value)
+        else:
+            msg = 'The Parameter <%s> does not accept multiple inputs'
+            raise TypeError(msg % param.name)
 
+    if param.keydescvalues:
+        msg = 'The Parameter <%s> require multiple inputs in the form: %s'
+        raise TypeError(msg % (param.name, param.keydescvalues))
+
     if param.typedesc == 'all':
         return value, value
 
@@ -86,7 +89,8 @@
         # check if value is in the list of valid values
         if param.values is not None and newvalue not in param.values:
             raise ValueError(must_val % (param.name, param.values))
-    return newvalue, value
+    return (([newvalue, ] if (param.multiple or param.keydescvalues)
+             else newvalue), value)
 
 
 # TODO add documentation

Modified: grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py	2014-07-08 07:10:51 UTC (rev 61182)
+++ grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_parameter.py	2014-07-08 08:24:08 UTC (rev 61183)
@@ -69,13 +69,17 @@
             value = ("1.4", "2.3")
             self.assertTupleEqual(([float(v) for v in value], value),
                                   _check_value(param, value))
+            value = 1.
+            self.assertTupleEqual(([value, ], value),
+                                  _check_value(param, value))
+            value = 1
+            self.assertTupleEqual(([value, ], value),
+                                  _check_value(param, value))
 
             # test errors
-            with self.assertRaises(TypeError):
-                _check_value(param, 1.)
-            with self.assertRaises(TypeError):
-                _check_value(param, 1)
             with self.assertRaises(ValueError):
+                _check_value(param, "elev")
+            with self.assertRaises(ValueError):
                 _check_value(param, ("elev", "slope", "aspect"))
 
     def test_range_float_double(self):
@@ -124,6 +128,7 @@
         param = Parameter(diz=dict(name='int_number', required='yes',
                                    multiple='yes', type='integer'))
         value = (1, 2)
+        #import ipdb; ipdb.set_trace()
         self.assertTupleEqual((list(value), value), _check_value(param, value))
         value = (1.2, 2.3)
         self.assertTupleEqual(([int(v) for v in value], value),
@@ -131,19 +136,36 @@
         value = ("1", "2")
         self.assertTupleEqual(([int(v) for v in value], value),
                               _check_value(param, value))
+        value = 1
+        self.assertTupleEqual(([1, ], value), _check_value(param, value))
+        value = 1.2
+        self.assertTupleEqual(([int(value), ], value),
+                              _check_value(param, value))
+        value = "1"
+        self.assertTupleEqual(([int(value), ], value),
+                              _check_value(param, value))
 
         # test errors
-        with self.assertRaises(TypeError):
-            _check_value(param, 1)
-        with self.assertRaises(TypeError):
-            _check_value(param, 1.0)
-        with self.assertRaises(TypeError):
-            _check_value(param, "1.")
-        with self.assertRaises(TypeError):
+        with self.assertRaises(ValueError):
             _check_value(param, "elev")
         with self.assertRaises(ValueError):
             _check_value(param, ("elev", "slope", "aspect"))
 
+    def test_keydescvalues(self):
+        for ptype in ('integer', 'float'):
+            param = Parameter(diz=dict(name='int_number', required='yes',
+                                       multiple='yes',
+                                       keydesc=('range', '(min, max)'),
+                                       type='integer'))
+            value = (1, 2)
+            self.assertTupleEqual(([value, ], value),
+                                  _check_value(param, value))
+            value = [(1, 2), (2, 3)]
+            self.assertTupleEqual((value, value), _check_value(param, value))
+
+            with self.assertRaises(TypeError):
+                _check_value(param, 1)
+
     def test_range_integer(self):
         param = Parameter(diz=dict(name='int_number', required='yes',
                                    multiple='no', type='integer',
@@ -224,6 +246,8 @@
         value = (1, 2, 3)
         self.assertTupleEqual(([str(v) for v in value], value),
                               _check_value(param, value))
+        value = 'elev'
+        self.assertTupleEqual(([value, ], value), _check_value(param, value))
 
         # test errors
         with self.assertRaises(ValueError):
@@ -234,7 +258,7 @@
         param = Parameter(diz=dict(name='rastname', required='yes',
                                    multiple='no', type='string',
                                    values=values))
-        value="asp"
+        value = "asp"
         self.assertTupleEqual((value, value), _check_value(param, value))
 
         # test errors



More information about the grass-commit mailing list