[GRASS-SVN] r62787 - in grass/branches/releasebranch_7_0: . lib/python/script
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Nov 17 18:38:49 PST 2014
Author: wenzeslaus
Date: 2014-11-17 18:38:49 -0800 (Mon, 17 Nov 2014)
New Revision: 62787
Modified:
grass/branches/releasebranch_7_0/
grass/branches/releasebranch_7_0/lib/python/script/core.py
Log:
pythonlib: allow control of error handling for run_command() and others (#2326, backport r62705, relates to r62566, author: glynn)
Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
- /grass/trunk:60817,61096,61141,62179-62180,62182,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62723,62730,62739,62741,62743,62746,62750,62752,62757,62762
+ /grass/trunk:60817,61096,61141,62179-62180,62182,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62705,62723,62730,62739,62741,62743,62746,62750,62752,62757,62762
Modified: grass/branches/releasebranch_7_0/lib/python/script/core.py
===================================================================
--- grass/branches/releasebranch_7_0/lib/python/script/core.py 2014-11-18 02:33:39 UTC (rev 62786)
+++ grass/branches/releasebranch_7_0/lib/python/script/core.py 2014-11-18 02:38:49 UTC (rev 62787)
@@ -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