Discussion:
[GRASSGUI] silly question - running a python script
Michael Barton
2007-11-14 22:56:51 UTC
Permalink
I made a python script (rules_colors.py) , following the template on the
GRASS WIKI

<http://grass.gdf-hannover.de/wiki/GRASS_and_Python>

I can run the script from the command line...

python rules_colors.py

...and it launches a TclTk GUI.


When I try to launch it from within the wxPython GUI, it will not launch and
gives the error...

IOError: Couldn't fetch interface description for command <rules_colors.py>.


Indeed if I try to launch it from the command line with...

python rules_colors.py --interface-description

...it gives me an error and does not recognize the ??interface-description?
flag.



This will need to work correctly if we are to start doing scripts in Python.
Any suggestions?

Mciahel



__________________________________________
Michael Barton, Professor of Anthropology
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University

phone: 480-965-6213
fax: 480-965-7671
www: http://www.public.asu.edu/~cmbarton


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/grass-gui/attachments/20070609/f0855506/attachment.html
Glynn Clements
2007-11-14 22:56:51 UTC
Permalink
Post by Michael Barton
I made a python script (rules_colors.py)
Can we see the script?
Post by Michael Barton
following the template on the GRASS WIKI
<http://grass.gdf-hannover.de/wiki/GRASS_and_Python>
Oh no:

for arg in sys.argv:
args += arg+" "

Jesus H %$&%*ing Christ, has no-one read anything I've said over the
past however-many years about argv[] being an array/list, and not a
string?

os.system("g.parser %s" % (args))

Use os.execve().
Post by Michael Barton
I can run the script from the command line...
python rules_colors.py
...and it launches a TclTk GUI.
You're running the command (which command? r.colors?) with no
arguments. For many commands, this will bring up a Tcl/Tk GUI (unless
GRASS_UI_TERM is set, in which case it will use a Q&A dialogue on the
terminal).

This behaviour is hard-coded into G_parser(). If you want to use a
Python GUI, modify your script to invoke the command with
--interface-description and feed that to the Python GUI.

Or modify G_gui() to optionally invoke a wxPython GUI instead of the
Tcl/Tk one.
Post by Michael Barton
When I try to launch it from within the wxPython GUI, it will not launch and
gives the error...
IOError: Couldn't fetch interface description for command <rules_colors.py>.
Indeed if I try to launch it from the command line with...
python rules_colors.py --interface-description
...it gives me an error and does not recognize the ??interface-description?
flag.
Is the Python interpreter trying to process --interface-description
itself? Does:

python -- rules_colors.py --interface-description

work?

What about:

chmod +x rules_colors.py
./rules_colors.py --interface-description

?

[BTW, please use a sane mail client, at least for mailing lists. If
you have to use a Mac-specific encoding (MacRoman?), at least ensure
that it's labelled as such, and not as ISO-8859-1. Although, that in
itself won't affect the fact that it's decided to convert "--" to an
em-dash. If it has a "smart quotes" option, turn it off.]
--
Glynn Clements <***@gclements.plus.com>
Michael Barton
2007-11-14 22:56:51 UTC
Permalink
Glynn,

I went in to change the WIKI, but don't quite have enough information to
make sure that I'm doing it correctly
Post by Glynn Clements
args += arg+" "
Jesus H %$&%*ing Christ, has no-one read anything I've said over the
past however-many years about argv[] being an array/list, and not a
string?
os.system("g.parser %s" % (args))
Here is what is currently there...

if __name__ == "__main__":
args = ""
for arg in sys.argv:
args += arg+" "

try:
if ( sys.argv[1] != "@ARGS_PARSED@" ):
os.system("g.parser %s " % (args))
except IndexError:
os.system("g.parser %s" % (args))

if sys.argv[1] == "@ARGS_PARSED@":
main();

I found an old email of yours I'd been saving (and couldn't initially find)
that covers part of what needs to be done. Following that, I think it would
change to...

if __name__ == "__main__":
# No need to make a string here, right?
# args = ""
# for arg in sys.argv:
# args += arg+" "

try:
if ( sys.argv[1] != "@ARGS_PARSED@" ):
os.execv("g.parser", [sys.argv[0]] + sys.argv)
except IndexError:
os.execv("g.parser", [sys.argv[0]] + sys.argv)

if sys.argv[1] == "@ARGS_PARSED@":
main();

Is this correct or is it still missing something?

Michael
__________________________________________
Michael Barton, Professor of Anthropology
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University

