build: fixed flake8 warnings

This commit is contained in:
Mathew Guest 2024-11-16 01:54:04 -07:00
parent d76932bcb9
commit ac4b765099
6 changed files with 46 additions and 49 deletions

@ -1,7 +1,19 @@
import logging # flake8: noqa
import sys
from .app_container import * from .app_container import (
from .cfg import * DEFAULT_APP_AUTHOR,
from .cli import * DEFAULT_APP_NAME,
from .log import * ApplicationContainer,
ApplicationContext,
NoCommandSpecified,
ServiceNotFound,
)
from .cfg import Config, EnvironmentVariables
from .cli import (
EXPLICIT_FAIL_ON_UNKNOWN_ARGS,
CommandEntry,
CommandTree,
HelpGenerator,
SubMenu,
)
from .log import LoggingLayer

@ -4,8 +4,6 @@ import inspect
import os import os
import sys import sys
from . import _util
def eprint(*args, **kwargs): def eprint(*args, **kwargs):
""" """
@ -30,9 +28,9 @@ def does_file_exist(filepath):
instant in execution. instant in execution.
""" """
try: try:
fp = open(filepath, "r") open(filepath, "r")
return True return True
except FileNotFoundError as ex: except FileNotFoundError:
return False return False
@ -88,7 +86,7 @@ def get_asset(module, filepath):
root = os.path.realpath(root) root = os.path.realpath(root)
root = os.path.dirname(os.path.abspath(root)) root = os.path.dirname(os.path.abspath(root))
except Exception as ex: except Exception:
raise raise
path = os.path.join(root, filepath) path = os.path.join(root, filepath)

@ -1,9 +1,6 @@
import collections
import functools import functools
import inspect import inspect
import logging
import os import os
import sys
import appdirs import appdirs
@ -86,7 +83,7 @@ class ApplicationContainer:
""" """
try: try:
del self._dependencies[service_name] del self._dependencies[service_name]
except KeyError as ex: except KeyError:
pass pass
def __getitem__(self, service_name): def __getitem__(self, service_name):
@ -101,7 +98,7 @@ class ApplicationContainer:
service_name service_name
] # Retrieve factory function ] # Retrieve factory function
return service_factory() # Call factory() to return instance of service return service_factory() # Call factory() to return instance of service
except KeyError as ex: except KeyError:
msg = "failed to inject service: {}".format(service_name) msg = "failed to inject service: {}".format(service_name)
_bootstrap_logger.critical(msg) _bootstrap_logger.critical(msg)
raise ServiceNotFound raise ServiceNotFound
@ -180,7 +177,7 @@ class ApplicationContainer:
return False return False
try: try:
self.cli.run_command() self.cli.run_command()
except NoCommandSpecified as ex: except NoCommandSpecified:
print("Failure: No command specified.") print("Failure: No command specified.")
def interactive_shell(self): def interactive_shell(self):

@ -4,15 +4,10 @@
# ConfigObj module and it's recommended to use config.spec files to define # ConfigObj module and it's recommended to use config.spec files to define
# your available configuration of the relevant application. # your available configuration of the relevant application.
import argparse
import os
import sys
import appdirs
import configobj import configobj
import validate import validate
from . import _util
from ._bootstrap import _bootstrap_logger from ._bootstrap import _bootstrap_logger
@ -100,7 +95,7 @@ class Config:
) )
self.load_config() self.load_config()
return return
except OSError as ex: except OSError:
_bootstrap_logger.critical( _bootstrap_logger.critical(
"cfg - Failed to find config.spec: file not found (%s)", filepath "cfg - Failed to find config.spec: file not found (%s)", filepath
) )
@ -112,7 +107,7 @@ class Config:
try: try:
has_item = key in self._config_obj has_item = key in self._config_obj
return has_item return has_item
except KeyError as ex: except KeyError:
pass pass
def __delitem__(self, key): def __delitem__(self, key):
@ -122,7 +117,7 @@ class Config:
""" """
try: try:
del self[key] del self[key]
except KeyError as ex: except KeyError:
pass pass
def __getitem__(self, key): def __getitem__(self, key):
@ -136,7 +131,7 @@ class Config:
else: else:
# return self._config_obj[key].dict() # return self._config_obj[key].dict()
return self._config_obj[key] return self._config_obj[key]
except KeyError as ex: except KeyError:
raise raise
def __setitem__(self, key, value): def __setitem__(self, key, value):
@ -156,7 +151,7 @@ class Config:
try: try:
v = self.__getitem__(key) v = self.__getitem__(key)
return v return v
except KeyError as ex: except KeyError:
return default return default
def load_config(self, configspec_filepath=None, configini_filepath=None): def load_config(self, configspec_filepath=None, configini_filepath=None):
@ -199,18 +194,17 @@ class Config:
) )
return True return True
except configobj.ParseError as ex: except configobj.ParseError:
msg = "cfg - Failed to load config: error in config.spec configuration: {}".format( msg = f"cfg - Failed to load config: error in config.spec configuration: {self.configspec_filepath}"
config_filepath
)
_bootstrap_logger.error(msg) _bootstrap_logger.error(msg)
return False return False
except OSError as ex: except OSError:
msg = "cfg - Failed to load config: config.spec file not found." msg = "cfg - Failed to load config: config.spec file not found."
_bootstrap_logger.error(msg) _bootstrap_logger.error(msg)
return False return False
except Exception as ex: except Exception as ex:
print(ex) _bootstrap_logger.error(ex)
return False
def _validate_config_against_spec(self): def _validate_config_against_spec(self):
config_spec = self.configspec_filepath config_spec = self.configspec_filepath
@ -246,7 +240,7 @@ class Config:
else: else:
self._validate_parse_errors(test_results) self._validate_parse_errors(test_results)
return False return False
except ValueError as ex: except ValueError:
_bootstrap_logger.error( _bootstrap_logger.error(
"cfg - Failed while validating config against spec. " "cfg - Failed while validating config against spec. "
) )

