From 5f69e23887f72b94857e5df7f5218494f7c75e32 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Sun, 4 Sep 2022 17:44:58 -0700 Subject: [PATCH] Address Pylint error W1518 --- scripts/wk/std.py | 77 ++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/scripts/wk/std.py b/scripts/wk/std.py index 793a5319..af4b9682 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -501,42 +501,6 @@ class TryAndPrint(): # Done return result_msg - @cache - def _get_exception(self, name): - """Get exception by name, returns exception object. - - [Doctest] - >>> t = TryAndPrint() - >>> t._get_exception('AttributeError') - - >>> t._get_exception('CalledProcessError') - - >>> t._get_exception('GenericError') - - """ - LOG.debug('Getting exception: %s', name) - obj = getattr(sys.modules[__name__], name, None) - if obj: - return obj - - # Try builtin classes - obj = getattr(sys.modules['builtins'], name, None) - if obj: - return obj - - # Try all modules - for _mod in sys.modules.values(): - obj = getattr(_mod, name, None) - if obj: - break - - # Check if not found - if not obj: - raise AttributeError(f'Failed to find exception: {name}') - - # Done - return obj - def _log_result(self, message, result_msg): """Log result text without color formatting.""" log_text = f'{" "*self.indent}{message:<{self.width}}{result_msg}' @@ -595,8 +559,8 @@ class TryAndPrint(): verbose = verbose if verbose is not None else self.verbose # Build exception tuples - e_exceptions = tuple(self._get_exception(e) for e in self.list_errors) - w_exceptions = tuple(self._get_exception(e) for e in self.list_warnings) + e_exceptions = tuple(get_exception(e) for e in self.list_errors) + w_exceptions = tuple(get_exception(e) for e in self.list_warnings) # Run function and catch exceptions print(f'{" "*self.indent}{message:<{self.width}}', end='', flush=True) @@ -830,6 +794,43 @@ def generate_debug_report(): return '\n'.join(report) +@cache +def get_exception(name): + """Get exception by name, returns exception object. + + [Doctest] + >>> t = TryAndPrint() + >>> t._get_exception('AttributeError') + + >>> t._get_exception('CalledProcessError') + + >>> t._get_exception('GenericError') + + """ + LOG.debug('Getting exception: %s', name) + obj = getattr(sys.modules[__name__], name, None) + if obj: + return obj + + # Try builtin classes + obj = getattr(sys.modules['builtins'], name, None) + if obj: + return obj + + # Try all modules + for _mod in sys.modules.values(): + obj = getattr(_mod, name, None) + if obj: + break + + # Check if not found + if not obj: + raise AttributeError(f'Failed to find exception: {name}') + + # Done + return obj + + def get_log_filepath(): """Get the log filepath from the root logger, returns pathlib.Path obj.