[GRASSGUI] silly question - running a python script

Michael Barton Michael.Barton at asu.edu
Sun Jun 10 14:53:17 EDT 2007


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




On 6/10/07 6:57 AM, "Glynn Clements" <glynn at gclements.plus.com> wrote:

> 
> Michael Barton wrote:
> 
>> I made a python script (rules_colors.py)
> 
> Can we see the script?
> 
>> 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().
> 
>> 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.
> 
>> 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.]

__________________________________________
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();



More information about the grass-gui mailing list