[GRASS-dev] Parser checking output maps but not input maps

Hi,

is there some reason for GRASS parser not checking if the input map exists? It checks if the output map exists and if it it does it ends execution with an error.

Having this interface definition (full script attached):

#%option G_OPT_R_INPUT
#% key: elevation
#%end
#%option G_OPT_R_OUTPUT
#% key: aspect
#%end

I now get this:

$ r.mapcalc “aaa = 1”
$ g.list rast m=.
aaa
$ test_parser_inputs.py elevation=bbb aspect=aaa
ERROR: option : exists.
$ test_parser_inputs.py elevation=bbb aspect=ccc
(‘bbb’, ‘ccc’) [i.e., no error]

But I would expect to get the error also in the second case:

$ test_parser_inputs.py elevation=bbb aspect=ccc
ERROR: option : doesn’t exist.

Is there some advantage in checking the existence of the input maps and files manually?

Vaclav

(attachments)

test_parser_inputs.py (436 Bytes)

Vaclav Petras wrote:

is there some reason for GRASS parser not checking if the input map exists?
It checks if the output map exists and if it it does it ends execution with
an error.

If an input map (or file) doesn't exist, you'll get an error when the
module tries to open it.

If an output map exists, it would just get silently overwritten if it
wasn't for the checks performed by G_parser().

--
Glynn Clements <glynn@gclements.plus.com>

On Sat, Jul 4, 2015 at 12:55 PM, Glynn Clements <glynn@gclements.plus.com>
wrote:

Vaclav Petras wrote:

> is there some reason for GRASS parser not checking if the input map
exists?
> It checks if the output map exists and if it it does it ends execution
with
> an error.

If an input map (or file) doesn't exist, you'll get an error when the
module tries to open it.

If I understand correctly, this applies only for C modules not Python or
Bash modules, right? In both cases the error is not associated with the
given option while in case of overwrite it is (which is nice).

If an output map exists, it would just get silently overwritten if it

wasn't for the checks performed by G_parser().

--
Glynn Clements <glynn@gclements.plus.com>

Vaclav Petras wrote:

> > is there some reason for GRASS parser not checking if the input map exists?
> > It checks if the output map exists and if it it does it ends execution with
> > an error.
>
> If an input map (or file) doesn't exist, you'll get an error when the
> module tries to open it.

If I understand correctly, this applies only for C modules not Python or
Bash modules, right?

For scripts which use g.parser for argument parsing, if G_parser()
fails g.parser itself fails, which in turn causes the script to fail.

Python scripts should normally terminate on an exception if a spawned
command fails, whereas shell scripts normally ignore the status of any
spanwed commands.

In both cases the error is not associated with the
given option while in case of overwrite it is (which is nice).

Correct.

--
Glynn Clements <glynn@gclements.plus.com>

On Mon, Jul 6, 2015 at 7:56 PM, Glynn Clements <glynn@gclements.plus.com>
wrote:

> > > is there some reason for GRASS parser not checking if the input map
exists?
> > > It checks if the output map exists and if it it does it ends
execution with
> > > an error.
> >
> > If an input map (or file) doesn't exist, you'll get an error when the
> > module tries to open it.
>
> If I understand correctly, this applies only for C modules not Python or
> Bash modules, right?

For scripts which use g.parser for argument parsing, if G_parser()
fails g.parser itself fails, which in turn causes the script to fail.

Python scripts should normally terminate on an exception if a spawned
command fails, whereas shell scripts normally ignore the status of any
spanwed commands.

For Python this is ensured by checking "@ARGS_PARSED@" in g.parser output
in grass.script.core.parse() [1]. For Bash I have no idea how it is/was
done (e.g. in version 6).

However, my problem is that in C if you have an input map, you don't have
to check that whether it exists. While in Python you have to check if it
exists otherwise the first module (subprocess) you call with it will
unexpectedly fail. I hit this issue when I was trying to write
documentation for writing scripts and I wanted to do it the right way (as
opposed to hopping that some random script I pick in the source code
follows well the undocumented API).

The question now is if we want to fix the inconsistency in between writing
C and Python (and error reporting with overwrite). Or if we say that the
current state is good enough as long as it is documented and there is some
convenient function to check existence of a map. I don't think I exhausted
all options, so if somebody has an idea or is willing to implement
something, that would be great.

Vaclav

[1]
https://trac.osgeo.org/grass/browser/grass/trunk/lib/python/script/core.py#L712

Vaclav Petras wrote:

> For scripts which use g.parser for argument parsing, if G_parser()
> fails g.parser itself fails, which in turn causes the script to fail.
>
> Python scripts should normally terminate on an exception if a spawned
> command fails, whereas shell scripts normally ignore the status of any
> spanwed commands.

For Python this is ensured by checking "@ARGS_PARSED@" in g.parser output
in grass.script.core.parse() [1]. For Bash I have no idea how it is/was
done (e.g. in version 6).

bash scripts "exec" g.parser in the current process, in place of bash.
g.parser then re-executes the calling script with the option values
stored in environment variables. If g.parser fails, the script never
gets re-executed, and the exit status of g.parser is the exit status
of the process which was originally running the script.

However, my problem is that in C if you have an input map, you don't have
to check that whether it exists. While in Python you have to check if it
exists otherwise the first module (subprocess) you call with it will
unexpectedly fail. I hit this issue when I was trying to write
documentation for writing scripts and I wanted to do it the right way (as
opposed to hopping that some random script I pick in the source code
follows well the undocumented API).

The question now is if we want to fix the inconsistency in between writing
C and Python (and error reporting with overwrite). Or if we say that the
current state is good enough as long as it is documented and there is some
convenient function to check existence of a map. I don't think I exhausted
all options, so if somebody has an idea or is willing to implement
something, that would be great.

If you consider GRASS modules as being the Python equivalent of GRASS
API functions in C, then there isn't an inconsistency. Rather than
explicitly checking for a map, we rely upon whatever tries to use
generating a fatal error if it doesn't exist.

If you want to directly check for the existence of a map, use
grass.script.find_file() (a wrapper around g.findfile, which in turn
is a wrapper around G_find_file2(), which is what C code would use for
that purpose).

--
Glynn Clements <glynn@gclements.plus.com>