OSGeo / grass

GRASS GIS - free and open-source geospatial processing engine
https://grass.osgeo.org
Other
847 stars 308 forks source link

[Bug] parse_command whole Python trace by error #1398

Open anikaweinmann opened 3 years ago

anikaweinmann commented 3 years ago

Describe the bug By using parse_command(...) in Python the whole Python trace is shown if the module fails and can only be transformed to an Error message with grass.Popen(...) e.g. in like addon as https://github.com/OSGeo/grass-addons/blob/master/grass7/imagery/i.sentinel/i.sentinel.coverage/i.sentinel.coverage.py#L231-L251 (https://github.com/OSGeo/grass-addons/pull/434/files). The error trace look e.g. like this:

>>> test = grass.parse_command('i.sentinel.download', flags='l', settings='.esa_sentinel2', start="2020-08-01", end="2020-09-30", producttype="S2MSI2A", clouds=5)
FEHLER: Unable to connect Copernicus Open Access Hub: HTTP status 503
        Service Unavailable:
        The Copernicus Open Access Hub

        # The Copernicus Open Access Hub will be back soon!

        Sorry for the inconvenience,
        we're performing some maintenance at the moment.

        <https://scihub.copernicus.eu/news/News00829>

        We'll be back online shortly!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/grass78/etc/python/grass/script/core.py", line 543, in parse_command
    res = read_command(*args, **kwargs)
  File "/usr/local/grass78/etc/python/grass/script/core.py", line 503, in read_command
    return handle_errors(returncode, stdout, args, kwargs)
  File "/usr/local/grass78/etc/python/grass/script/core.py", line 342, in handle_errors
    raise CalledModuleError(module=None, code=code,
grass.exceptions.CalledModuleError: Module run None i.sentinel.download -l settings=.sentinel2 start=2020-08-01 end=2020-09-30 producttype=S2MSI2A clouds=5 ended with error
Process ended with non-zero return code 1. See errors in the (error) output.

To Reproduce Steps to reproduce the behavior with the error FEHLER: Invalid settings file:

  1. touch .esa_sentinel2
  2. python3
    import grass.script as grass
    test = grass.parse_command('i.sentinel.download', flags='l', settings='.esa_sentinel2', start="2020-08-01", end="2020-09-30", producttype="S2MSI2A", clouds=5)

Expected behavior It would be nice if grass.parse_command(...) returned an error properly, so that the whole Python trace is not visible and you can then just pass on the error.

System description (please complete the following information):

petrasovaa commented 3 years ago

This is not a bug, parse_command is supposed to give you parsed stdout or throw exception as you would expect from a Python function. You probably need to use start_command, which is lower-level and allows you to capture stdout and stderr. I suggest to better specify what you would expect and open it as feature request.