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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Feb 15 01:13:14 EST 2010


Author: cmbarton
Date: 2010-02-15 01:13:13 -0500 (Mon, 15 Feb 2010)
New Revision: 41007

Modified:
   grass/trunk/lib/python/core.py
Log:
Added command_info method. Returns as dictionary all information output from <command> help.

Modified: grass/trunk/lib/python/core.py
===================================================================
--- grass/trunk/lib/python/core.py	2010-02-14 22:30:58 UTC (rev 41006)
+++ grass/trunk/lib/python/core.py	2010-02-15 06:13:13 UTC (rev 41007)
@@ -20,6 +20,7 @@
 
 @author Glynn Clements
 @author Martin Landa <landa.martin gmail.com>
+ at author Michael Barton <michael.barton at asu.edu>
 """
 
 import os
@@ -754,3 +755,64 @@
     @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()
+    
+    # Parameters
+    first, params = err.split('Parameters:')
+    paramlines = params.splitlines()
+    dict = {}
+    for line in paramlines:
+        line = line.strip()
+        if line == '': continue
+        param = line.split(' ',1)[0].strip()
+        pval = line.split(' ',1)[1].strip()
+        dict[param] = pval
+        
+    cmdinfo['parameters'] = dict
+    
+    # Flags
+    first, flags = first.split('Flags:')
+    flaglines = flags.splitlines()
+    dict = {}
+    for line in flaglines:
+        line = line.strip()
+        if line == '': continue
+        flag = line.split(' ',1)[0].strip()
+        fval = line.split(' ',1)[1].strip()
+        dict[flag] = fval
+        
+    cmdinfo['flags'] = dict
+    
+    # Usage
+    first, usage = first.split('Usage:')
+    usagelines = usage.splitlines()
+    list = []
+    for line in usagelines:
+        line = line.strip()+' '
+        if line == '': continue
+        list.append(line)
+        
+    cmdinfo['usage'] = ''.join(list).strip()
+    
+    # Keywords
+    first, keywords = first.split('Keywords:')
+    list = []
+    list = keywords.strip().split(',')
+    cmdinfo['keywords'] = list
+        
+    #Description
+    first, desc = first.split('Description:')
+    desclines = desc.splitlines()
+    for line in desclines:
+        line = line.strip()+' '
+        
+    cmdinfo['description'] = ''.join(desclines).strip()
+            
+    return cmdinfo



More information about the grass-commit mailing list