phone: 480-965-6213
fax: 480-965-7671
www: http://www.public.asu.edu/~cmbarton
Glynn Clements
2007-11-14 22:56:51 UTC
Permalink
Post by Michael Barton
Here is what is currently there...
args = ""
args += arg+" "
os.system("g.parser %s " % (args))
os.system("g.parser %s" % (args))
main();
I found an old email of yours I'd been saving (and couldn't initially find)
that covers part of what needs to be done. Following that, I think it would
change to...
# No need to make a string here, right?
# args = ""
# args += arg+" "
os.execv("g.parser", [sys.argv[0]] + sys.argv)
os.execv("g.parser", [sys.argv[0]] + sys.argv)
main();
Is this correct or is it still missing something?
That looks about right.

Although the "try ... except IndexError" can be replaced with a length
check on sys.argv, i.e.:

if __name__ == "__main__":
if ( len(sys.argv) > 1 && sys.argv[1] != "@ARGS_PARSED@" ):
os.execv("g.parser", [sys.argv[0]] + sys.argv)
else:
main()
--
Glynn Clements <***@gclements.plus.com>
Glynn Clements
2007-11-14 22:56:51 UTC
Permalink
Post by Glynn Clements
Although the "try ... except IndexError" can be replaced with a length
os.execv("g.parser", [sys.argv[0]] + sys.argv)
main()
Correction:

if __name__ == "__main__":
if ( len(sys.argv) <= 1 or sys.argv[1] != "@ARGS_PARSED@" ):
os.execvp("g.parser", [sys.argv[0]] + sys.argv)
else:
main();
--
Glynn Clements <***@gclements.plus.com>
Glynn Clements
2007-11-14 22:56:52 UTC
Permalink
Post by Glynn Clements
args += arg+" "
I have updated the g.parser help page Python example with the fixed args
usage, but I notice the Perl example there (6.3cvs version only) is
broken as well.
http://freegis.org/cgi-bin/viewcvs.cgi/grass6/general/g.parser/description.html
my $arg = "";
$arg .= " $ARGV[$i] ";
}
system("$ENV{GISBASE}/bin/g.parser $0 $arg");
exit;
}
Perl's system() supports both scalar and array context, so the array
version should be used. Also, Perl has exec(), which is preferable to
system()+exit().
Also I notice a difference in the python version where main(): now ends
with "return". Does this matter?
A "return" at the end of a method should be a no-op.
--
Glynn Clements <***@gclements.plus.com>
Michael Barton
2007-11-14 22:56:51 UTC
Permalink
I forgot, to send the script. Here it is. Doesn't work and I haven't updated
main to use a list. But maybe a starting place for figuring out how to do
scripting with python and GRASS.

Michael
Post by Glynn Clements
Post by Michael Barton
I made a python script (rules_colors.py)
Can we see the script?
Post by Michael Barton
following the template on the GRASS WIKI
<http://grass.gdf-hannover.de/wiki/GRASS_and_Python>
args += arg+" "
Jesus H %$&%*ing Christ, has no-one read anything I've said over the
past however-many years about argv[] being an array/list, and not a
string?
os.system("g.parser %s" % (args))
Use os.execve().
Post by Michael Barton
I can run the script from the command line...
python rules_colors.py
...and it launches a TclTk GUI.
You're running the command (which command? r.colors?) with no
arguments. For many commands, this will bring up a Tcl/Tk GUI (unless
GRASS_UI_TERM is set, in which case it will use a Q&A dialogue on the
terminal).
This behaviour is hard-coded into G_parser(). If you want to use a
Python GUI, modify your script to invoke the command with
--interface-description and feed that to the Python GUI.
Or modify G_gui() to optionally invoke a wxPython GUI instead of the
Tcl/Tk one.
Post by Michael Barton
When I try to launch it from within the wxPython GUI, it will not launch and
gives the error...
IOError: Couldn't fetch interface description for command <rules_colors.py>.
Indeed if I try to launch it from the command line with...
python rules_colors.py --interface-description
...it gives me an error and does not recognize the ??interface-description?
flag.
Is the Python interpreter trying to process --interface-description
python -- rules_colors.py --interface-description
work?
chmod +x rules_colors.py
./rules_colors.py --interface-description
?
[BTW, please use a sane mail client, at least for mailing lists. If
you have to use a Mac-specific encoding (MacRoman?), at least ensure
that it's labelled as such, and not as ISO-8859-1. Although, that in
itself won't affect the fact that it's decided to convert "--" to an
em-dash. If it has a "smart quotes" option, turn it off.]
__________________________________________
Michael Barton, Professor of Anthropology
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University

phone: 480-965-6213
fax: 480-965-7671
www: http://www.public.asu.edu/~cmbarton


-------------- next part --------------
#!/usr/bin/env python

import sys
import os
import wx
import rules


#
############################################################################
#
# MODULE: rules_colors.py
# AUTHOR(S): Michael Barton
# PURPOSE: Permit use of color rules in r.colors from wxgrass GUI
# COPYRIGHT: (C) 2007 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################


#%Module
#% description: Use rules to set colors for raster map
#%End
#%option
#% key: map
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of raster map for color management
#% required : yes
#%end
#%Option
#% key: file
#% type: string
#% required: no
#% multiple: no
#% description: Name of rules file (if not given user is prompted for interactive entry)
#% gisprompt: old_file,file,input
#%End


def main():


if os.getenv("GIS_OPT_FILE"):
# get from file
os.popen('r.colors map=%s color=%s rules=%s' % (os.getenv('GIS_OPT_MAP'),'rules',os.getenv('GIS_OPT_FILE')))

else:
dlg = rules.RulesText(self,id=wx.ID_ANY, title="Enter color rules",
pos=wx.DefaultPosition, size=wx.DefaultSize)

dlg.CenterOnScreen()

# if OK button pressed in decoration control dialog
if dlg.ShowModal() == wx.ID_OK:
colorrules = dlg.rules

# run r.colors with rules
os.popen('r.colors map=%s color=%s rules=%s' % (os.getenv('GIS_OPT_MAP'),'rules',colorrules))


dlg.Destroy()

#end of your code
return

if __name__ == "__main__":
# if not os.getenv("GISBASE"):
# print "You must be in GRASS GIS to run this program"
#
args = ""
for arg in sys.argv:
args += arg+" "
try:
if ( sys.argv[1] != "@ARGS_PARSED@" ):
os.system("g.parser %s " % (args))
except IndexError:
os.system("g.parser %s" % (args))

if sys.argv[1] == "@ARGS_PARSED@":
main();
Glynn Clements
2007-11-14 22:56:51 UTC
Permalink
Post by Michael Barton
I forgot, to send the script. Here it is. Doesn't work and I haven't updated
main to use a list. But maybe a starting place for figuring out how to do
scripting with python and GRASS.
If I comment out the "import wx" and "import rules", then both:

python rules_colors.py --interface-description
and:
chmod +x rules_colors.py
./rules_colors.py --interface-description

work fine. Ditto for --help etc.

That indicates that the use of g.parser is okay.

BTW, what's the point of using:

#!/usr/bin/env python

rather than:

#!/usr/bin/python

?

AFAICT, using env in this way is a no-op.
--
Glynn Clements <***@gclements.plus.com>
Michael Barton
2007-11-14 22:56:51 UTC
Permalink
I forgot, to send the script. Here it is. Doesn't work and I haven't updated
main to use a list. But maybe a starting place for figuring out how to do
scripting with python and GRASS.

Michael
Post by Glynn Clements
Post by Michael Barton
I made a python script (rules_colors.py)
Can we see the script?
Post by Michael Barton
following the template on the GRASS WIKI
<http://grass.gdf-hannover.de/wiki/GRASS_and_Python>
args += arg+" "
Jesus H %$&%*ing Christ, has no-one read anything I've said over the
past however-many years about argv[] being an array/list, and not a
string?
os.system("g.parser %s" % (args))
Use os.execve().
Post by Michael Barton
I can run the script from the command line...
python rules_colors.py
...and it launches a TclTk GUI.
You're running the command (which command? r.colors?) with no
arguments. For many commands, this will bring up a Tcl/Tk GUI (unless
GRASS_UI_TERM is set, in which case it will use a Q&A dialogue on the
terminal).
This behaviour is hard-coded into G_parser(). If you want to use a
Python GUI, modify your script to invoke the command with
--interface-description and feed that to the Python GUI.
Or modify G_gui() to optionally invoke a wxPython GUI instead of the
Tcl/Tk one.
Post by Michael Barton
When I try to launch it from within the wxPython GUI, it will not launch and
gives the error...
IOError: Couldn't fetch interface description for command <rules_colors.py>.
Indeed if I try to launch it from the command line with...
python rules_colors.py --interface-description
...it gives me an error and does not recognize the ??interface-description?
flag.
Is the Python interpreter trying to process --interface-description
python -- rules_colors.py --interface-description
work?
chmod +x rules_colors.py
./rules_colors.py --interface-description
?
[BTW, please use a sane mail client, at least for mailing lists. If
you have to use a Mac-specific encoding (MacRoman?), at least ensure
that it's labelled as such, and not as ISO-8859-1. Although, that in
itself won't affect the fact that it's decided to convert "--" to an
em-dash. If it has a "smart quotes" option, turn it off.]
__________________________________________
Michael Barton, Professor of Anthropology
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University

phone: 480-965-6213
fax: 480-965-7671
www: http://www.public.asu.edu/~cmbarton


-------------- next part --------------
#!/usr/bin/env python

import sys
import os
import wx
import rules


#
############################################################################
#
# MODULE: rules_colors.py
# AUTHOR(S): Michael Barton
# PURPOSE: Permit use of color rules in r.colors from wxgrass GUI
# COPYRIGHT: (C) 2007 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################


#%Module
#% description: Use rules to set colors for raster map
#%End
#%option
#% key: map
#% type: string
#% gisprompt: old,cell,raster
#% description: Name of raster map for color management
#% required : yes
#%end
#%Option
#% key: file
#% type: string
#% required: no
#% multiple: no
#% description: Name of rules file (if not given user is prompted for interactive entry)
#% gisprompt: old_file,file,input
#%End


def main():


if os.getenv("GIS_OPT_FILE"):
# get from file
os.popen('r.colors map=%s color=%s rules=%s' % (os.getenv('GIS_OPT_MAP'),'rules',os.getenv('GIS_OPT_FILE')))

else:
dlg = rules.RulesText(self,id=wx.ID_ANY, title="Enter color rules",
pos=wx.DefaultPosition, size=wx.DefaultSize)

dlg.CenterOnScreen()

# if OK button pressed in decoration control dialog
if dlg.ShowModal() == wx.ID_OK:
colorrules = dlg.rules

# run r.colors with rules
os.popen('r.colors map=%s color=%s rules=%s' % (os.getenv('GIS_OPT_MAP'),'rules',colorrules))


dlg.Destroy()

#end of your code
return

if __name__ == "__main__":
# if not os.getenv("GISBASE"):
# print "You must be in GRASS GIS to run this program"
#
args = ""
for arg in sys.argv:
args += arg+" "
try:
if ( sys.argv[1] != "@ARGS_PARSED@" ):
os.system("g.parser %s " % (args))
except IndexError:
os.system("g.parser %s" % (args))

if sys.argv[1] == "@ARGS_PARSED@":
main();
Michael Barton
2007-11-14 22:56:51 UTC
Permalink
OK. I've changed the WIKI accordingly.

Michael
Post by Glynn Clements
Post by Glynn Clements
Although the "try ... except IndexError" can be replaced with a length
os.execv("g.parser", [sys.argv[0]] + sys.argv)
main()
os.execvp("g.parser", [sys.argv[0]] + sys.argv)
main();
__________________________________________
Michael Barton, Professor of Anthropology
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University

phone: 480-965-6213
fax: 480-965-7671
www: http://www.public.asu.edu/~cmbarton
Michael Barton
2007-11-14 22:56:51 UTC
Permalink
Post by Michael Barton
Post by Michael Barton
I forgot, to send the script. Here it is. Doesn't work and I haven't updated
main to use a list. But maybe a starting place for figuring out how to do
scripting with python and GRASS.
python rules_colors.py --interface-description
chmod +x rules_colors.py
./rules_colors.py --interface-description
work fine. Ditto for --help etc.
I needed import wx and import rules to launch an interactive window in
wxPython. I wonder why they cause problems.
Post by Michael Barton
That indicates that the use of g.parser is okay.
#!/usr/bin/env python
#!/usr/bin/python
It's that way in the modules in the svn. In the past, I've had trouble with
the Python shbang on my Mac and so can't really tell which is better. My
Python is in /usr/bin however.
Post by Michael Barton
AFAICT, using env in this way is a no-op.
Michael

__________________________________________
Michael Barton, Professor of Anthropology
School of Human Evolution & Social Change
Center for Social Dynamics & Complexity
Arizona State University

phone: 480-965-6213
fax: 480-965-7671
www: http://www.public.asu.edu/~cmbarton
Glynn Clements
2007-11-14 22:56:51 UTC
Permalink
Post by Michael Barton
Post by Michael Barton
Post by Michael Barton
I forgot, to send the script. Here it is. Doesn't work and I haven't updated
main to use a list. But maybe a starting place for figuring out how to do
scripting with python and GRASS.
python rules_colors.py --interface-description
chmod +x rules_colors.py
./rules_colors.py --interface-description
work fine. Ditto for --help etc.
I needed import wx and import rules to launch an interactive window in
wxPython. I wonder why they cause problems.
I didn't say that they caused problems; I was treating the script as a
normal GRASS script (rather than a wxgrass script), and removing the
imports was simpler than setting up the operating environment to allow
those modules to be imported.
Post by Michael Barton
Post by Michael Barton
That indicates that the use of g.parser is okay.
#!/usr/bin/env python
#!/usr/bin/python
It's that way in the modules in the svn. In the past, I've had trouble with
the Python shbang on my Mac and so can't really tell which is better. My
Python is in /usr/bin however.
Calling Python directly is definitely better. If #!/usr/bin/python
doesn't work, you need to fix your system; other Python scripts aren't
going to use that workaround.
--
Glynn Clements <***@gclements.plus.com>
Continue reading on narkive:
Loading...