Allow setting verbose value for TryAndPrint obj
* Instead of strictly per-call
This commit is contained in:
parent
154acc5280
commit
64db679a4d
1 changed files with 12 additions and 5 deletions
|
|
@ -388,6 +388,7 @@ class Menu():
|
||||||
|
|
||||||
|
|
||||||
class TryAndPrint():
|
class TryAndPrint():
|
||||||
|
# pylint: disable=too-many-instance-attributes
|
||||||
"""Object used to standardize running functions and returning the result.
|
"""Object used to standardize running functions and returning the result.
|
||||||
|
|
||||||
The errors and warning attributes are used to allow fine-tuned results
|
The errors and warning attributes are used to allow fine-tuned results
|
||||||
|
|
@ -396,11 +397,12 @@ class TryAndPrint():
|
||||||
def __init__(self, msg_bad='FAILED', msg_good='SUCCESS'):
|
def __init__(self, msg_bad='FAILED', msg_good='SUCCESS'):
|
||||||
self.catch_all = True
|
self.catch_all = True
|
||||||
self.indent = INDENT
|
self.indent = INDENT
|
||||||
self.msg_bad = msg_bad
|
|
||||||
self.msg_good = msg_good
|
|
||||||
self.width = WIDTH
|
|
||||||
self.list_errors = ['GenericError']
|
self.list_errors = ['GenericError']
|
||||||
self.list_warnings = ['GenericWarning']
|
self.list_warnings = ['GenericWarning']
|
||||||
|
self.msg_bad = msg_bad
|
||||||
|
self.msg_good = msg_good
|
||||||
|
self.verbose = False
|
||||||
|
self.width = WIDTH
|
||||||
|
|
||||||
def _format_exception_message(self, _exception):
|
def _format_exception_message(self, _exception):
|
||||||
"""Format using the exception's args or name, returns str."""
|
"""Format using the exception's args or name, returns str."""
|
||||||
|
|
@ -525,13 +527,13 @@ class TryAndPrint():
|
||||||
|
|
||||||
def run(
|
def run(
|
||||||
self, message, function, *args,
|
self, message, function, *args,
|
||||||
catch_all=None, msg_good=None, verbose=False, **kwargs):
|
catch_all=None, msg_good=None, verbose=None, **kwargs):
|
||||||
# pylint: disable=catching-non-exception
|
# pylint: disable=catching-non-exception
|
||||||
"""Run a function and print the results, returns results as dict.
|
"""Run a function and print the results, returns results as dict.
|
||||||
|
|
||||||
If catch_all is True then (nearly) all exceptions will be caught.
|
If catch_all is True then (nearly) all exceptions will be caught.
|
||||||
Otherwise if an exception occurs that wasn't specified it will be
|
Otherwise if an exception occurs that wasn't specified it will be
|
||||||
re-raised. If passed it will override self.catch_all for this call.
|
re-raised.
|
||||||
|
|
||||||
If the function returns data it will be used instead of msg_good,
|
If the function returns data it will be used instead of msg_good,
|
||||||
msg_bad, or exception text.
|
msg_bad, or exception text.
|
||||||
|
|
@ -542,6 +544,9 @@ class TryAndPrint():
|
||||||
If verbose is True then exception names or messages will be used for
|
If verbose is True then exception names or messages will be used for
|
||||||
the result message. Otherwise it will simply be set to result_bad.
|
the result message. Otherwise it will simply be set to result_bad.
|
||||||
|
|
||||||
|
If catch_all and/or verbose are passed it will override
|
||||||
|
self.catch_all and/or self.verbose for this call.
|
||||||
|
|
||||||
args and kwargs are passed to the function.
|
args and kwargs are passed to the function.
|
||||||
"""
|
"""
|
||||||
LOG.debug('function: %s.%s', function.__module__, function.__name__)
|
LOG.debug('function: %s.%s', function.__module__, function.__name__)
|
||||||
|
|
@ -558,6 +563,8 @@ class TryAndPrint():
|
||||||
result_msg = 'UNKNOWN'
|
result_msg = 'UNKNOWN'
|
||||||
if catch_all is None:
|
if catch_all is None:
|
||||||
catch_all = self.catch_all
|
catch_all = self.catch_all
|
||||||
|
if verbose is None:
|
||||||
|
verbose = self.verbose
|
||||||
|
|
||||||
# Build exception tuples
|
# Build exception tuples
|
||||||
e_exceptions = tuple(self._get_exception(e) for e in self.list_errors)
|
e_exceptions = tuple(self._get_exception(e) for e in self.list_errors)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue