[GRASS-dev] still problems with overwrite in scripts

I thought from reading past mail posts that -o and --o and --overwrite all set the overwrite flag.

I test for overwrite with grass.overwrite()

But how do I include a check box for the overwrite flag in the GUI specs (for g.parser)?

Putting in an “o” flag does not seem to set the overwrite flag (at least in GRASS 7).
Putting in a “-o” (for creating --o) shows “-” in the interface. Not good.

So how to do this in a python script? I’ve looked at several other python scripts in the main GRASS 7 distribution and can’t find a script that deals with this issue. All seem to ignore setting an overwrite flag in the GUI.

Michael


C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Corporation for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

Michael Barton wrote:

I thought from reading past mail posts that -o and --o and --overwrite
all set the overwrite flag.

The last two do; the first one is just a normal flag.

Historically, modules used -o for overwrite, but that is deprecated in
favour of the --o/--overwrite option which is built into G_parser().

I test for overwrite with grass.overwrite()

But how do I include a check box for the overwrite flag in the GUI specs
(for g.parser)?

If any of the options have a gisprompt setting which starts with
"new", the --help output will display the --o flag and the GUI will
have a checkbox to enable overwrite.

In 7.0, you can force the existence of the overwrite flag by adding
"#% overwrite: yes" to the module options, but this doesn't work in
6.x, partly because it isn't implemented in g.parser and partly
because the underlying logic in G_parser() is broken; see:

  https://trac.osgeo.org/grass/ticket/1658#comment:1

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

Thanks. I'll try one of these methods.

Michael

On Jun 27, 2012, at 5:06 PM, Glynn Clements wrote:

Michael Barton wrote:

I thought from reading past mail posts that -o and --o and --overwrite
all set the overwrite flag.

The last two do; the first one is just a normal flag.

Historically, modules used -o for overwrite, but that is deprecated in
favour of the --o/--overwrite option which is built into G_parser().

I test for overwrite with grass.overwrite()

But how do I include a check box for the overwrite flag in the GUI specs
(for g.parser)?

If any of the options have a gisprompt setting which starts with
"new", the --help output will display the --o flag and the GUI will
have a checkbox to enable overwrite.

In 7.0, you can force the existence of the overwrite flag by adding
"#% overwrite: yes" to the module options, but this doesn't work in
6.x, partly because it isn't implemented in g.parser and partly
because the underlying logic in G_parser() is broken; see:

  #1658 (g.rename, g.copy do not ask for --overwrite, they just do it) – GRASS GIS

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

_____________________
C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Consortium for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

Hmmm. This should have worked. But...

The options (copied from another script) are written as...

#%option G_OPT_R_INPUT
#% key: ms3
#% description: Input raster map for red channel
#%end

So there is no way to add "new" unless I redo the options section. This way of doing the interface is new, but I thought that maybe it is the desired approach for Python now.

I added "#% overwrite: yes" by itself,

as...

#%option
#% overwrite: yes
#%end

and as another line in an existing option section

In all cases, it generated an error.

Suggestions?

Michael

On Jun 27, 2012, at 5:06 PM, Glynn Clements wrote:

Michael Barton wrote:

I thought from reading past mail posts that -o and --o and --overwrite
all set the overwrite flag.

The last two do; the first one is just a normal flag.

Historically, modules used -o for overwrite, but that is deprecated in
favour of the --o/--overwrite option which is built into G_parser().

I test for overwrite with grass.overwrite()

But how do I include a check box for the overwrite flag in the GUI specs
(for g.parser)?

If any of the options have a gisprompt setting which starts with
"new", the --help output will display the --o flag and the GUI will
have a checkbox to enable overwrite.

In 7.0, you can force the existence of the overwrite flag by adding
"#% overwrite: yes" to the module options, but this doesn't work in
6.x, partly because it isn't implemented in g.parser and partly
because the underlying logic in G_parser() is broken; see:

  #1658 (g.rename, g.copy do not ask for --overwrite, they just do it) – GRASS GIS

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

