[GRASS-SVN] r37121 - grass/trunk/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Sun May 10 15:11:23 EDT 2009


Author: martinl
Date: 2009-05-10 15:11:23 -0400 (Sun, 10 May 2009)
New Revision: 37121

Modified:
   grass/trunk/lib/python/grass.py
Log:
grass.parse_command() - parse output by default


Modified: grass/trunk/lib/python/grass.py
===================================================================
--- grass/trunk/lib/python/grass.py	2009-05-10 19:01:06 UTC (rev 37120)
+++ grass/trunk/lib/python/grass.py	2009-05-10 19:11:23 UTC (rev 37121)
@@ -114,29 +114,31 @@
     return ps.communicate()[0]
 
 def parse_command(*args, **kwargs):
-    """Passes all arguments to read_command, then optionally parses
-    the output.
+    """Passes all arguments to read_command, then parses the output by
+    parse_key_val().
 
-    Output can be automatically parsed if <b>parse</b> parameter is
-    given. Use True for default parse function -- parse_key_val().
+    Parsing function can be optionally given by <b>parse</b> parameter
+    including its arguments, e.g.
+
+    @code
+    parse_command(..., parse = (grass.parse_key_val, { 'sep' : ':' }))
+    @endcode
     """
-    parse = None # do not parse output
+    parse = None
     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']
-
+    
+    if not parse:
+        parse = parse_key_val # use default fn
+        parse_args = {}
+        
     res = read_command(*args, **kwargs)
 
-    if parse:
-        return parse(res, **parse_args)
+    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