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 <[email protected]>
  • Loading branch information
tmszi and petrasovaa committed Nov 21, 2023
1 parent a34eb51 commit 4f6ea9a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 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
28 changes: 21 additions & 7 deletions python/grass/script/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,27 @@ def get_num_suffix(number, max_number):


def split(s):
"""!Platform specific shlex.split"""
if sys.version_info >= (2, 6):
return shlex.split(s, posix=(sys.platform != "win32"))
elif sys.platform == "win32":
return shlex.split(s.replace("\\", r"\\"))
else:
return shlex.split(s)
"""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 4f6ea9a

Please sign in to comment.