Discussion:
[GRASSGUI] commands from menus and database table not working
Jachym Cepicky
2007-11-14 22:56:50 UTC
Permalink
hi,

after some weeks, I'm back from the middle of nowhere and I tried some
new features of your wx gui.

It looks good, however, no commands are working now:

1 - start the gui
wxgrass &
2 - Select Raster-> Reports and statistics -> Report basic file
information

3 - Fill the form

4 - Hint "Run" button:

Result:

Traceback (most recent call last):
File "/usr/src/gis/grass/addons/gui/gui_modules/menuform.py", line
619, in OnRun
self.goutput.runCmd(cmd)
File "/usr/src/gis/grass/addons/gui/gui_modules/wxgui_utils.py", line
922, in runCmd
self.cmd_output.write("$ " + command + "\n")
TypeError: cannot concatenate 'str' and 'list' objects

Why:

method getCmd() of grassTask() class (gui_modules/menform.py, line 215)
returns not command string (e.g. "r.info elevation.dem"), but already
command list (e.g. ["r.info", "map=elevation.dem"]).

This is in conflict with rest of the code, where ever cmd.split(" ") is
called (e.g. wxgui.py line 410 or gui_modules/wxgui_utils.py, line 868).

So, what to do? Fix getCmd(), so it returns string or go through whole
code and try to fix all cmd.split() calls?

Jachym
--
Jachym Cepicky
e-mail: ***@gmail.com
URL: http://les-ejk.cz
GPG: http://www.les-ejk.cz/pgp/jachym_cepicky-gpg.pub
Michael Barton
2007-11-14 22:56:50 UTC
Permalink
Jachym,

Yet another casualty of the switch from command strings to command lists.
Glynn made a change here that will be better for all in the end. He tried to
catch instances of commands as strings and convert them to lists, but didn't
get them all (there were a lot of places). But we've almost got it done.
Specific comments below.
Post by Jachym Cepicky
hi,
after some weeks, I'm back from the middle of nowhere and I tried some
new features of your wx gui.
1 - start the gui
wxgrass &
2 - Select Raster-> Reports and statistics -> Report basic file
information
3 - Fill the form
File "/usr/src/gis/grass/addons/gui/gui_modules/menuform.py", line
619, in OnRun
self.goutput.runCmd(cmd)
File "/usr/src/gis/grass/addons/gui/gui_modules/wxgui_utils.py", line
922, in runCmd
self.cmd_output.write("$ " + command + "\n")
TypeError: cannot concatenate 'str' and 'list' objects
The problem is not command but "$ " and "\n". They is a strings. Change to

['$ '] + command + ['\n']
Post by Jachym Cepicky
method getCmd() of grassTask() class (gui_modules/menform.py, line 215)
returns not command string (e.g. "r.info elevation.dem"), but already
command list (e.g. ["r.info", "map=elevation.dem"]).
This is in conflict with rest of the code, where ever cmd.split(" ") is
called (e.g. wxgui.py line 410 or gui_modules/wxgui_utils.py, line 868).
cmd.split(" ") is potentially problematic and we should avoid it. For
example, <some command> map="My map" will end up as

['<command>', 'map=My', 'map'].

Or <command> query="attribute1>5 and attribute2<15"

['<command>', 'query=attribute>5', 'and', 'attribute2<15']
Post by Jachym Cepicky
So, what to do? Fix getCmd(), so it returns string or go through whole
code and try to fix all cmd.split() calls?
The latter. We've already got most of them, but there are a few left
apparently.

Michael
Post by Jachym Cepicky
Jachym
__________________________________________
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:50 UTC
Permalink
Post by Michael Barton
Post by Jachym Cepicky
self.cmd_output.write("$ " + command + "\n")
TypeError: cannot concatenate 'str' and 'list' objects
The problem is not command but "$ " and "\n". They is a strings. Change to
['$ '] + command + ['\n']
That will produce a list; does write() accept a list?

But the default string conversion performed by %s probably isn't what
you want. From the leading "$", I'm guessing that you want output in
shell syntax, which is going to need some form of quoting.

