[GRASS-SVN] r37120 - grass/branches/releasebranch_6_4/lib/python
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun May 10 15:01:06 EDT 2009
Author: martinl
Date: 2009-05-10 15:01:06 -0400 (Sun, 10 May 2009)
New Revision: 37120
Modified:
grass/branches/releasebranch_6_4/lib/python/grass.py
Log:
grass.py: parse_command() added
Modified: grass/branches/releasebranch_6_4/lib/python/grass.py
===================================================================
--- grass/branches/releasebranch_6_4/lib/python/grass.py 2009-05-10 18:59:33 UTC (rev 37119)
+++ grass/branches/releasebranch_6_4/lib/python/grass.py 2009-05-10 19:01:06 UTC (rev 37120)
@@ -112,6 +112,30 @@
ps = pipe_command(*args, **kwargs)
return ps.communicate()[0]
+def parse_command(*args, **kwargs):
+ """Passes all arguments to read_command, then optionally parses
+ the output.
+
+ Output can be automatically parsed if <b>parse</b> parameter is
+ given. Use True for default parse function -- parse_key_val().
+ """
+ parse = None # do not parse output
+ if kwargs.has_key('parse'):
+ if type(kwargs['parse']) is types.TupleType:
+ parse = kwargs['parse'][0]
+ parse_args = kwargs['parse'][1]
+ else:
+ parse = parse_key_val # use default fn
+ parse_args = {}
+ del kwargs['parse']
+
+ res = read_command(*args, **kwargs)
+
+ if parse:
+ return parse(res, **parse_args)
+
+ return res
+
def write_command(*args, **kwargs):
"""Passes all arguments to feed_command, with the string specified
by the 'stdin' argument fed to the process' stdin.
More information about the grass-commit
mailing list