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

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 16 02:44:25 PDT 2015


Author: zarch
Date: 2015-07-16 02:44:25 -0700 (Thu, 16 Jul 2015)
New Revision: 65598

Modified:
   grass/trunk/lib/python/pygrass/modules/interface/module.py
   grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py
Log:
pygrass: Add a new method to check the correctness of the provide parameter, a new special parameter: check_ is added to skip the check

Modified: grass/trunk/lib/python/pygrass/modules/interface/module.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/module.py	2015-07-16 09:40:30 UTC (rev 65597)
+++ grass/trunk/lib/python/pygrass/modules/interface/module.py	2015-07-16 09:44:25 UTC (rev 65598)
@@ -505,6 +505,7 @@
         #
         self.run_ = True
         self.finish_ = True
+        self.check_ = True
         self.env_ = None
         self.stdin_ = None
         self.stdin = None
@@ -544,7 +545,7 @@
             del(kargs['flags'])
 
         # set attributs
-        for key in ('run_', 'env_', 'finish_', 'stdout_', 'stderr_'):
+        for key in ('run_', 'env_', 'finish_', 'stdout_', 'stderr_', 'check_'):
             if key in kargs:
                 setattr(self, key, kargs.pop(key))
 
@@ -554,7 +555,7 @@
                 self.inputs[key[:-1]].value = kargs.pop(key)
 
         #
-        # check args
+        # set/update args
         #
         for param, arg in zip(self.params_list, args):
             param.value = arg
@@ -577,11 +578,8 @@
             #
             # check reqire parameters
             #
-            for k in self.required:
-                if ((k in self.inputs and self.inputs[k].value is None) or
-                        (k in self.outputs and self.outputs[k].value is None)):
-                    msg = "Required parameter <%s> not set."
-                    raise ParameterError(msg % k)
+            if self.check_:
+                self.check()
             return self.run()
         return self
 
@@ -635,6 +633,19 @@
         flags = self.flags.__doc__
         return '\n'.join([head, params, DOC['flag_head'], flags, DOC['foot']])
 
+    def check(self):
+        """Check the correctness of the provide parameters"""
+        required = True
+        for flg in self.flags.values():
+            if flg and flg.suppress_required:
+                required = False
+        if required:
+            for k in self.required:
+                if ((k in self.inputs and self.inputs[k].value is None) or
+                        (k in self.outputs and self.outputs[k].value is None)):
+                    msg = "Required parameter <%s> not set."
+                    raise ParameterError(msg % k)
+
     def get_dict(self):
         """Return a dictionary that includes the name, all valid
         inputs, outputs and flags

Modified: grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py	2015-07-16 09:40:30 UTC (rev 65597)
+++ grass/trunk/lib/python/pygrass/modules/interface/testsuite/test_modules.py	2015-07-16 09:44:25 UTC (rev 65598)
@@ -10,6 +10,7 @@
 from grass.gunittest.main import test
 
 from grass.script.core import get_commands
+from grass.exceptions import ParameterError
 from grass.pygrass.modules.interface import Module
 
 PY2 = sys.version_info[0] == 2
@@ -65,5 +66,18 @@
         out.close()
 
 
+class TestModulesCheck(TestCase):
+    def test_flags_with_suppress_required(self):
+        """Test if flags with suppress required are handle correctly"""
+        gextension = Module('g.extension')
+        # check if raise an error if required parameter are missing
+        with self.assertRaises(ParameterError):
+            gextension.check()
+
+        # check if the flag suppress the required parameters
+        gextension.flags.a = True
+        self.assertIsNone(gextension.check())
+
+
 if __name__ == '__main__':
     test()



More information about the grass-commit mailing list