Skip to content

Commit

Permalink
pygrass: backport #1407 Module fix (#1606)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrasovaa committed May 31, 2021
1 parent 5337713 commit 83c4533
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 123 deletions.
30 changes: 6 additions & 24 deletions lib/python/docs/src/pygrass_modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,32 +168,16 @@ User or developer can check which parameters have been set, with: ::
print "Aspect is not computed"


After we set the parameters and run the module, the execution of the module
instantiate a popen attribute to the class. The `Popen`_ class allow user
to kill/wait/ the process. ::
After we set the parameters, we can run the module in the background with
`finish_=False`. Then call `wait()` and retrieve the returncode. ::

>>> slope_aspect = Module('r.slope.aspect')
>>> slope_aspect(elevation='elevation', slope='slp', aspect='asp', overwrite=True, finish_=False)
>>> slope_aspect.popen.wait() # *.kill(), *.terminate()
>>> slope_aspect.wait()
Module('r.slope.aspect')
>>> slope_aspect.returncode
0
>>> out, err = slope_aspect.popen.communicate()
>>> print err #doctest: +NORMALIZE_WHITESPACE
100%
Aspect raster map <asp> complete
Slope raster map <slp> complete
<BLANKLINE>

On the above example we use a new parameter `finish_`, if is set to True, the
run method, automatically store the stdout and stderr to stdout and stderr
attributes of the class: ::

>>> slope_aspect = Module('r.slope.aspect')
>>> slope_aspect(elevation='elevation', slope='slp', aspect='asp', overwrite=True, finish_=True)
>>> print slope_aspect.stderr #doctest: +NORMALIZE_WHITESPACE
100%
Aspect raster map <asp> complete
Slope raster map <slp> complete
<BLANKLINE>

Another example of use: ::

Expand All @@ -202,9 +186,7 @@ Another example of use: ::
>>> from grass.script.utils import parse_key_val
>>> parse_key_val(info.outputs.stdout)
{'max': '156.3299', 'min': '55.57879'}
>>> info = Module("r.info", map="elevation", flags="r", finish_=False)
>>> category = Module("r.category", map="elevation",
... stdin_=info.popen.stdout, finish_=True)


Launching GRASS GIS modules in parallel
---------------------------------------
Expand Down
23 changes: 12 additions & 11 deletions lib/python/gunittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,9 @@ def runModule(cls, module, expecting_stdout=False, **kwargs):
errors += "\nSee available vector maps:\n"
errors += call_module('g.list', type='vector')
# TODO: message format, parameters
raise CalledModuleError(module.popen.returncode, module.name,
module.get_python(),
errors=errors)
raise CalledModuleError(
module.returncode, module.name, module.get_python(), errors=errors
)
# TODO: use this also in assert and apply when appropriate
if expecting_stdout and not module.outputs.stdout.strip():

Expand Down Expand Up @@ -1153,14 +1153,15 @@ def assertModule(self, module, msg=None, **kwargs):
print(text_to_string(module.outputs.stderr))
# TODO: message format
# TODO: stderr?
stdmsg = ('Running <{m.name}> module ended'
' with non-zero return code ({m.popen.returncode})\n'
'Called: {code}\n'
'See the following errors:\n'
'{errors}'.format(
m=module, code=module.get_python(),
errors=module.outputs.stderr
))
stdmsg = (
"Running <{m.name}> module ended"
" with non-zero return code ({m.returncode})\n"
"Called: {code}\n"
"See the following errors:\n"
"{errors}".format(
m=module, code=module.get_python(), errors=module.outputs.stderr
)
)
self.fail(self._formatMessage(msg, stdmsg))
print(text_to_string(module.outputs.stdout))
print(text_to_string(module.outputs.stderr))
Expand Down
4 changes: 2 additions & 2 deletions lib/python/gunittest/gmodules.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class SimpleModule(Module):
... overwrite=True)
>>> mapcalc.run()
Module('r.mapcalc')
>>> mapcalc.popen.returncode
>>> mapcalc.returncode
0
>>> colors = SimpleModule('r.colors',
... map='test_a', rules='-', stdin_='1 red')
>>> colors.run()
Module('r.colors')
>>> colors.popen.returncode
>>> colors.returncode
0
>>> str(colors.inputs.stdin)
'1 red'
Expand Down
Loading

0 comments on commit 83c4533

Please sign in to comment.