OTOH, you could just use Python syntax. Generating Bourne-shell syntax
is arguably wrong for something that's supposed to be cross-platform.
Post by Michael Barton
Post by Jachym Cepicky
method getCmd() of grassTask() class (gui_modules/menform.py, line 215)
returns not command string (e.g. "r.info elevation.dem"), but already
command list (e.g. ["r.info", "map=elevation.dem"]).
This is in conflict with rest of the code, where ever cmd.split(" ") is
called (e.g. wxgui.py line 410 or gui_modules/wxgui_utils.py, line 868).
cmd.split(" ") is potentially problematic and we should avoid it. For
example, <some command> map="My map" will end up as
['<command>', 'map=My', 'map'].
It doesn't matter for maps, as spaces aren't valid in map names. But
it matters for arbitrary text (titles, etc), filenames, SQL "where"
clauses etc.
Post by Michael Barton
Or <command> query="attribute1>5 and attribute2<15"
['<command>', 'query=attribute>5', 'and', 'attribute2<15']
Post by Jachym Cepicky
So, what to do? Fix getCmd(), so it returns string or go through whole
code and try to fix all cmd.split() calls?
The latter. We've already got most of them, but there are a few left
apparently.
What is the purpose of getCmd()? If it's meant to return the command
in a form that can be executed within wxgui, it should return a list.

If it's simply a string to show to the user, the format isn't
critical. OTOH, if it's going to be saved to a script, it needs to be
in the appropriate syntax (sh, csh, cmd.exe, etc).

The main point is that, if you have a function which returns a command
as a string, you need to be absolutely crystal clear on what syntax is
used. If it's Bourne-shell syntax, don't try to execute it via cmd.exe
(and vice-versa). If it's just "human-readable string", don't try to
execute it at all.

Depending upon what you want to do, you might need multiple functions
to return commands in different formats. In that case, I would suggest
having class methods always return commands as lists, and have
separate utility functions for converting such lists to strings.
--
Glynn Clements <***@gclements.plus.com>
Daniel Calvelo
2007-11-14 22:56:51 UTC
Permalink
Hi all.

grassTask is list-ready. In menuform.py, the grassTask.getCmd() is
meant to return a list. We discussed this a few months ago. The
quick'n'dirty way of maing this into a command string is to

" ".join( getCmd() )

This is used in e.g. building the statusbar "current command string",
and used to be also in the OnCopy() event handler. wxgui.py and
wxgui_utils.py should be changed to accomodate a list and call
getCmd() directly instead of the join snippet or equivalent.

Daniel.
Post by Glynn Clements
Post by Michael Barton
Post by Jachym Cepicky
self.cmd_output.write("$ " + command + "\n")
TypeError: cannot concatenate 'str' and 'list' objects
The problem is not command but "$ " and "\n". They is a strings. Change to
['$ '] + command + ['\n']
That will produce a list; does write() accept a list?
But the default string conversion performed by %s probably isn't what
you want. From the leading "$", I'm guessing that you want output in
shell syntax, which is going to need some form of quoting.
OTOH, you could just use Python syntax. Generating Bourne-shell syntax
is arguably wrong for something that's supposed to be cross-platform.
Post by Michael Barton
Post by Jachym Cepicky
method getCmd() of grassTask() class (gui_modules/menform.py, line 215)
returns not command string (e.g. "r.info elevation.dem"), but already
command list (e.g. ["r.info", "map=elevation.dem"]).
This is in conflict with rest of the code, where ever cmd.split(" ") is
called (e.g. wxgui.py line 410 or gui_modules/wxgui_utils.py, line 868).
cmd.split(" ") is potentially problematic and we should avoid it. For
example, <some command> map="My map" will end up as
['<command>', 'map=My', 'map'].
It doesn't matter for maps, as spaces aren't valid in map names. But
it matters for arbitrary text (titles, etc), filenames, SQL "where"
clauses etc.
Post by Michael Barton
Or <command> query="attribute1>5 and attribute2<15"
['<command>', 'query=attribute>5', 'and', 'attribute2<15']
Post by Jachym Cepicky
So, what to do? Fix getCmd(), so it returns string or go through whole
code and try to fix all cmd.split() calls?
The latter. We've already got most of them, but there are a few left
apparently.
What is the purpose of getCmd()? If it's meant to return the command
in a form that can be executed within wxgui, it should return a list.
If it's simply a string to show to the user, the format isn't
critical. OTOH, if it's going to be saved to a script, it needs to be
in the appropriate syntax (sh, csh, cmd.exe, etc).
The main point is that, if you have a function which returns a command
as a string, you need to be absolutely crystal clear on what syntax is
used. If it's Bourne-shell syntax, don't try to execute it via cmd.exe
(and vice-versa). If it's just "human-readable string", don't try to
execute it at all.
Depending upon what you want to do, you might need multiple functions
to return commands in different formats. In that case, I would suggest
having class methods always return commands as lists, and have
separate utility functions for converting such lists to strings.
--
_______________________________________________
grassgui mailing list
http://grass.itc.it/mailman/listinfo/grassgui
--
-- Daniel Calvelo Aros
Michael Barton
2007-11-14 22:56:51 UTC
Permalink
That's excellent. Should I update and try it?