@ -1,11 +1,8 @@
import argparse import argparse
import inspect import inspect
import logging
import re import re
import sys import sys
import app_skellington
from . import app_container from . import app_container
from ._bootstrap import _bootstrap_logger from ._bootstrap import _bootstrap_logger
@ -69,7 +66,7 @@ class CommandTree:
self._single_command = None self._single_command = None
def print_tree(self): def print_tree(self):
raise NotImplemented raise NotImplementedError
def add_argument(self, *args, **kwargs): def add_argument(self, *args, **kwargs):
""" """
@ -144,13 +141,13 @@ class CommandTree:
# help is displayed next to the command in the submenu enumeration or # help is displayed next to the command in the submenu enumeration or
# list of commands: # list of commands:
help_text = HelpGenerator.generate_help_from_sig(docstring) HelpGenerator.generate_help_from_sig(docstring)
# description is displayed when querying help for the specific command: # description is displayed when querying help for the specific command:
description_text = HelpGenerator.generate_description_from_sig(docstring) HelpGenerator.generate_description_from_sig(docstring)
# end copy-paste from SubMenu.register_command # end copy-paste from SubMenu.register_command
# begin copy-paste then editted from SubMenu.register_command # begin copy-paste then edited from SubMenu.register_command
# For each paramter in the function create an argparse argument in # For each parameter in the function create an argparse argument in
# the child ArgumentParser created for this menu entry: # the child ArgumentParser created for this menu entry:
for key in params: for key in params:
if key == "self": if key == "self":
@ -210,7 +207,7 @@ class CommandTree:
return pargs, unk, True return pargs, unk, True
# Note: SystemExit is raised when '-h' argument is supplied. # Note: SystemExit is raised when '-h' argument is supplied.
except SystemExit as ex: except SystemExit:
return None, None, False return None, None, False
def run_command(self, args=None): def run_command(self, args=None):
@ -254,7 +251,7 @@ class CommandTree:
assert self._single_command is None, "corrupt data structure in CommandMenu" assert self._single_command is None, "corrupt data structure in CommandMenu"
assert ( assert (
self._cmd_tree_is_single_command == False self._cmd_tree_is_single_command is False
), "corrupt data structure in CommandMenu" ), "corrupt data structure in CommandMenu"
# Key or variable name used by argparse to store the submenu options # Key or variable name used by argparse to store the submenu options
@ -510,7 +507,7 @@ class HelpGenerator:
The 'help' text is displayed next to the command when enumerating The 'help' text is displayed next to the command when enumerating
the submenu commands. the submenu commands.
""" """
if doctext == None: if doctext is None:
return doctext return doctext
regex = "(.*?)[.?!]" regex = "(.*?)[.?!]"
match = re.match(regex, doctext, re.MULTILINE | re.DOTALL) match = re.match(regex, doctext, re.MULTILINE | re.DOTALL)
@ -524,7 +521,7 @@ class HelpGenerator:
The 'description' paragraph is provided when the user requests help The 'description' paragraph is provided when the user requests help
on a specific command. on a specific command.
""" """
if doctext == None: if doctext is None:
return doctext return doctext
regex = "(.*?)[.?!]" regex = "(.*?)[.?!]"
match = re.match(regex, doctext, re.MULTILINE | re.DOTALL) match = re.match(regex, doctext, re.MULTILINE | re.DOTALL)

@ -3,7 +3,6 @@ import logging.config
import os import os
import appdirs import appdirs
import colorlog
from . import _util from . import _util
from ._bootstrap import _bootstrap_logger, _logger_name from ._bootstrap import _bootstrap_logger, _logger_name
@ -115,7 +114,7 @@ class LoggingLayer:
if config_dict["loggers"].get("root") is not None: if config_dict["loggers"].get("root") is not None:
config_dict["loggers"][""] = config_dict["loggers"]["root"] config_dict["loggers"][""] = config_dict["loggers"]["root"]
del config_dict["loggers"]["root"] del config_dict["loggers"]["root"]
except Exception as ex: except Exception:
_bootstrap_logger.warn( _bootstrap_logger.warn(
"was not able to find and patch root logger configuration from arguments" "was not able to find and patch root logger configuration from arguments"
) )
@ -169,7 +168,7 @@ class LoggingLayer:
""" """
try: try:
s = dict_[key] s = dict_[key]
except KeyError as ex: except KeyError:
raise raise
if s == "critical": if s == "critical":
dict_[key] = logging.CRITICAL dict_[key] = logging.CRITICAL