[GRASS-SVN] r32416 - in grass/branches/develbranch_6/lib: . python
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Jul 31 14:51:20 EDT 2008
Author: martinl
Date: 2008-07-31 14:51:20 -0400 (Thu, 31 Jul 2008)
New Revision: 32416
Added:
grass/branches/develbranch_6/lib/python/
grass/branches/develbranch_6/lib/python/README.txt
grass/branches/develbranch_6/lib/python/grass.py
Log:
lib/python backported from trunk, partly used in wxGUI
Copied: grass/branches/develbranch_6/lib/python/README.txt (from rev 32415, grass/trunk/lib/python/README.txt)
===================================================================
--- grass/branches/develbranch_6/lib/python/README.txt (rev 0)
+++ grass/branches/develbranch_6/lib/python/README.txt 2008-07-31 18:51:20 UTC (rev 32416)
@@ -0,0 +1,159 @@
+def make_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **options):
+
+Return a list of strings suitable for use as the args parameter to
+Popen() or call(). Example:
+
+>>> grass.make_command("g.message", flags = 'w', message = 'this is a warning')
+['g.message', '-w', 'message=this is a warning']
+
+def start_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **kwargs):
+
+Returns a Popen object with the command created by make_command.
+Accepts any of the arguments which Popen() accepts apart from "args"
+and "shell". Example:
+
+>>> p = grass.start_command("g.gisenv", stdout = subprocess.PIPE)
+>>> print p
+<subprocess.Popen object at 0xb7c12f6c>
+>>> print p.communicate()[0]
+GISDBASE='/opt/grass-data';
+LOCATION_NAME='spearfish57';
+MAPSET='glynn';
+GRASS_DB_ENCODING='ascii';
+GRASS_GUI='text';
+MONITOR='x0';
+
+def pipe_command(*args, **kwargs):
+
+Passes all arguments to start_command, but also adds
+"stdout = subprocess.PIPE". Returns the Popen object. Example:
+
+>>> p = grass.pipe_command("g.gisenv")
+>>> print p
+<subprocess.Popen object at 0xb7c12f6c>
+>>> print p.communicate()[0]
+GISDBASE='/opt/grass-data';
+LOCATION_NAME='spearfish57';
+MAPSET='glynn';
+GRASS_DB_ENCODING='ascii';
+GRASS_GUI='text';
+MONITOR='x0';
+
+def run_command(*args, **kwargs):
+
+Passes all arguments to start_command, then waits for the process to
+complete, returning its exit code. Similar to subprocess.call(), but
+with the make_command() interface.
+
+def read_command(*args, **kwargs):
+
+Passes all arguments to start_command, then waits for the process to
+complete, returning its stdout (i.e. similar to shell "backticks").
+
+def message(msg, flag = None):
+def debug(msg):
+def verbose(msg):
+def info(msg):
+def warning(msg):
+def error(msg):
+
+These all run g.message, differing only in which flag (if any) is used.
+
+def fatal(msg):
+
+Like error(), but also calls sys.exit(1).
+
+def parser():
+
+Interface to g.parser, intended to be run from the top-level, e.g.:
+
+ if __name__ == "__main__":
+ options, flags = grass.parser()
+ main()
+
+Thereafter, the global variables "options" and "flags" will be
+dictionaries containing option/flag values, keyed by lower-case
+option/flag names. The values in "options" are strings, those in
+"flags" are Python booleans.
+
+def tempfile():
+
+Returns the name of a temporary file, created with g.tempfile.
+
+def gisenv():
+
+Returns the output from running g.gisenv (with no arguments), as a
+dictionary. Example:
+
+>>> env = grass.gisenv()
+>>> print env['GISDBASE']
+/opt/grass-data
+
+def region():
+
+Returns the output from running "g.region -g", as a dictionary.
+Example:
+
+>>> region = grass.region()
+>>> [region[key] for key in "nsew"]
+['4928000', '4914020', '609000', '590010']
+>>> (region['nsres'], region['ewres'])
+('30', '30')
+
+def use_temp_region():
+
+Copies the current region to a temporary region with "g.region save=",
+then sets WIND_OVERRIDE to refer to that region. Installs an atexit
+handler to delete the temporary region upon termination.
+
+def del_temp_region():
+
+Unsets WIND_OVERRIDE and removes any region named by it.
+
+def find_file(name, element = 'cell'):
+
+Returns the output from running g.findfile as a dictionary. Example:
+
+>>> result = grass.find_file('fields', element = 'vector')
+>>> print result['fullname']
+fields at PERMANENT
+>>> print result['file']
+/opt/grass-data/spearfish57/PERMANENT/vector/fields
+
+def list_grouped(type):
+
+Returns the output from running g.list, as a dictionary where the keys
+are mapset names and the values are lists of maps in that mapset.
+Example:
+
+>>> grass.list_grouped('rast')['PERMANENT']
+['aspect', 'erosion1', 'quads', 'soils', 'strm.dist', ...
+
+def list_pairs(type):
+
+Returns the output from running g.list, as a list of (map, mapset)
+pairs. Example:
+
+>>> grass.list_pairs('rast')
+[('aspect', 'PERMANENT'), ('erosion1', 'PERMANENT'), ('quads', 'PERMANENT'), ...
+
+def list_strings(type):
+
+Returns the output from running g.list, as a list of qualified names.
+Example:
+
+>>> grass.list_strings('rast')
+['aspect at PERMANENT', 'erosion1 at PERMANENT', 'quads at PERMANENT', 'soils at PERMANENT', ...
+
+def parse_color(val, dflt = None):
+
+Parses the string "val" as a GRASS colour, which can be either one of
+the named colours or an R:G:B tuple e.g. 255:255:255. Returns an
+(r,g,b) triple whose components are floating point values between 0
+and 1. Example:
+
+>>> grass.parse_color("red")
+(1.0, 0.0, 0.0)
+>>> grass.parse_color("255:0:0")
+(1.0, 0.0, 0.0)
+
Copied: grass/branches/develbranch_6/lib/python/grass.py (from rev 32414, grass/trunk/lib/python/grass.py)
===================================================================
--- grass/branches/develbranch_6/lib/python/grass.py (rev 0)
+++ grass/branches/develbranch_6/lib/python/grass.py 2008-07-31 18:51:20 UTC (rev 32416)
@@ -0,0 +1,218 @@
+import os
+import os.path
+import sys
+import types
+import subprocess
+import re
+import atexit
+
+# GRASS-oriented interface to subprocess module
+
+_popen_args = ["bufsize", "executable", "stdin", "stdout", "stderr",
+ "preexec_fn", "close_fds", "cwd", "env",
+ "universal_newlines", "startupinfo", "creationflags"]
+
+def _make_val(val):
+ if isinstance(val, types.StringType):
+ return val
+ if isinstance(val, types.ListType):
+ return ",".join(map(_make_val, val))
+ if isinstance(val, types.TupleType):
+ return _make_val(list(val))
+ return str(val)
+
+def make_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **options):
+ args = [prog]
+ if overwrite:
+ args.append("--o")
+ if quiet:
+ args.append("--q")
+ if verbose:
+ args.append("--v")
+ if flags:
+ args.append("-%s" % flags)
+ for opt, val in options.iteritems():
+ args.append("%s=%s" % (opt, _make_val(val)))
+ return args
+
+def start_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **kwargs):
+ options = {}
+ popts = {}
+ for opt, val in kwargs.iteritems():
+ if opt in _popen_args:
+ popts[opt] = val
+ else:
+ options[opt] = val
+ args = make_command(prog, flags, overwrite, quiet, verbose, **options)
+ return subprocess.Popen(args, **popts)
+
+def run_command(*args, **kwargs):
+ ps = start_command(*args, **kwargs)
+ return ps.wait()
+
+def pipe_command(*args, **kwargs):
+ kwargs['stdout'] = subprocess.PIPE
+ return start_command(*args, **kwargs)
+
+def read_command(*args, **kwargs):
+ ps = pipe_command(*args, **kwargs)
+ return ps.communicate()[0]
+
+# interface to g.message
+
+def message(msg, flag = None):
+ run_command("g.message", flags = flag, message = msg)
+
+def debug(msg):
+ message(msg, flag = 'd')
+
+def verbose(msg):
+ message(msg, flag = 'v')
+
+def info(msg):
+ message(msg, flag = 'i')
+
+def warning(msg):
+ message(msg, flag = 'w')
+
+def error(msg):
+ message(msg, flag = 'e')
+
+def fatal(msg):
+ error(msg)
+ sys.exit(1)
+
+# interface to g.parser
+
+def _parse_env():
+ 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));
+ return (options, flags)
+
+def parser():
+ if not os.getenv("GISBASE"):
+ 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()
+
+ argv = sys.argv[:]
+ name = argv[0]
+ if not os.path.isabs(name):
+ if os.sep in name or (os.altsep and os.altsep in name):
+ argv[0] = os.path.abspath(name)
+ else:
+ argv[0] = os.path.join(sys.path[0], name)
+
+ os.execvp("g.parser", [name] + argv)
+ raise OSError("error executing g.parser")
+
+# interface to g.tempfile
+
+def tempfile():
+ return read_command("g.tempfile", pid = os.getpid()).strip()
+
+# interface to g.gisenv
+
+_kv_regex = re.compile("([^=]+)='(.*)';?")
+
+def gisenv():
+ lines = read_command("g.gisenv").splitlines()
+ return dict([_kv_regex.match(line).groups() for line in lines])
+
+# interface to g.region
+
+def region():
+ lines = read_command("g.region", flags='g').splitlines()
+ return dict([line.split('=',1) for line in lines])
+
+def use_temp_region():
+ name = "tmp.%s.%d" % (os.path.basename(sys.argv[0]), os.getpid())
+ run_command("g.region", save = name)
+ os.environ['WIND_OVERRIDE'] = name
+ atexit.register(del_temp_region)
+
+def del_temp_region():
+ try:
+ name = os.environ.pop('WIND_OVERRIDE')
+ run_command("g.remove", quiet = True, region = name)
+ except:
+ pass
+
+# interface to g.findfile
+
+def find_file(name, element = 'cell'):
+ lines = read_command("g.findfile", element = element, file = name).splitlines()
+ return dict([_kv_regex.match(line).groups() for line in lines])
+
+# interface to g.list
+
+def list_grouped(type):
+ dashes_re = re.compile("^----+$")
+ mapset_re = re.compile("<(.*)>:$")
+ result = {}
+ mapset = None
+ for line in read_command("g.list", type = type).splitlines():
+ if line == "":
+ continue
+ if dashes_re.match(line):
+ continue
+ m = mapset_re.search(line)
+ if m:
+ mapset = m.group(1)
+ result[mapset] = []
+ continue
+ if mapset:
+ result[mapset].extend(line.split())
+ return result
+
+def _concat(xs):
+ result = []
+ for x in xs:
+ result.extend(x)
+ return result
+
+def list_pairs(type):
+ return _concat([[(map, mapset) for map in maps]
+ for mapset, maps in list_grouped(type).iteritems()])
+
+def list_strings(type):
+ return ["%s@%s" % pair for pair in list_pairs(type)]
+
+# color parsing
+
+named_colors = {
+ "white": (1.00, 1.00, 1.00),
+ "black": (0.00, 0.00, 0.00),
+ "red": (1.00, 0.00, 0.00),
+ "green": (0.00, 1.00, 0.00),
+ "blue": (0.00, 0.00, 1.00),
+ "yellow": (1.00, 1.00, 0.00),
+ "magenta": (1.00, 0.00, 1.00),
+ "cyan": (0.00, 1.00, 1.00),
+ "aqua": (0.00, 0.75, 0.75),
+ "grey": (0.75, 0.75, 0.75),
+ "gray": (0.75, 0.75, 0.75),
+ "orange": (1.00, 0.50, 0.00),
+ "brown": (0.75, 0.50, 0.25),
+ "purple": (0.50, 0.00, 1.00),
+ "violet": (0.50, 0.00, 1.00),
+ "indigo": (0.00, 0.50, 1.00)}
+
+def parse_color(val, dflt = None):
+ if val in named_colors:
+ return named_colors[val]
+
+ vals = val.split(':')
+ if len(vals) == 3:
+ return tuple(float(v) / 255 for v in vals)
+
+ return dflt
More information about the grass-commit
mailing list