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

2015-07-08 Thread Glynn Clements

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 gl...@gclements.plus.com
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


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

2015-07-06 Thread Glynn Clements

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 gl...@gclements.plus.com
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


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

2015-07-06 Thread Vaclav Petras
On Mon, Jul 6, 2015 at 7:56 PM, Glynn Clements gl...@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
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

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

2015-07-04 Thread Glynn Clements

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 gl...@gclements.plus.com
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


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

2015-07-04 Thread Vaclav Petras
On Sat, Jul 4, 2015 at 12:55 PM, Glynn Clements gl...@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 gl...@gclements.plus.com

___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

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

2015-06-30 Thread Vaclav Petras
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 aspect: aaa 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 elevation: bbb doesn't exist.

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

Vaclav
#!/usr/bin/env python

#%module
#% description: Test inputs and outputs in parser
#% keyword: test
#% keyword: raster
#%end
#%option G_OPT_R_INPUT
#% key: elevation
#%end
#%option G_OPT_R_OUTPUT
#% key: aspect
#%end


import grass.script as gscript

def main():
options, flags = gscript.parser()
elevation = options['elevation']
aspect = options['aspect']
print(elevation, aspect)

if __name__ == '__main__':
main()
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev