[GRASS-SVN] r62705 - grass/trunk/lib/python/script

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Nov 11 05:28:22 PST 2014


Author: glynn
Date: 2014-11-11 05:28:22 -0800 (Tue, 11 Nov 2014)
New Revision: 62705

Modified:
   grass/trunk/lib/python/script/core.py
Log:
Allow control of error handling for run_command() etc


Modified: grass/trunk/lib/python/script/core.py
===================================================================
--- grass/trunk/lib/python/script/core.py	2014-11-11 12:31:48 UTC (rev 62704)
+++ grass/trunk/lib/python/script/core.py	2014-11-11 13:28:22 UTC (rev 62705)
@@ -247,7 +247,7 @@
 
 
 def make_command(prog, flags="", overwrite=False, quiet=False, verbose=False,
-                 **options):
+                 errors=None, **options):
     """Return a list of strings suitable for use as the args parameter to
     Popen() or call(). Example:
 
@@ -291,6 +291,22 @@
     return args
 
 
+def handle_errors(returncode, result, args, kwargs):
+    if returncode == 0:
+        return result
+    handler = kwargs.get('errors', 'raise')
+    if handler.lower() == 'ignore':
+        return result
+    elif handler.lower() == 'status':
+        return returncode
+    elif handler.lower() == 'exit':
+        sys.exit(1)
+    else:
+        # TODO: construction of the whole command is far from perfect
+        args = make_command(*args, **kwargs)
+        raise CalledModuleError(module=None, code=repr(args),
+                                returncode=returncode)
+
 def start_command(prog, flags="", overwrite=False, quiet=False,
                   verbose=False, **kwargs):
     """Returns a Popen object with the command created by make_command.
@@ -354,14 +370,7 @@
     """
     ps = start_command(*args, **kwargs)
     returncode = ps.wait()
-    if returncode:
-        # TODO: construction of the whole command is far from perfect
-        args = make_command(*args, **kwargs)
-        raise CalledModuleError(module=None, code=' '.join(args),
-                                returncode=returncode)
-    else:
-        # the else is just for compatibility, remove before 7.1
-        return 0
+    return handle_errors(returncode, returncode, args, kwargs)
 
 
 def pipe_command(*args, **kwargs):
@@ -413,12 +422,7 @@
     process = pipe_command(*args, **kwargs)
     stdout, unused = process.communicate()
     returncode = process.poll()
-    if returncode:
-        # TODO: construction of the whole command is far from perfect
-        args = make_command(*args, **kwargs)
-        raise CalledModuleError(module=None, code=' '.join(args),
-                                returncode=returncode)
-    return stdout
+    return handle_errors(returncode, stdout, args, kwargs)
 
 
 def parse_command(*args, **kwargs):
@@ -476,14 +480,7 @@
     process = feed_command(*args, **kwargs)
     process.communicate(stdin)
     returncode = process.poll()
-    if returncode:
-        # TODO: construction of the whole command is far from perfect
-        args = make_command(*args, **kwargs)
-        raise CalledModuleError(module=None, code=' '.join(args),
-                                returncode=returncode)
-    else:
-        # the else is just for compatibility, remove before 7.1
-        return 0
+    return handle_errors(returncode, returncode, args, kwargs)
 
 
 def exec_command(prog, flags="", overwrite=False, quiet=False, verbose=False,
@@ -514,7 +511,7 @@
     :param str msg: message to be displayed
     :param str flag: flags (given as string)
     """
-    run_command("g.message", flags=flag, message=msg)
+    run_command("g.message", flags=flag, message=msg, errors='ignore')
 
 
 def debug(msg, debug=1):



More information about the grass-commit mailing list