[GRASS-SVN] r64404 - grass/trunk/lib/python/script
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Feb 2 10:23:12 PST 2015
Author: martinl
Date: 2015-02-02 10:23:12 -0800 (Mon, 02 Feb 2015)
New Revision: 64404
Modified:
grass/trunk/lib/python/script/task.py
grass/trunk/lib/python/script/utils.py
Log:
libpython: move cmd list <-> tuple from wxGUI
Modified: grass/trunk/lib/python/script/task.py
===================================================================
--- grass/trunk/lib/python/script/task.py 2015-02-02 17:28:42 UTC (rev 64403)
+++ grass/trunk/lib/python/script/task.py 2015-02-02 18:23:12 UTC (rev 64404)
@@ -25,7 +25,7 @@
except ImportError:
import elementtree.ElementTree as etree # Python <= 2.4
-from utils import decode
+from utils import decode, split
from core import *
@@ -581,3 +581,68 @@
cmdinfo['usage'] = usage
return cmdinfo
+
+def cmdtuple_to_list(cmd):
+ """Convert command tuple to list.
+
+ :param tuple cmd: GRASS command to be converted
+
+ :return: command in list
+ """
+ cmdList = []
+ if not cmd:
+ return cmdList
+
+ cmdList.append(cmd[0])
+
+ if 'flags' in cmd[1]:
+ for flag in cmd[1]['flags']:
+ cmdList.append('-' + flag)
+ for flag in ('help', 'verbose', 'quiet', 'overwrite'):
+ if flag in cmd[1] and cmd[1][flag] is True:
+ cmdList.append('--' + flag)
+
+ for k, v in cmd[1].iteritems():
+ if k in ('flags', 'help', 'verbose', 'quiet', 'overwrite'):
+ continue
+ cmdList.append('%s=%s' % (k, v))
+
+ return cmdList
+
+def cmdlist_to_tuple(cmd):
+ """Convert command list to tuple for run_command() and others
+
+ :param list cmd: GRASS command to be converted
+
+ :return: command as tuple
+ """
+ if len(cmd) < 1:
+ return None
+
+ dcmd = {}
+ for item in cmd[1:]:
+ if '=' in item: # params
+ key, value = item.split('=', 1)
+ dcmd[str(key)] = str(value).replace('"', '')
+ elif item[:2] == '--': # long flags
+ flag = item[2:]
+ if flag in ('help', 'verbose', 'quiet', 'overwrite'):
+ dcmd[str(flag)] = True
+ elif len(item) == 2 and item[0] == '-': # -> flags
+ if 'flags' not in dcmd:
+ dcmd['flags'] = ''
+ dcmd['flags'] += item[1]
+ else: # unnamed parameter
+ module = gtask.parse_interface(cmd[0])
+ dcmd[module.define_first()] = item
+
+ return (cmd[0], dcmd)
+
+def cmdstring_to_tuple(cmd):
+ """Convert command string to tuple for run_command() and others
+
+ :param str cmd: command to be converted
+
+ :return: command as tuple
+ """
+ return cmdlist_to_tuple(split(cmd))
Modified: grass/trunk/lib/python/script/utils.py
===================================================================
--- grass/trunk/lib/python/script/utils.py 2015-02-02 17:28:42 UTC (rev 64403)
+++ grass/trunk/lib/python/script/utils.py 2015-02-02 18:23:12 UTC (rev 64404)
@@ -18,10 +18,11 @@
"""
import os
+import sys
import shutil
import locale
+import shlex
-
def float_or_dms(s):
"""Convert DMS to float.
@@ -235,3 +236,12 @@
"""
return '{number:0{width}d}'.format(width=len(str(max_number)),
number=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)
More information about the grass-commit
mailing list