[GRASS-dev] Default content for the "answer" to G_OPT_F_INPUT

Dear developers,

is there a way to set a string as the content of a default "answer" for
the G_OPT_F_INPUT option? I am trying to identify potential cases in the
grass-addons repository, but I am not sure I have seen something related.

The G_OPT_F_INPUT offers a field to enter a filename. The default
"answer", for the parser, will be the name of the file.

In addition, a field to "enter values directly" is created. The
description of this field is: "Enter file content directly instead of
specifying a file. Temporary file will be automatically created."

Can this field be filled with a pre-defined content?
The use case is to expose, to the user, a pre-defined set of
reclassification rules. These will help a beginner to get started,
understand easier and modify rules as he sees fit.

Thank you, Nikos

On 17/04/18 15:31, Nikos Alexandris wrote:

Dear developers,

is there a way to set a string as the content of a default "answer" for
the G_OPT_F_INPUT option? I am trying to identify potential cases in the
grass-addons repository, but I am not sure I have seen something related.

The G_OPT_F_INPUT offers a field to enter a filename. The default
"answer", for the parser, will be the name of the file.

In addition, a field to "enter values directly" is created. The
description of this field is: "Enter file content directly instead of
specifying a file. Temporary file will be automatically created."

Can this field be filled with a pre-defined content?
The use case is to expose, to the user, a pre-defined set of
reclassification rules. These will help a beginner to get started,
understand easier and modify rules as he sees fit.

Use the 'answer' parameter:

#% answer : myfilename

Moritz

* Moritz Lennert <mlennert@club.worldonline.be> [2018-04-17 15:56:10 +0200]:

On 17/04/18 15:31, Nikos Alexandris wrote:

Dear developers,

is there a way to set a string as the content of a default "answer" for
the G_OPT_F_INPUT option? I am trying to identify potential cases in the
grass-addons repository, but I am not sure I have seen something related.

The G_OPT_F_INPUT offers a field to enter a filename. The default
"answer", for the parser, will be the name of the file.

In addition, a field to "enter values directly" is created. The
description of this field is: "Enter file content directly instead of
specifying a file. Temporary file will be automatically created."

Can this field be filled with a pre-defined content?
The use case is to expose, to the user, a pre-defined set of
reclassification rules. These will help a beginner to get started,
understand easier and modify rules as he sees fit.

Use the 'answer' parameter:

#% answer : myfilename

Moritz

It works: it loads the content of an external file. Are we forced to use
external files in such a case? Which means an extra file has to be
shipped with the source code.

Can we not use an in-script hardcoded string for example?

Thank you Moritz, Nikos

On Tue, Apr 17, 2018 at 4:21 PM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

On 17/04/18 15:31, Nikos Alexandris wrote:

Dear developers,

is there a way to set a string as the content of a default “answer” for
the G_OPT_F_INPUT option? I am trying to identify potential cases in the
grass-addons repository, but I am not sure I have seen something related.

The G_OPT_F_INPUT offers a field to enter a filename. The default
“answer”, for the parser, will be the name of the file.

In addition, a field to “enter values directly” is created. The
description of this field is: “Enter file content directly instead of
specifying a file. Temporary file will be automatically created.”

Can this field be filled with a pre-defined content?
The use case is to expose, to the user, a pre-defined set of
reclassification rules. These will help a beginner to get started,
understand easier and modify rules as he sees fit.

Use the ‘answer’ parameter:

#% answer : myfilename

Moritz

It works: it loads the content of an external file. Are we forced to use
external files in such a case? Which means an extra file has to be
shipped with the source code.
Can we not use an in-script hardcoded string for example?

Be aware that you can’t use a hardcoded full path, because depending on the installation/user/os, that path may or may not exist. If you use a relative path, that path is relative to the current working directory which is of course not constant between sessions/installation/user/os. I’m afraid a default answer to G_OPT_F_INPUT will cause a lot of trouble.

my2c

Markus M

Thank you Moritz, Nikos


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

On 17/04/18 16:21, Nikos Alexandris wrote:

* Moritz Lennert <mlennert@club.worldonline.be> [2018-04-17 15:56:10 +0200]:

On 17/04/18 15:31, Nikos Alexandris wrote:

