[GRASS-SVN] r36992 - grass/branches/develbranch_6/lib/python

svn_grass at osgeo.org svn_grass at osgeo.org
Mon May 4 12:46:39 EDT 2009


Author: martinl
Date: 2009-05-04 12:46:39 -0400 (Mon, 04 May 2009)
New Revision: 36992

Modified:
   grass/branches/develbranch_6/lib/python/grass.py
Log:
grass.py: enable parse output from read_command()


Modified: grass/branches/develbranch_6/lib/python/grass.py
===================================================================
--- grass/branches/develbranch_6/lib/python/grass.py	2009-05-04 16:43:32 UTC (rev 36991)
+++ grass/branches/develbranch_6/lib/python/grass.py	2009-05-04 16:46:39 UTC (rev 36992)
@@ -5,6 +5,7 @@
 import re
 import atexit
 import string
+import types
 
 # subprocess wrapper that uses shell on Windows
 
@@ -108,8 +109,22 @@
 def read_command(*args, **kwargs):
     """Passes all arguments to pipe_command, then waits for the process to
     complete, returning its stdout (i.e. similar to shell `backticks`).
+
+    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(parse) is types.FunctionType:
+            parse = kwargs['parse']
+        else:
+            parse = parse_key_val # use default fn
+        del kwargs['parse']
+    
     ps = pipe_command(*args, **kwargs)
+    if parse:
+        return parse(ps.communicate()[0])
+    
     return ps.communicate()[0]
 
 def write_command(*args, **kwargs):
@@ -221,6 +236,9 @@
     by newlines and the key and value are separated by `sep' (default: `=')
     """
     result = {}
+    if not s:
+        return result
+    
     for line in s.splitlines():
 	kv = line.split(sep, 1)
 	k = kv[0].strip()



More information about the grass-commit mailing list