Skip to content

Commit

Permalink
Allow control of error handling for run_command() etc
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.osgeo.org/grass/grass/trunk@62705 15284696-431f-4ddb-bdfa-cd5b030d7da7
  • Loading branch information
glynnc committed Nov 11, 2014
1 parent 3fef961 commit c33ef44
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions lib/python/script/core.py
Expand Up @@ -247,7 +247,7 @@ def get_real_command(cmd):


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:
Expand Down Expand Up @@ -291,6 +291,22 @@ def make_command(prog, flags="", overwrite=False, quiet=False, verbose=False,
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.
Expand Down Expand Up @@ -354,14 +370,7 @@ def run_command(*args, **kwargs):
"""
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):
Expand Down Expand Up @@ -413,12 +422,7 @@ def read_command(*args, **kwargs):
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):
Expand Down Expand Up @@ -476,14 +480,7 @@ def write_command(*args, **kwargs):
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,
Expand Down Expand Up @@ -514,7 +511,7 @@ def message(msg, flag=None):
: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):
Expand Down

0 comments on commit c33ef44

Please sign in to comment.