Allow setting verbose value for TryAndPrint obj

* Instead of strictly per-call
This commit is contained in:
2Shirt 2020-01-30 13:47:59 -07:00
parent 154acc5280
commit 64db679a4d
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -388,6 +388,7 @@ class Menu():
class TryAndPrint():
# pylint: disable=too-many-instance-attributes
"""Object used to standardize running functions and returning the result.
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'):
self.catch_all = True
self.indent = INDENT
self.msg_bad = msg_bad
self.msg_good = msg_good
self.width = WIDTH
self.list_errors = ['GenericError']
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):
"""Format using the exception's args or name, returns str."""
@ -525,13 +527,13 @@ class TryAndPrint():
def run(
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
"""Run a function and print the results, returns results as dict.
If catch_all is True then (nearly) all exceptions will be caught.
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,
msg_bad, or exception text.
@ -542,6 +544,9 @@ class TryAndPrint():
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.
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.
"""
LOG.debug('function: %s.%s', function.__module__, function.__name__)
@ -558,6 +563,8 @@ class TryAndPrint():
result_msg = 'UNKNOWN'
if catch_all is None:
catch_all = self.catch_all
if verbose is None:
verbose = self.verbose
# Build exception tuples
e_exceptions = tuple(self._get_exception(e) for e in self.list_errors)