Dear developers,

is there a way to set a string as the content of a default "answer" for
the G_OPT_F_INPUT option? I am trying to identify potential cases in the
grass-addons repository, but I am not sure I have seen something related.

The G_OPT_F_INPUT offers a field to enter a filename. The default
"answer", for the parser, will be the name of the file.

In addition, a field to "enter values directly" is created. The
description of this field is: "Enter file content directly instead of
specifying a file. Temporary file will be automatically created."

Can this field be filled with a pre-defined content?
The use case is to expose, to the user, a pre-defined set of
reclassification rules. These will help a beginner to get started,
understand easier and modify rules as he sees fit.

Use the 'answer' parameter:

#% answer : myfilename

Moritz

It works: it loads the content of an external file. Are we forced to use
external files in such a case? Which means an extra file has to be
shipped with the source code.

Can we not use an in-script hardcoded string for example?

Sure you can.

Use write_command() with stdin=text that you wish to feed into r.reclass. You can find several examples of its usage in existing scripts.

Moritz

P.S. Thanks to MarkusM for raising the point about file path difficulties...

Le Mardi 17 avril 2018, Markus Metz a écrit :

On Tue, Apr 17, 2018 at 4:21 PM, Nikos Alexandris <nik@nikosalexandris.net>
wrote:
>
> * Moritz Lennert <mlennert@club.worldonline.be> [2018-04-17 15:56:10
+0200]:
>
>> On 17/04/18 15:31, Nikos Alexandris wrote:
>>>
>>> Dear developers,
>>>
>>> is there a way to set a string as the content of a default "answer" for
>>> the G_OPT_F_INPUT option? I am trying to identify potential cases in the
>>> grass-addons repository, but I am not sure I have seen something
related.
>>>
>>> The G_OPT_F_INPUT offers a field to enter a filename. The default
>>> "answer", for the parser, will be the name of the file.
>>>
>>> In addition, a field to "enter values directly" is created. The
>>> description of this field is: "Enter file content directly instead of
>>> specifying a file. Temporary file will be automatically created."
>>>
>>> Can this field be filled with a pre-defined content?
>>> The use case is to expose, to the user, a pre-defined set of
>>> reclassification rules. These will help a beginner to get started,
>>> understand easier and modify rules as he sees fit.
>>
>>
>> Use the 'answer' parameter:
>>
>> #% answer : myfilename
>>
>> Moritz
>
>
> It works: it loads the content of an external file. Are we forced to use
> external files in such a case? Which means an extra file has to be
> shipped with the source code.
> Can we not use an in-script hardcoded string for example?

Be aware that you can't use a hardcoded full path, because depending on the
installation/user/os, that path may or may not exist. If you use a relative
path, that path is relative to the current working directory which is of
course not constant between sessions/installation/user/os. I'm afraid a
default answer to G_OPT_F_INPUT will cause a lot of trouble.

my2c

Markus M

Ciao Markus.

Yet my intention was/is to use a hardcoded string of rules. Not a path to a file.

It appears the only option is to document, the rules, and suggest a copy and paste in to the corresponding field.

Can the parser be extended in this regard with yet another option? Perhaps I missed some existing option (?).

Thank you Markus, Nikos

NikosAlexandris wrote

Yet my intention was/is to use a hardcoded string of rules. Not a path to
a file.

It appears the only option is to document, the rules, and suggest a copy
and paste in to the corresponding field.

Can the parser be extended in this regard with yet another option? Perhaps
I missed some existing option (?).

https://trac.osgeo.org/grass/browser/grass-addons/grass7/vector/v.habitat.dem/v.habitat.dem.py#L568

in this script I write out a text file for a predefined color rule and later
on

https://trac.osgeo.org/grass/browser/grass-addons/grass7/vector/v.habitat.dem/v.habitat.dem.py#L579

I use it. it's an ugly hack, maybe you can use it without writing a text
file.

-----
best regards
Helmut
--
Sent from: http://osgeo-org.1560.x6.nabble.com/Grass-Dev-f3991897.html

Hi Nikos,

