[GRASS-SVN] r41972 - grass/branches/releasebranch_6_4/lib/python
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Apr 22 05:37:51 EDT 2010
Author: martinl
Date: 2010-04-22 05:37:46 -0400 (Thu, 22 Apr 2010)
New Revision: 41972
Modified:
grass/branches/releasebranch_6_4/lib/python/core.py
grass/branches/releasebranch_6_4/lib/python/db.py
grass/branches/releasebranch_6_4/lib/python/raster.py
grass/branches/releasebranch_6_4/lib/python/vector.py
Log:
libpython synchronized with trunk
Modified: grass/branches/releasebranch_6_4/lib/python/core.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/core.py 2010-04-22 08:53:15 UTC (rev 41971)
+++ grass/branches/releasebranch_6_4/lib/python/core.py 2010-04-22 09:37:46 UTC (rev 41972)
@@ -55,6 +55,8 @@
PIPE = subprocess.PIPE
STDOUT = subprocess.STDOUT
+fatal_exit = True # abort on fatal()
+
def call(*args, **kwargs):
return Popen(*args, **kwargs).wait()
@@ -335,20 +337,42 @@
@return g.message's exit code
"""
error(msg)
- sys.exit(1)
+
+ global fatal_exit
+ if fatal_exit:
+ sys.exit(1)
+
+def set_fatal_exit(exit = True):
+ """!Set fatal_exit variable
+ @param exit True to abort on fatal() otherwise just error message
+ is printed"""
+ global fatal_exit
+ fatal_exit = exit
+
# interface to g.parser
-def _parse_env():
+def _parse_opts(lines):
options = {}
flags = {}
- for var, val in os.environ.iteritems():
- if var.startswith("GIS_OPT_"):
- opt = var.replace("GIS_OPT_", "", 1).lower()
- options[opt] = val;
- if var.startswith("GIS_FLAG_"):
- flg = var.replace("GIS_FLAG_", "", 1).lower()
- flags[flg] = bool(int(val));
+ for line in lines:
+ line = line.rstrip('\r\n')
+ if not line:
+ break
+ try:
+ [var, val] = line.split('=', 1)
+ except:
+ raise SyntaxError("invalid output from g.parser: %s" % line)
+
+ if var.startswith('flag_'):
+ flags[var[5:]] = bool(int(val))
+ elif var.startswith('opt_'):
+ options[var[4:]] = val
+ elif var in ['GRASS_OVERWRITE', 'GRASS_VERBOSE']:
+ os.environ[var] = val
+ else:
+ raise SyntaxError("invalid output from g.parser: %s" % line)
+
return (options, flags)
def parser():
@@ -369,9 +393,6 @@
print >> sys.stderr, "You must be in GRASS GIS to run this program."
sys.exit(1)
- if len(sys.argv) > 1 and sys.argv[1] == "@ARGS_PARSED@":
- return _parse_env()
-
cmdline = [basename(sys.argv[0])]
cmdline += ['"' + arg + '"' for arg in sys.argv[1:]]
os.environ['CMDLINE'] = ' '.join(cmdline)
@@ -384,12 +405,16 @@
else:
argv[0] = os.path.join(sys.path[0], name)
- if sys.platform == "win32":
- os.execvp("g.parser.exe", [name] + argv)
- else:
- os.execvp("g.parser", [name] + argv)
- raise OSError("error executing g.parser")
+ p = Popen(['g.parser', '-s'] + argv, stdout = PIPE)
+ s = p.communicate()[0]
+ lines = s.splitlines()
+ if not lines or lines[0].rstrip('\r\n') != "@ARGS_PARSED@":
+ sys.stdout.write(s)
+ sys.exit()
+
+ return _parse_opts(lines[1:])
+
# interface to g.tempfile
def tempfile():
@@ -471,7 +496,10 @@
@return dictionary of region values
"""
s = read_command("g.region", flags='g')
- return parse_key_val(s, val_type = float)
+ reg = parse_key_val(s, val_type = float)
+ for k in ['rows', 'cols']:
+ reg[k] = int(reg[k])
+ return reg
def use_temp_region():
"""!Copies the current region to a temporary region with "g.region save=",
@@ -742,3 +770,68 @@
@return float value
"""
return sum(float(x) / 60 ** n for (n, x) in enumerate(s.split(':')))
+
+def command_info(cmd):
+ """!Returns 'help' information for any command as dictionary with entries
+ for description, keywords, usage, flags, and parameters"""
+
+ cmdinfo = {}
+ s = start_command(cmd, 'help', stdout = subprocess.PIPE, stderr = subprocess.PIPE)
+ out, err = s.communicate()
+
+ sections = err.split('\n\n')
+
+ #Description
+ first, desc = sections[0].split(':\n', 1)
+ desclines = desc.splitlines()
+ for line in desclines:
+ line = line.strip()+' '
+
+ # Keywords
+ first, keywords = sections[1].split(':\n', 1)
+ keylines = keywords.splitlines()
+ list = []
+ list = keywords.strip().split(',')
+ cmdinfo['keywords'] = list
+
+ cmdinfo['description'] = ''.join(desclines).strip()
+
+ # Usage
+ first, usage = sections[2].split(':\n', 1)
+ usagelines = usage.splitlines()
+ list = []
+ for line in usagelines:
+ line = line.strip()
+ if line == '': continue
+ line = line+' '
+ list.append(line)
+
+ cmdinfo['usage'] = ''.join(list).strip()
+
+ # Flags
+ first, flags = sections[3].split(':\n', 1)
+ flaglines = flags.splitlines()
+ dict = {}
+ for line in flaglines:
+ line = line.strip()
+ if line == '': continue
+ item = line.split(' ',1)[0].strip()
+ val = line.split(' ',1)[1].strip()
+ dict[item] = val
+
+ cmdinfo['flags'] = dict
+
+ # Parameters
+ first, params = err.rsplit(':\n', 1)
+ paramlines = params.splitlines()
+ dict = {}
+ for line in paramlines:
+ line = line.strip()
+ if line == '': continue
+ item = line.split(' ',1)[0].strip()
+ val = line.split(' ',1)[1].strip()
+ dict[item] = val
+
+ cmdinfo['parameters'] = dict
+
+ return cmdinfo
Modified: grass/branches/releasebranch_6_4/lib/python/db.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/db.py 2010-04-22 08:53:15 UTC (rev 41971)
+++ grass/branches/releasebranch_6_4/lib/python/db.py 2010-04-22 09:37:46 UTC (rev 41972)
@@ -25,12 +25,10 @@
from core import *
-# run "db.describe -c ..." and parse output
-
def db_describe(table, **args):
"""!Return the list of columns for a database table
(interface to `db.describe -c'). Example:
-
+
\code
>>> grass.db_describe('lakes')
{'nrows': 15279, 'cols': [['cat', 'INTEGER', '11'], ['AREA', 'DOUBLE PRECISION', '20'],
@@ -78,4 +76,3 @@
"""
s = read_command('db.connect', flags = 'p')
return parse_key_val(s, sep = ':')
-
Modified: grass/branches/releasebranch_6_4/lib/python/raster.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/raster.py 2010-04-22 08:53:15 UTC (rev 41971)
+++ grass/branches/releasebranch_6_4/lib/python/raster.py 2010-04-22 09:37:46 UTC (rev 41972)
@@ -34,6 +34,8 @@
"""!Set the command history for a raster map to the command used to
invoke the script (interface to `r.support').
+ @param map map name
+
@return True on success
@return False on failure
"""
@@ -82,6 +84,5 @@
"""
t = string.Template(exp)
e = t.substitute(**kwargs)
-
if write_command('r.mapcalc', stdin = e) != 0:
fatal("An error occurred while running r.mapcalc")
Modified: grass/branches/releasebranch_6_4/lib/python/vector.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/vector.py 2010-04-22 08:53:15 UTC (rev 41971)
+++ grass/branches/releasebranch_6_4/lib/python/vector.py 2010-04-22 09:37:46 UTC (rev 41972)
@@ -41,6 +41,7 @@
\endcode
@param map vector map
+ @param args
@return dictionary { layer : { 'layer', 'table, 'database', 'driver', 'key' }
"""
@@ -72,7 +73,13 @@
def vector_layer_db(map, layer):
"""!Return the database connection details for a vector map layer.
- If db connection for given layer is not defined, fatal() is called."""
+ If db connection for given layer is not defined, fatal() is called.
+
+ @param map map name
+ @param layer layer number
+
+ @return parsed output
+ """
try:
f = vector_db(map)[int(layer)]
except KeyError:
@@ -86,7 +93,7 @@
"""!Return a dictionary (or a list) of the columns for the
database table connected to a vector map (interface to `v.info
-c').
-
+
@code
>>> vector_columns(urbanarea, getDict = True)
{'UA_TYPE': {'index': 4, 'type': 'CHARACTER'}, 'UA': {'index': 2, 'type': 'CHARACTER'}, 'NAME': {'index': 3, 'type': 'CHARACTER'}, 'OBJECTID': {'index': 1, 'type': 'INTEGER'}, 'cat': {'index': 0, 'type': 'INTEGER'}}
@@ -124,6 +131,10 @@
def vector_history(map):
"""!Set the command history for a vector map to the command used to
invoke the script (interface to `v.support').
+
+ @param map mapname
+
+ @return v.support output
"""
run_command('v.support', map = map, cmdhist = os.environ['CMDLINE'])
More information about the grass-commit
mailing list