From 3af8d006616b6412f05c21a3f6297a3db61f3332 Mon Sep 17 00:00:00 2001 From: dtomlinson Date: Wed, 4 Dec 2019 11:50:55 +0000 Subject: [PATCH] updating with latest changes to CONFIG --- .../read-from-config/__header__.py | 1 + .../read-from-config/__init__.py | 34 +++++++++++++++---- .../read-from-config/config/config.py | 18 +++++----- .../read-from-config/library/__init__.py | 27 +++++++++++++-- 4 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 python/module-packages/read-from-config/__header__.py diff --git a/python/module-packages/read-from-config/__header__.py b/python/module-packages/read-from-config/__header__.py new file mode 100644 index 0000000..b27130e --- /dev/null +++ b/python/module-packages/read-from-config/__header__.py @@ -0,0 +1 @@ +__header__ = 'module_name' diff --git a/python/module-packages/read-from-config/__init__.py b/python/module-packages/read-from-config/__init__.py index 6e8beba..d307591 100644 --- a/python/module-packages/read-from-config/__init__.py +++ b/python/module-packages/read-from-config/__init__.py @@ -1,12 +1,14 @@ from __future__ import annotations -from .config.config import Config -from .library import set_config -from .__header__ import __header__ import logging from logging.handlers import RotatingFileHandler import os import sys +from .config import Config +from .library import set_config, process_cached_logs +from .__header__ import __header__ + + # Load User Defined Config DEFAULT_CONFIG_PATH = f'~/.config/{__header__.lower()}' CONFIG_PATH = os.environ.get(f'{__header__}_CONFIG_PATH', DEFAULT_CONFIG_PATH) @@ -23,22 +25,40 @@ set_config( set_config(CONFIG, 'logging.level', 'INFO') loghandler_sys = logging.StreamHandler(sys.stdout) +# Checking if log path is set if CONFIG.logging_path: + CONFIG.logging_path += ( + f'{__header__}.log' + if CONFIG.logging_path[-1] == '/' + else f'/{__header__}.log' + ) + # Set default log file options set_config(CONFIG, 'logging.backup_count', 3, int) set_config(CONFIG, 'logging.rotate_bytes', 512000, int) + + # Add to file handler loghandler_file = RotatingFileHandler( os.path.expanduser(CONFIG.logging_path), 'a', CONFIG.logging_rotate_bytes, - CONFIG.logging_backup_count + CONFIG.logging_backup_count, ) + + # Set file formatter loghandler_file.setFormatter(logging.Formatter(CONFIG.logging_format)) logger.addHandler(loghandler_file) +# Set stdout formatter loghandler_sys.setFormatter(logging.Formatter(CONFIG.logging_format)) logger.addHandler(loghandler_sys) logger.setLevel(CONFIG.logging_level) -for msg in CONFIG.deferred_messages: - logger.info(msg) -CONFIG.reset_log() + +# Load Slack environment variables +set_config(CONFIG, 'slack.client_id') +set_config(CONFIG, 'slack.client_secret') +set_config(CONFIG, 'slack.signing_secret') + + +# Print logged messages +process_cached_logs(CONFIG, logger) diff --git a/python/module-packages/read-from-config/config/config.py b/python/module-packages/read-from-config/config/config.py index f2231b5..fb55a1e 100644 --- a/python/module-packages/read-from-config/config/config.py +++ b/python/module-packages/read-from-config/config/config.py @@ -1,9 +1,11 @@ -from plex_posters.library import export -from plex_posters.__header__ import __header__ as header from typing import Callable, Union import os import toml +from plex_posters.library import export +from plex_posters.__header__ import __header__ as header + + __all__ = [] @@ -36,9 +38,9 @@ class Config: """ See :class:`~plex_posters.config.config.Config` for parameters. """ + self.deferred_messages = [] self.config_file = self.read_config(path) self.module_name = header.lower() - self.deferred_messages = [] def read_config(self, path: str) -> Union[dict, None]: """Reads the toml config file from `path` if it exists. @@ -73,16 +75,12 @@ class Config: path += 'config.toml' if path[-1] == '/' else '/config.toml' path = os.path.expanduser(path) - try: with open(path, 'r+') as config_file: config_file = toml.load(config_file) return config_file except FileNotFoundError: - try: - self.defer_log(f'Config file not found at {config_file}') - except UnboundLocalError: - pass + self.defer_log(f'Config file not found at {path}') pass def get( @@ -108,7 +106,7 @@ class Config: Any Will return the config variable if found, or the default. """ - env_key = f"{header}_{key.upper().replace('.', '_')}" + env_key = f"{header.upper()}_{key.upper().replace('.', '_')}" # self.defer_log(self.config_file) try: @@ -120,7 +118,7 @@ class Config: except KeyError: self.defer_log(f'{env_key} not found in config.toml') except TypeError: - pass + self.defer_log(f'{env_key} not found in config.toml') # look for an environment variable value = os.environ.get(env_key) diff --git a/python/module-packages/read-from-config/library/__init__.py b/python/module-packages/read-from-config/library/__init__.py index 82f6d7c..f0a3525 100644 --- a/python/module-packages/read-from-config/library/__init__.py +++ b/python/module-packages/read-from-config/library/__init__.py @@ -1,8 +1,12 @@ +from __future__ import annotations import sys -from typing import Any, TypeVar, Type +from typing import Any, TypeVar, Type, TYPE_CHECKING + +if TYPE_CHECKING: + import logging -config_inst_t = TypeVar('config_inst_t', bound='config.config.Config') +config_inst_t = TypeVar('config_inst_t', bound='module.config.config.Config') def export(fn: callable) -> callable: @@ -35,3 +39,22 @@ def set_config( """ config_var = key.lower().replace('.', '_') setattr(config_inst, config_var, config_inst.get(key, default, cast)) + + +# Create function to print cached logged messages and reset +def process_cached_logs( + config_inst: Type[config_inst_t], + logger: logging.Logger +): + """Prints the cached messages from the CONFIG class and resets the cache. + + Parameters + ---------- + config_inst : Type[config_inst_t] + Instance of the CONFIG class. + logger : logging.Logger + Instance of the active logger. + """ + for msg in config_inst.deferred_messages: + logger.info(msg) + config_inst.reset_log()