I’m sorry if my comment is out of topic as I’m not following the whole discussion, but can you just hardcode the values in the code and allow them to be overridden by the user-specified file? That is much simpler (for both user and GRASS) than handling paths and if the file is short, it does not raise major code cleanness concern. If people need to edit the file, then documentation is the appropriate place.

Vaclav

···

On Tue, Apr 17, 2018 at 10:32 AM, Nikos Alexandris <nik@nikosalexandris.net> wrote:

Le Mardi 17 avril 2018, Markus Metz a écrit :

On Tue, Apr 17, 2018 at 4:21 PM, Nikos Alexandris <nik@nikosalexandris.net>
wrote:

On 17/04/18 15:31, Nikos Alexandris wrote:

Dear developers,

is there a way to set a string as the content of a default “answer” for
the G_OPT_F_INPUT option? I am trying to identify potential cases in the
grass-addons repository, but I am not sure I have seen something
related.

The G_OPT_F_INPUT offers a field to enter a filename. The default
“answer”, for the parser, will be the name of the file.

In addition, a field to “enter values directly” is created. The
description of this field is: “Enter file content directly instead of
specifying a file. Temporary file will be automatically created.”

Can this field be filled with a pre-defined content?
The use case is to expose, to the user, a pre-defined set of
reclassification rules. These will help a beginner to get started,
understand easier and modify rules as he sees fit.

Use the ‘answer’ parameter:

#% answer : myfilename

Moritz

It works: it loads the content of an external file. Are we forced to use
external files in such a case? Which means an extra file has to be
shipped with the source code.
Can we not use an in-script hardcoded string for example?

Be aware that you can’t use a hardcoded full path, because depending on the
installation/user/os, that path may or may not exist. If you use a relative
path, that path is relative to the current working directory which is of
course not constant between sessions/installation/user/os. I’m afraid a
default answer to G_OPT_F_INPUT will cause a lot of trouble.

my2c

Markus M

Ciao Markus.

Yet my intention was/is to use a hardcoded string of rules. Not a path to a file.

It appears the only option is to document, the rules, and suggest a copy and paste in to the corresponding field.

Can the parser be extended in this regard with yet another option? Perhaps I missed some existing option (?).

Thank you Markus, Nikos


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

Vaclav:

Hi Nikos,

I'm sorry if my comment is out of topic as I'm not following the whole
discussion, but can you just hardcode the values in the code and allow them
to be overridden by the user-specified file? That is much simpler (for both
user and GRASS) than handling paths and if the file is short, it does not
raise major code cleanness concern. If people need to edit the file, then
documentation is the appropriate place.

Hi Vaclav,

I can do what you suggest. It is not, however, what I want to do.

By referring to "a string as the content of a default answer for
G_OPT_F_INPUT" I mean exactly this: the content of the answer. The answer is a
file name. It's content is a string.

Example: one want to use a GRASS GIS module, say through QGIS. If one
sees an example set of rules in the corresponding box, right away when
launching the GUI (or r.SomeModule --ui), it is easier to grasp the syntax and
successively the logic of whatever is shown.

I can't figure out a way in Python on how to set this content without
using an external file.

Would you devs think it makes sense to request, via a
ticket, an option for the parser to fill in, in addition to the existing
"answer" to G_OPT_F_INPUT (which will fill in a file name, and if the
file exists, its content will be printed in the larger box below it), an
in-script hardcoded string as the content that will be "loaded" in this
larger box?

Maybe name it "content" or "rules" or "filecontent" or something
similar?

Nikos

[rest deleted]

On 18/04/18 10:50, Nikos Alexandris wrote:

Vaclav:

Hi Nikos,

I'm sorry if my comment is out of topic as I'm not following the whole
discussion, but can you just hardcode the values in the code and allow them
to be overridden by the user-specified file? That is much simpler (for both
user and GRASS) than handling paths and if the file is short, it does not
raise major code cleanness concern. If people need to edit the file, then
documentation is the appropriate place.

Hi Vaclav,

I can do what you suggest. It is not, however, what I want to do.

By referring to "a string as the content of a default answer for
G_OPT_F_INPUT" I mean exactly this: the content of the answer. The answer is a
file name. It's content is a string.

Example: one want to use a GRASS GIS module, say through QGIS. If one
sees an example set of rules in the corresponding box, right away when
launching the GUI (or r.SomeModule --ui), it is easier to grasp the syntax and
successively the logic of whatever is shown.

