Updated TryAndPrint

* catch_all can now be set for a TryAndPrint instance
  * As opposed to defining for every TryAndPrint.run() call
This commit is contained in:
2Shirt 2020-01-23 14:06:52 -07:00
parent ea3240772e
commit 019cbb6c1a
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -392,6 +392,7 @@ class TryAndPrint():
based on exception names. based on exception names.
""" """
def __init__(self, msg_bad='FAILED', msg_good='SUCCESS'): def __init__(self, msg_bad='FAILED', msg_good='SUCCESS'):
self.catch_all = True
self.indent = INDENT self.indent = INDENT
self.msg_bad = msg_bad self.msg_bad = msg_bad
self.msg_good = msg_good self.msg_good = msg_good
@ -522,13 +523,13 @@ class TryAndPrint():
def run( def run(
self, message, function, *args, self, message, function, *args,
catch_all=True, msg_good=None, verbose=False, **kwargs): catch_all=None, msg_good=None, verbose=False, **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. re-raised. If passed it will override self.catch_all for this call.
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.
@ -553,6 +554,8 @@ class TryAndPrint():
f_exception = None f_exception = None
output = None output = None
result_msg = 'UNKNOWN' result_msg = 'UNKNOWN'
if catch_all is None:
catch_all = self.catch_all
# 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)