updating with latest changes to CONFIG

This commit is contained in:
dtomlinson
2019-12-04 11:50:55 +00:00
parent e2f07a0d9f
commit 3af8d00661
4 changed files with 61 additions and 19 deletions

View File

@@ -0,0 +1 @@
__header__ = 'module_name'

View File

@@ -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)

View File

@@ -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)

View File

@@ -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()