From ac4b765099d5ea70ee1ee0e15787fda408cebdbb Mon Sep 17 00:00:00 2001 From: Mathew Guest Date: Sat, 16 Nov 2024 01:54:04 -0700 Subject: [PATCH] build: fixed flake8 warnings --- app_skellington/__init__.py | 24 ++++++++++++++++++------ app_skellington/_util.py | 8 +++----- app_skellington/app_container.py | 9 +++------ app_skellington/cfg.py | 28 +++++++++++----------------- app_skellington/cli.py | 21 +++++++++------------ app_skellington/log.py | 5 ++--- 6 files changed, 46 insertions(+), 49 deletions(-) diff --git a/app_skellington/__init__.py b/app_skellington/__init__.py index 8c1461f..1f8ce08 100644 --- a/app_skellington/__init__.py +++ b/app_skellington/__init__.py @@ -1,7 +1,19 @@ -import logging -import sys +# flake8: noqa -from .app_container import * -from .cfg import * -from .cli import * -from .log import * +from .app_container import ( + DEFAULT_APP_AUTHOR, + DEFAULT_APP_NAME, + 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 diff --git a/app_skellington/_util.py b/app_skellington/_util.py index 433b9da..84f5f8e 100644 --- a/app_skellington/_util.py +++ b/app_skellington/_util.py @@ -4,8 +4,6 @@ import inspect import os import sys -from . import _util - def eprint(*args, **kwargs): """ @@ -30,9 +28,9 @@ def does_file_exist(filepath): instant in execution. """ try: - fp = open(filepath, "r") + open(filepath, "r") return True - except FileNotFoundError as ex: + except FileNotFoundError: return False @@ -88,7 +86,7 @@ def get_asset(module, filepath): root = os.path.realpath(root) root = os.path.dirname(os.path.abspath(root)) - except Exception as ex: + except Exception: raise path = os.path.join(root, filepath) diff --git a/app_skellington/app_container.py b/app_skellington/app_container.py index 237a7b9..9911d85 100644 --- a/app_skellington/app_container.py +++ b/app_skellington/app_container.py @@ -1,9 +1,6 @@ -import collections import functools import inspect -import logging import os -import sys import appdirs @@ -86,7 +83,7 @@ class ApplicationContainer: """ try: del self._dependencies[service_name] - except KeyError as ex: + except KeyError: pass def __getitem__(self, service_name): @@ -101,7 +98,7 @@ class ApplicationContainer: service_name ] # Retrieve factory function return service_factory() # Call factory() to return instance of service - except KeyError as ex: + except KeyError: msg = "failed to inject service: {}".format(service_name) _bootstrap_logger.critical(msg) raise ServiceNotFound @@ -180,7 +177,7 @@ class ApplicationContainer: return False try: self.cli.run_command() - except NoCommandSpecified as ex: + except NoCommandSpecified: print("Failure: No command specified.") def interactive_shell(self): diff --git a/app_skellington/cfg.py b/app_skellington/cfg.py index ea112fd..13b76dd 100644 --- a/app_skellington/cfg.py +++ b/app_skellington/cfg.py @@ -4,15 +4,10 @@ # ConfigObj module and it's recommended to use config.spec files to define # your available configuration of the relevant application. -import argparse -import os -import sys -import appdirs import configobj import validate -from . import _util from ._bootstrap import _bootstrap_logger @@ -100,7 +95,7 @@ class Config: ) self.load_config() return - except OSError as ex: + except OSError: _bootstrap_logger.critical( "cfg - Failed to find config.spec: file not found (%s)", filepath ) @@ -112,7 +107,7 @@ class Config: try: has_item = key in self._config_obj return has_item - except KeyError as ex: + except KeyError: pass def __delitem__(self, key): @@ -122,7 +117,7 @@ class Config: """ try: del self[key] - except KeyError as ex: + except KeyError: pass def __getitem__(self, key): @@ -136,7 +131,7 @@ class Config: else: # return self._config_obj[key].dict() return self._config_obj[key] - except KeyError as ex: + except KeyError: raise def __setitem__(self, key, value): @@ -156,7 +151,7 @@ class Config: try: v = self.__getitem__(key) return v - except KeyError as ex: + except KeyError: return default def load_config(self, configspec_filepath=None, configini_filepath=None): @@ -199,18 +194,17 @@ class Config: ) return True - except configobj.ParseError as ex: - msg = "cfg - Failed to load config: error in config.spec configuration: {}".format( - config_filepath - ) + except configobj.ParseError: + msg = f"cfg - Failed to load config: error in config.spec configuration: {self.configspec_filepath}" _bootstrap_logger.error(msg) return False - except OSError as ex: + except OSError: msg = "cfg - Failed to load config: config.spec file not found." _bootstrap_logger.error(msg) return False except Exception as ex: - print(ex) + _bootstrap_logger.error(ex) + return False def _validate_config_against_spec(self): config_spec = self.configspec_filepath @@ -246,7 +240,7 @@ class Config: else: self._validate_parse_errors(test_results) return False - except ValueError as ex: + except ValueError: _bootstrap_logger.error( "cfg - Failed while validating config against spec. " ) diff --git a/app_skellington/cli.py b/app_skellington/cli.py index 2c791ca..79cc280 100644 --- a/app_skellington/cli.py +++ b/app_skellington/cli.py @@ -1,11 +1,8 @@ import argparse import inspect -import logging import re import sys -import app_skellington - from . import app_container from ._bootstrap import _bootstrap_logger @@ -69,7 +66,7 @@ class CommandTree: self._single_command = None def print_tree(self): - raise NotImplemented + raise NotImplementedError def add_argument(self, *args, **kwargs): """ @@ -144,13 +141,13 @@ class CommandTree: # help is displayed next to the command in the submenu enumeration or # 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_text = HelpGenerator.generate_description_from_sig(docstring) + HelpGenerator.generate_description_from_sig(docstring) # end copy-paste from SubMenu.register_command - # begin copy-paste then editted from SubMenu.register_command - # For each paramter in the function create an argparse argument in + # begin copy-paste then edited from SubMenu.register_command + # For each parameter in the function create an argparse argument in # the child ArgumentParser created for this menu entry: for key in params: if key == "self": @@ -210,7 +207,7 @@ class CommandTree: return pargs, unk, True # Note: SystemExit is raised when '-h' argument is supplied. - except SystemExit as ex: + except SystemExit: return None, None, False 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._cmd_tree_is_single_command == False + self._cmd_tree_is_single_command is False ), "corrupt data structure in CommandMenu" # 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 submenu commands. """ - if doctext == None: + if doctext is None: return doctext regex = "(.*?)[.?!]" 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 on a specific command. """ - if doctext == None: + if doctext is None: return doctext regex = "(.*?)[.?!]" match = re.match(regex, doctext, re.MULTILINE | re.DOTALL) diff --git a/app_skellington/log.py b/app_skellington/log.py index 761ffef..2ec2a7a 100644 --- a/app_skellington/log.py +++ b/app_skellington/log.py @@ -3,7 +3,6 @@ import logging.config import os import appdirs -import colorlog from . import _util from ._bootstrap import _bootstrap_logger, _logger_name @@ -115,7 +114,7 @@ class LoggingLayer: if config_dict["loggers"].get("root") is not None: config_dict["loggers"][""] = config_dict["loggers"]["root"] del config_dict["loggers"]["root"] - except Exception as ex: + except Exception: _bootstrap_logger.warn( "was not able to find and patch root logger configuration from arguments" ) @@ -169,7 +168,7 @@ class LoggingLayer: """ try: s = dict_[key] - except KeyError as ex: + except KeyError: raise if s == "critical": dict_[key] = logging.CRITICAL