I can't figure out a way in Python on how to set this content without
using an external file.

Would you devs think it makes sense to request, via a
ticket, an option for the parser to fill in, in addition to the existing
"answer" to G_OPT_F_INPUT (which will fill in a file name, and if the
file exists, its content will be printed in the larger box below it), an
in-script hardcoded string as the content that will be "loaded" in this
larger box?

Maybe name it "content" or "rules" or "filecontent" or something
similar?

IIUC, what you would like to be able to do is to prefill the text box that you have, for example, in in the r.reclass GUI under "or enter values directly" (see attached screenshot) ?

Moritz

(attachments)

2018-04-18-111235_636x382_scrot.png

* Helmut Kudrnovsky <hellik@web.de> [2018-04-17 07:40:59 -0700]:

NikosAlexandris wrote

Yet my intention was/is to use a hardcoded string of rules. Not a path to
a file.

It appears the only option is to document, the rules, and suggest a copy
and paste in to the corresponding field.

Can the parser be extended in this regard with yet another option? Perhaps
I missed some existing option (?).

https://trac.osgeo.org/grass/browser/grass-addons/grass7/vector/v.habitat.dem/v.habitat.dem.py#L568

in this script I write out a text file for a predefined color rule and later
on

https://trac.osgeo.org/grass/browser/grass-addons/grass7/vector/v.habitat.dem/v.habitat.dem.py#L579

I use it. it's an ugly hack, maybe you can use it without writing a text
file.

Helmut,

do you need to write out the rules to a file? If not, you may do
something like:

# set rules as a string

POTENTIAL_COLORS = """ # Cubehelix color table generated using:
# r.colors.cubehelix -dn ncolors=3 map=recreation_potential nrotations=0.33 gamma=1.5 hue=0.9 dark=0.3 output=recreation_potential.colors
0.000% 55:29:66
33.333% 55:29:66
33.333% 157:85:132
66.667% 157:85:132
66.667% 235:184:193
100.000% 235:184:193"""

# shortcuts
from grass.pygrass.modules.shortcuts import raster as r

# (re-)use later in the script
r.colors(map=recreation_potential, rules='-', stdin = POTENTIAL_COLORS)

Nikos

* Moritz Lennert <mlennert@club.worldonline.be> [2018-04-18 11:13:53 +0200]:

On 18/04/18 10:50, Nikos Alexandris wrote:

Vaclav:

Hi Nikos,

I'm sorry if my comment is out of topic as I'm not following the whole
discussion, but can you just hardcode the values in the code and allow them
to be overridden by the user-specified file? That is much simpler (for both
user and GRASS) than handling paths and if the file is short, it does not
raise major code cleanness concern. If people need to edit the file, then
documentation is the appropriate place.

Hi Vaclav,

I can do what you suggest. It is not, however, what I want to do.

By referring to "a string as the content of a default answer for
G_OPT_F_INPUT" I mean exactly this: the content of the answer. The answer is a
file name. It's content is a string.

Example: one want to use a GRASS GIS module, say through QGIS. If one
sees an example set of rules in the corresponding box, right away when
launching the GUI (or r.SomeModule --ui), it is easier to grasp the syntax and
successively the logic of whatever is shown.

I can't figure out a way in Python on how to set this content without
using an external file.

Would you devs think it makes sense to request, via a
ticket, an option for the parser to fill in, in addition to the existing
"answer" to G_OPT_F_INPUT (which will fill in a file name, and if the
file exists, its content will be printed in the larger box below it), an
in-script hardcoded string as the content that will be "loaded" in this
larger box?

Maybe name it "content" or "rules" or "filecontent" or something
similar?

IIUC, what you would like to be able to do is to prefill the text box that you have, for example, in in the r.reclass GUI under "or enter values directly" (see attached screenshot) ?

Correct (first post, last sentence -- apologies if it was not clear
enough). It will enable a better user-experience, the way I understand
GRASS GIS.

Nikos

Moritz

--
Nikos Alexandris | Remote Sensing & Geomatics
GPG Key Fingerprint 6F9D4506F3CA28380974D31A9053534B693C4FB3