Skip to content

Commit

Permalink
python/grass/script: split the command string using shell-like syntax…
Browse files Browse the repository at this point in the history
… on the win32 platform same as on POSIX-compliant platforms (#1908)

e.g. run 'd.vect map=census where="cat > 10 AND cat < 20"' with
where parameter from Command Prompt CMD/emulator terminal:

non-POSIX platform

```
>>> import shlex
>>> shlex.split('where="cat > 10 AND cat < 20"', posix=False)
['where="cat', '>', '10', 'AND', 'cat', '<', '20"']
```

POSIX-compliant platform

```
>>> import shlex
>>> shlex.split('where="cat > 10 AND cat < 20"', posix=True)
['where=cat > 10 AND cat < 20']
```

Under OS MS Windows 'd.vect map=census where="cat > 10 AND cat < 20"'
command runned from Command Prompt CMD require use ^ carret symbol for
escaping special characters < > ( ) & | , ; ".

e.g. 'd.vect map=census where="cat ^> 10 AND cat ^< 20"'

---------

Co-authored-by: Anna Petrasova <kratochanna@gmail.com>
  • Loading branch information
tmszi and petrasovaa committed Nov 21, 2023
1 parent d757e42 commit 816c25f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions display/d.vect/d.vect.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ <h2>NOTES</h2>
which allow the user to specify vector type, colors, data fields, SQL
queries, label size and justification, etc.

<p>When <em>d.vect</em> is used with <b>where</b> parameter on MS Windows
Command Prompt, it is important to use <tt>&#710;</tt>
carret symbol for escaping special characters <tt>&lt; &gt; ( ) &amp; &#124; , ; &quot;</tt>.
<div class="code"><pre>
d.vect map=vector_map where="cat &#710;&gt; 10 AND cat &#710;&lt; 20"
</pre></div>

<p>By default <em>d.vect</em> areas are filled with <b>fill_color</b> and
outlined with <b>color</b>. Area outlines can be suppressed with
<div class="code"><pre>
Expand Down
24 changes: 21 additions & 3 deletions python/grass/script/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""

import os
import sys
import shutil
import locale
import shlex
Expand Down Expand Up @@ -306,8 +305,27 @@ def get_num_suffix(number, max_number):


def split(s):
"""!Platform specific shlex.split"""
return shlex.split(s, posix=(sys.platform != "win32"))
"""Same shlex.split() func on all OS platforms
We don't use parameter posix=True on the OS MS Windows due to incorrectly
splitting command line parameters:
e.g. d.vect where="cat < 10"
is split incorrectly as follows:
'where="cat', '<', '10"'
Should be:
'where=cat < 10'
:param str s: cmd string
return list: cmd list
"""
return shlex.split(s)


# source:
Expand Down

0 comments on commit 816c25f

Please sign in to comment.