_____________________
C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Consortium for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

going back to your original question, if you use --overwrite
with the master script, it should be set for everything called
by the master script as the GRASS_OVERWRITE enviro var will
be set by the parser. You can test to see if it is set with:

import os

    if "GRASS_OVERWRITE" in os.environ:
        print 'ow is ',os.environ["GRASS_OVERWRITE"]
    else:
        print 'os is not set.'

but if the intention is to have modules started within the
master script to respect it too, you shouldn't have to do anything
as it was already taken care of automatically via the exported
enviro var.

Hamish

Hi,

2012/6/28 Michael Barton <michael.barton@asu.edu>:

#%option G_OPT_R_INPUT
#% key: ms3
#% description: Input raster map for red channel
#%end

So there is no way to add "new" unless I redo the options section.

but this is standard option for input not output! "new" refers to
`gisprompt`. Eg.

#% gisprompt: new,cell,raster

but is added automatically e.g. for G_OPT_R_OUTPUT.

I added "#% overwrite: yes" by itself,

as...

#%option
#% overwrite: yes
#%end

wrong, see my previous answer (just read it carefully)

#%module
...
#% overwrite: yes
#%end

Martin

--
Martin Landa <landa.martin gmail.com> * http://geo.fsv.cvut.cz/~landa

Thanks, but neither are relevant (or work) in my case. See below.

2012/6/26 Michael Barton <Michael.Barton@asu.edu>:

What is the test for the parser based overwrite flag?

as I told you the flag is added by parser automatically when there is
at least one option which is dedicated for output, eg. G_OPT_V_OUTPUT
or G_OPT_F_OUTPUT.

I don't want a pull down list of existing files. I just want a string option so that users can enter a prefix to create 3 output files.

So %# type: string.

Of course this does not automatically add the overwrite flag

Martin

--
Martin Landa <landa.martin gmail.com> * Studijní program Geodézie a kartografie – GeoWikiCZ

My question was about testing for the overwrite flag. But I did try this, as I mentioned in my post to Glynn and get an error wherever I put it. I tried in the string option for output prefix first, of course. But when that didn't work, I tried in the input and pull-down for sharpening algorithm too. No luck anywhere. If it only works in an option specified by G_OPT_R_OUTPUT and G_OPT_V_OUTPUT automatically generates and overwrite flag box without adding it, it seems kind of redundant. Shrug.

I wrote a workaround using an -o flag. But if there is some other way to do this, let me know.

It will help when the g.parser docs are updated from the current BASH centric examples to use G_OPT_R_OUTPUT, G_OPT_V_OUTPUT, and other such function that currently only show up in the C part of the programmer's manual.

Michael

Hi,

2012/6/26 Michael Barton <Michael.Barton@asu.edu>:

Is there some special way to specify an overwrite flag in a script? Or is it
just:

overwrite flag should be added automatically by the parser when the
module produces any output. In really special cases you can force
adding overwrite flag by

#%module
...
#% overwrite: yes
#%end

Martin

--
Martin Landa <landa.martin gmail.com> * Studijní program Geodézie a kartografie – GeoWikiCZ

On Jun 28, 2012, at 1:10 AM, Martin Landa wrote:

Hi,

2012/6/28 Michael Barton <michael.barton@asu.edu>:

#%option G_OPT_R_INPUT
#% key: ms3
#% description: Input raster map for red channel
#%end

So there is no way to add "new" unless I redo the options section.

but this is standard option for input not output! "new" refers to
`gisprompt`. Eg.

#% gisprompt: new,cell,raster

but is added automatically e.g. for G_OPT_R_OUTPUT.

I added "#% overwrite: yes" by itself,

as...

#%option
#% overwrite: yes
#%end

wrong, see my previous answer (just read it carefully)

#%module
...
#% overwrite: yes
#%end

Martin

--
Martin Landa <landa.martin gmail.com> * Studijní program Geodézie a kartografie – GeoWikiCZ

_____________________
C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Corporation for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

Michael Barton wrote:

My question was about testing for the overwrite flag. But I did try
this, as I mentioned in my post to Glynn and get an error wherever I
put it. I tried in the string option for output prefix first, of
course. But when that didn't work, I tried in the input and
pull-down for sharpening algorithm too. No luck anywhere. If it only
works in an option specified by G_OPT_R_OUTPUT and G_OPT_V_OUTPUT
automatically generates and overwrite flag box without adding it, it
seems kind of redundant. Shrug.

The "overwrite" setting goes in the module section (along with
keywords), not the options/flags sections.

In C (this is from r.mapcalc):

    module = G_define_module();
    G_add_keyword(_("raster"));
    G_add_keyword(_("algebra"));
    module->description = _("Raster map calculator.");
    module->overwrite = 1;

In Python (there aren't any scripts which currently require this
feature, so this is contrived):

  #%module
  #% description: This is a script
  #% keywords: script
  #% overwrite: yes
  #%end

It only works in 7.0. In 6.x, g.parser won't recognise the overwrite
setting, and using it in C will force the overwrite setting on due to
bug #1658.

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

Thanks!!!

This is what I needed to know.

Michael

On Jun 28, 2012, at 12:43 PM, Glynn Clements wrote:

Michael Barton wrote:

My question was about testing for the overwrite flag. But I did try
this, as I mentioned in my post to Glynn and get an error wherever I
put it. I tried in the string option for output prefix first, of
course. But when that didn't work, I tried in the input and
pull-down for sharpening algorithm too. No luck anywhere. If it only
works in an option specified by G_OPT_R_OUTPUT and G_OPT_V_OUTPUT
automatically generates and overwrite flag box without adding it, it
seems kind of redundant. Shrug.

The "overwrite" setting goes in the module section (along with
keywords), not the options/flags sections.

In C (this is from r.mapcalc):

   module = G_define_module();
   G_add_keyword(_("raster"));
   G_add_keyword(_("algebra"));
   module->description = _("Raster map calculator.");
   module->overwrite = 1;

In Python (there aren't any scripts which currently require this
feature, so this is contrived):

  #%module
  #% description: This is a script
  #% keywords: script
  #% overwrite: yes
  #%end

It only works in 7.0. In 6.x, g.parser won't recognise the overwrite
setting, and using it in C will force the overwrite setting on due to
bug #1658.

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

_____________________
C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Corporation for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu

Works perfectly.

Michael

On Jun 28, 2012, at 12:43 PM, Glynn Clements wrote:

Michael Barton wrote:

My question was about testing for the overwrite flag. But I did try
this, as I mentioned in my post to Glynn and get an error wherever I
put it. I tried in the string option for output prefix first, of
course. But when that didn't work, I tried in the input and
pull-down for sharpening algorithm too. No luck anywhere. If it only
works in an option specified by G_OPT_R_OUTPUT and G_OPT_V_OUTPUT
automatically generates and overwrite flag box without adding it, it
seems kind of redundant. Shrug.

The "overwrite" setting goes in the module section (along with
keywords), not the options/flags sections.

In C (this is from r.mapcalc):

   module = G_define_module();
   G_add_keyword(_("raster"));
   G_add_keyword(_("algebra"));
   module->description = _("Raster map calculator.");
   module->overwrite = 1;

In Python (there aren't any scripts which currently require this
feature, so this is contrived):

  #%module
  #% description: This is a script
  #% keywords: script
  #% overwrite: yes
  #%end

It only works in 7.0. In 6.x, g.parser won't recognise the overwrite
setting, and using it in C will force the overwrite setting on due to
bug #1658.

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

_____________________
C. Michael Barton
Visiting Scientist, Integrated Science Program
National Center for Atmospheric Research &
University Corporation for Atmospheric Research
303-497-2889 (voice)

Director, Center for Social Dynamics & Complexity
Professor of Anthropology, School of Human Evolution & Social Change
Arizona State University
www: http://www.public.asu.edu/~cmbarton, http://csdc.asu.edu