Address Pylint error W1518
This commit is contained in:
parent
d36589751e
commit
5f69e23887
1 changed files with 39 additions and 38 deletions
|
|
@ -501,42 +501,6 @@ class TryAndPrint():
|
||||||
# Done
|
# Done
|
||||||
return result_msg
|
return result_msg
|
||||||
|
|
||||||
@cache
|
|
||||||
def _get_exception(self, name):
|
|
||||||
"""Get exception by name, returns exception object.
|
|
||||||
|
|
||||||
[Doctest]
|
|
||||||
>>> t = TryAndPrint()
|
|
||||||
>>> t._get_exception('AttributeError')
|
|
||||||
<class 'AttributeError'>
|
|
||||||
>>> t._get_exception('CalledProcessError')
|
|
||||||
<class 'subprocess.CalledProcessError'>
|
|
||||||
>>> t._get_exception('GenericError')
|
|
||||||
<class 'wk.std.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):
|
def _log_result(self, message, result_msg):
|
||||||
"""Log result text without color formatting."""
|
"""Log result text without color formatting."""
|
||||||
log_text = f'{" "*self.indent}{message:<{self.width}}{result_msg}'
|
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
|
verbose = verbose if verbose is not None else self.verbose
|
||||||
|
|
||||||
# Build exception tuples
|
# Build exception tuples
|
||||||
e_exceptions = tuple(self._get_exception(e) for e in self.list_errors)
|
e_exceptions = tuple(get_exception(e) for e in self.list_errors)
|
||||||
w_exceptions = tuple(self._get_exception(e) for e in self.list_warnings)
|
w_exceptions = tuple(get_exception(e) for e in self.list_warnings)
|
||||||
|
|
||||||
# Run function and catch exceptions
|
# Run function and catch exceptions
|
||||||
print(f'{" "*self.indent}{message:<{self.width}}', end='', flush=True)
|
print(f'{" "*self.indent}{message:<{self.width}}', end='', flush=True)
|
||||||
|
|
@ -830,6 +794,43 @@ def generate_debug_report():
|
||||||
return '\n'.join(report)
|
return '\n'.join(report)
|
||||||
|
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def get_exception(name):
|
||||||
|
"""Get exception by name, returns exception object.
|
||||||
|
|
||||||
|
[Doctest]
|
||||||
|
>>> t = TryAndPrint()
|
||||||
|
>>> t._get_exception('AttributeError')
|
||||||
|
<class 'AttributeError'>
|
||||||
|
>>> t._get_exception('CalledProcessError')
|
||||||
|
<class 'subprocess.CalledProcessError'>
|
||||||
|
>>> t._get_exception('GenericError')
|
||||||
|
<class 'wk.std.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():
|
def get_log_filepath():
|
||||||
"""Get the log filepath from the root logger, returns pathlib.Path obj.
|
"""Get the log filepath from the root logger, returns pathlib.Path obj.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue