silly question - running a python script

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

silly question - running a python script

Michael Barton
silly question - running a python script 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


_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: silly question - running a python script

Glynn Clements

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.]

--
Glynn Clements <[hidden email]>

_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: silly question - running a python script

Michael Barton
Glynn,

I went in to change the WIKI, but don't quite have enough information to
make sure that I'm doing it correctly


On 6/10/07 6:57 AM, "Glynn Clements" <[hidden email]> wrote:

> 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))


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


_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: silly question - running a python script

Michael Barton
In reply to this post by Glynn Clements
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" <[hidden email]> 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



_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui

rules_colors.py (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: silly question - running a python script

Glynn Clements
In reply to this post by Michael Barton

Michael Barton wrote:

> 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?

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 <[hidden email]>

_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: silly question - running a python script

Glynn Clements
In reply to this post by Michael Barton

Michael Barton wrote:

> 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 <[hidden email]>

_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: [GRASS-dev] Re: silly question - running a python script

Glynn Clements
In reply to this post by Glynn Clements

Glynn Clements wrote:

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

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 <[hidden email]>

_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: [GRASS-dev] Re: silly question - running a python script

Michael Barton
OK. I've changed the WIKI accordingly.

Michael


On 6/10/07 1:46 PM, "Glynn Clements" <[hidden email]> wrote:

>
> Glynn Clements wrote:
>
>> 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()
>
> 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();

__________________________________________
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


_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: silly question - running a python script

Michael Barton
In reply to this post by Glynn Clements



On 6/10/07 1:36 PM, "Glynn Clements" <[hidden email]> wrote:

>
> Michael Barton wrote:
>
>> 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.

I needed import wx and import rules to launch an interactive window in
wxPython. I wonder why they cause problems.


>
> 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
>

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.

>
> 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


_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: silly question - running a python script

Glynn Clements

Michael Barton wrote:

> >> 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.
>
> 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.

> > 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
>
> 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 <[hidden email]>

_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui
Reply | Threaded
Open this post in threaded view
|

Re: [GRASS-dev] Re: silly question - running a python script

Glynn Clements
In reply to this post by Michael Barton

Hamish wrote:

> > > Oh no:
> > >
> > >     for arg in sys.argv:
> > >         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
>
> if( $ARGV[0] ne '@ARGS_PARSED@' ){
>     my $arg = "";
>     for (my $i=0; $i < @ARGV;$i++) {
>         $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 <[hidden email]>

_______________________________________________
grassgui mailing list
[hidden email]
http://grass.itc.it/mailman/listinfo/grassgui