[GRASS-SVN] r62196 - in grass/trunk/lib/python/script: . testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Oct 6 07:56:02 PDT 2014


Author: wenzeslaus
Date: 2014-10-06 07:56:02 -0700 (Mon, 06 Oct 2014)
New Revision: 62196

Added:
   grass/trunk/lib/python/script/testsuite/
   grass/trunk/lib/python/script/testsuite/test_start_command_functions.py
   grass/trunk/lib/python/script/testsuite/test_start_command_functions_nc.py
Modified:
   grass/trunk/lib/python/script/core.py
Log:
pythonlib: allow underscore at the end of core start_command family of functions as a way of avoiding conflict with Python keywords

The older undocumented behavior was undersocre at the beginning which does not follow PEP8 was depreciated.

Tests added for location without maps and for location with stadard map names (not curretly supported in gunittest). The ones using existing maps are more precise.


Modified: grass/trunk/lib/python/script/core.py
===================================================================
--- grass/trunk/lib/python/script/core.py	2014-10-06 13:01:28 UTC (rev 62195)
+++ grass/trunk/lib/python/script/core.py	2014-10-06 14:56:02 UTC (rev 62196)
@@ -278,8 +278,15 @@
         args.append("-%s" % flags)
     for opt, val in options.iteritems():
         if val != None:
-            if opt[0] == '_':
+            if opt.startswith('_'):
                 opt = opt[1:]
+                warning(_("To run the module add underscore at the end"
+                    " of the option <%s> to avoid conflict with Python"
+                    " keywords. Underscore at the beginning is"
+                    " depreciated in GRASS GIS 7.0 and will be removed"
+                    " in version 7.1.") % opt)
+            elif opt.endswith('_'):
+                opt = opt[:-1]
             args.append("%s=%s" % (opt, _make_val(val)))
     return args
 
@@ -301,6 +308,9 @@
     GUI='text';
     MONITOR='x0';
 
+    If the module parameter is the same as Python keyword, add
+    underscore at the end of the parameter. For example, use
+    ``lambda_=1.6`` instead of ``lambda=1.6``.
 
     :param str prog: GRASS module
     :param str flags: flags to be used (given as a string)

Added: grass/trunk/lib/python/script/testsuite/test_start_command_functions.py
===================================================================
--- grass/trunk/lib/python/script/testsuite/test_start_command_functions.py	                        (rev 0)
+++ grass/trunk/lib/python/script/testsuite/test_start_command_functions.py	2014-10-06 14:56:02 UTC (rev 62196)
@@ -0,0 +1,41 @@
+"""Tests of start_command function family (location independent)"""
+
+import grass.gunittest
+
+from grass.script.core import start_command, PIPE
+
+
+class TestPythonKeywordsInParameters(grass.gunittest.TestCase):
+    """Tests additional underscore syntax which helps to avoid Python keywords
+
+    It works the same for keywords, buildins and any names.
+    """
+
+    raster = 'does_not_exist'
+
+    def test_prefixed_underscore(self):
+        proc = start_command(
+            'g.region', _rast=self.raster, stderr=PIPE)
+        stderr = proc.communicate()[1]
+        self.assertNotIn('_rast', stderr)
+        self.assertIn(self.raster, stderr,
+            msg="Raster map name should appear in the error output")
+
+    def test_suffixed_underscore(self):
+        proc = start_command(
+            'g.region', rast_=self.raster, stderr=PIPE)
+        stderr = proc.communicate()[1]
+        self.assertNotIn('rast_', stderr)
+        self.assertIn(self.raster, stderr,
+            msg="Raster map name should appear in the error output")
+
+    def test_multiple_underscores(self):
+        proc = start_command(
+            'g.region', _rast_=self.raster, stderr=PIPE)
+        stderr = proc.communicate()[1]
+        returncode = proc.poll()
+        self.assertEquals(returncode, 1)
+        self.assertIn('rast', stderr)
+
+if __name__ == '__main__':
+    grass.gunittest.test()


Property changes on: grass/trunk/lib/python/script/testsuite/test_start_command_functions.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native

Added: grass/trunk/lib/python/script/testsuite/test_start_command_functions_nc.py
===================================================================
--- grass/trunk/lib/python/script/testsuite/test_start_command_functions_nc.py	                        (rev 0)
+++ grass/trunk/lib/python/script/testsuite/test_start_command_functions_nc.py	2014-10-06 14:56:02 UTC (rev 62196)
@@ -0,0 +1,53 @@
+"""Tests of start_command function family in nc location"""
+
+LOCATION = 'nc'
+
+import grass.gunittest
+
+from grass.script.core import start_command, PIPE
+
+
+class TestPythonKeywordsInParameters(grass.gunittest.TestCase):
+    """Tests additional underscore syntax which helps to avoid Python keywords
+
+    It works the same for keywords, buildins and any names.
+    """
+
+    raster = 'elevation'
+
+    # fresh region for each test function
+    def setUp(self):
+        self.use_temp_region()
+        
+    def tearDown(self):
+        self.del_temp_region()
+
+    def test_prefixed_underscore(self):
+        proc = start_command(
+            'g.region', _rast=self.raster, stderr=PIPE)
+        stderr = proc.communicate()[1]
+        returncode = proc.poll()
+        self.assertEquals(returncode, 0,
+            msg="Undersocre as prefix was not accepted")
+        self.assertNotIn('_rast', stderr)
+
+    def test_suffixed_underscore(self):
+        proc = start_command(
+            'g.region', rast_=self.raster, stderr=PIPE)
+        stderr = proc.communicate()[1]
+        returncode = proc.poll()
+        self.assertEquals(returncode, 0,
+            msg="Undersocre as suffix was not accepted, stderr is:\n%s" % stderr)
+        self.assertNotIn('rast_', stderr)
+
+    def test_multiple_underscores(self):
+        proc = start_command(
+            'g.region', _rast_=self.raster, stderr=PIPE)
+        stderr = proc.communicate()[1]
+        returncode = proc.poll()
+        self.assertEquals(returncode, 1,
+            msg="Undersocre at both sides was accepted")
+        self.assertIn('rast', stderr)
+
+if __name__ == '__main__':
+    grass.gunittest.test()


Property changes on: grass/trunk/lib/python/script/testsuite/test_start_command_functions_nc.py
___________________________________________________________________
Added: svn:mime-type
   + text/x-python
Added: svn:eol-style
   + native



More information about the grass-commit mailing list