Any thoughts on my locked up r.profile command??

Cheers
Michael
Post by Daniel Calvelo
Hi all.
grassTask is list-ready. In menuform.py, the grassTask.getCmd() is
meant to return a list. We discussed this a few months ago. The
quick'n'dirty way of maing this into a command string is to
" ".join( getCmd() )
This is used in e.g. building the statusbar "current command string",
and used to be also in the OnCopy() event handler. wxgui.py and
wxgui_utils.py should be changed to accomodate a list and call
getCmd() directly instead of the join snippet or equivalent.
Daniel.
Post by Glynn Clements
Post by Michael Barton
Post by Jachym Cepicky
self.cmd_output.write("$ " + command + "\n")
TypeError: cannot concatenate 'str' and 'list' objects
The problem is not command but "$ " and "\n". They is a strings. Change to
['$ '] + command + ['\n']
That will produce a list; does write() accept a list?
But the default string conversion performed by %s probably isn't what
you want. From the leading "$", I'm guessing that you want output in
shell syntax, which is going to need some form of quoting.
OTOH, you could just use Python syntax. Generating Bourne-shell syntax
is arguably wrong for something that's supposed to be cross-platform.
Post by Michael Barton
Post by Jachym Cepicky
method getCmd() of grassTask() class (gui_modules/menform.py, line 215)
returns not command string (e.g. "r.info elevation.dem"), but already
command list (e.g. ["r.info", "map=elevation.dem"]).
This is in conflict with rest of the code, where ever cmd.split(" ") is
called (e.g. wxgui.py line 410 or gui_modules/wxgui_utils.py, line 868).
cmd.split(" ") is potentially problematic and we should avoid it. For
example, <some command> map="My map" will end up as
['<command>', 'map=My', 'map'].
It doesn't matter for maps, as spaces aren't valid in map names. But
it matters for arbitrary text (titles, etc), filenames, SQL "where"
clauses etc.
Post by Michael Barton
Or <command> query="attribute1>5 and attribute2<15"
['<command>', 'query=attribute>5', 'and', 'attribute2<15']
Post by Jachym Cepicky
So, what to do? Fix getCmd(), so it returns string or go through whole
code and try to fix all cmd.split() calls?
The latter. We've already got most of them, but there are a few left
apparently.
What is the purpose of getCmd()? If it's meant to return the command
in a form that can be executed within wxgui, it should return a list.
If it's simply a string to show to the user, the format isn't
critical. OTOH, if it's going to be saved to a script, it needs to be
in the appropriate syntax (sh, csh, cmd.exe, etc).
The main point is that, if you have a function which returns a command
as a string, you need to be absolutely crystal clear on what syntax is
used. If it's Bourne-shell syntax, don't try to execute it via cmd.exe
(and vice-versa). If it's just "human-readable string", don't try to
execute it at all.
Depending upon what you want to do, you might need multiple functions
to return commands in different formats. In that case, I would suggest
having class methods always return commands as lists, and have
separate utility functions for converting such lists to strings.
--
_______________________________________________
grassgui mailing list
http://grass.itc.it/mailman/listinfo/grassgui
__________________________________________
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

Loading...