From 9d07c0077f198e7ae93e0a9b92bbd34b16359dc3 Mon Sep 17 00:00:00 2001 From: Vaclav Petras Date: Wed, 29 Nov 2023 17:17:24 -0500 Subject: [PATCH 1/2] grass.pygrass: Support shortened parameter values Command line interface parser allows parameter (option) values such as 'val' when full value should be 'value'. Parameter class from pygrass does the checking, but does not know about these rules. This addition covers the simple case of val-value which is not much work to implement and maintain. It does not cover more complex cases with underscores and legacy aliases. The test covers the issue for use=value change in v.to.rast made in #3110. Strict checking in pygrass is discussed in #3237. --- .../pygrass/modules/interface/parameter.py | 13 ++++- vector/v.to.rast/testsuite/test_v_to_rast.py | 58 +++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 vector/v.to.rast/testsuite/test_v_to_rast.py diff --git a/python/grass/pygrass/modules/interface/parameter.py b/python/grass/pygrass/modules/interface/parameter.py index a7107434426..37166a9a48b 100644 --- a/python/grass/pygrass/modules/interface/parameter.py +++ b/python/grass/pygrass/modules/interface/parameter.py @@ -13,7 +13,6 @@ def _check_value(param, value): """Function to check the correctness of a value and return the checked value and the original. """ - must_val = "The Parameter <%s>, must be one of the following values: %r" req = "The Parameter <%s>, require: %s, get: %s instead: %r\n%s" string = (type(b""), type("")) @@ -107,7 +106,17 @@ def check_string(value): raise ValueError(err_str) # 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)) + good = False + if param.type == str: + for param_value in param.values: + if param_value.startswith(newvalue): + good = True + break + if not good: + raise ValueError( + f"The Parameter <{param.name}>, must be one of the following values:" + f" {param.values!r} not '{newvalue}'" + ) return ( ( [ diff --git a/vector/v.to.rast/testsuite/test_v_to_rast.py b/vector/v.to.rast/testsuite/test_v_to_rast.py new file mode 100644 index 00000000000..273c77983d8 --- /dev/null +++ b/vector/v.to.rast/testsuite/test_v_to_rast.py @@ -0,0 +1,58 @@ +""" +Name: v.to.rast test + +Purpose: Test v.to.rast + +Author: Vaclav Petras + +Copyright: (C) 2023 by Vaclav Petras and the GRASS Development Team + +Licence: This program is free software under the GNU General Public + License (>=v2). Read the file COPYING that comes with GRASS + for details. +""" +from grass.gunittest.case import TestCase +from grass.gunittest.main import test + + +class TestParameters(TestCase): + """Test v.to.rast""" + + output = "roads" + + @classmethod + def setUpClass(cls): + """Specify region for raster creation for this class""" + cls.use_temp_region() + cls.runModule("g.region", raster="roadsmajor", res=10, flags="a") + + @classmethod + def tearDownClass(cls): + """Remove temporary region""" + cls.del_temp_region() + + def tearDown(self): + """Remove maps after each test method""" + self.runModule( + "g.remove", + flags="f", + type="raster", + name=[self.output], + ) + + def test_legacy_use_interface(self): + """Check that a legacy value for use parameter works""" + self.assertModule( + "v.to.rast", input="roadsmajor", output=self.output, use="val", value=1 + ) + + def test_use_interface(self): + """Check that use=value value=1 works""" + self.assertModule( + "v.to.rast", input="roadsmajor", output=self.output, use="value", value=1 + ) + self.assertRasterFitsInfo(raster=self.output, reference={"min": 1, "max": 1}) + + +if __name__ == "__main__": + test() From 53074fbe76809c9e406c32f207f8c07514c8cd92 Mon Sep 17 00:00:00 2001 From: Vaclav Petras Date: Wed, 29 Nov 2023 20:18:46 -0500 Subject: [PATCH 2/2] Update doctest test result --- python/grass/pygrass/modules/interface/parameter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/grass/pygrass/modules/interface/parameter.py b/python/grass/pygrass/modules/interface/parameter.py index 37166a9a48b..bd998da13e3 100644 --- a/python/grass/pygrass/modules/interface/parameter.py +++ b/python/grass/pygrass/modules/interface/parameter.py @@ -143,7 +143,7 @@ class Parameter(object): >>> param.value = 3 Traceback (most recent call last): ... - ValueError: The Parameter , must be one of the following values: [2, 4, 6, 8] + ValueError: The Parameter , must be one of the following values: [2, 4, 6, 8] not '3' ... """