adding latest changes to read-from-config

This commit is contained in:
2019-12-05 03:07:35 +00:00
parent 3af8d00661
commit e0b77d7150
4 changed files with 49 additions and 33 deletions

View File

@@ -36,7 +36,7 @@ if CONFIG.logging_path:
set_config(CONFIG, 'logging.backup_count', 3, int) set_config(CONFIG, 'logging.backup_count', 3, int)
set_config(CONFIG, 'logging.rotate_bytes', 512000, int) set_config(CONFIG, 'logging.rotate_bytes', 512000, int)
# Add to file handler # Configure file handler
loghandler_file = RotatingFileHandler( loghandler_file = RotatingFileHandler(
os.path.expanduser(CONFIG.logging_path), os.path.expanduser(CONFIG.logging_path),
'a', 'a',
@@ -44,20 +44,16 @@ if CONFIG.logging_path:
CONFIG.logging_backup_count, CONFIG.logging_backup_count,
) )
# Set file formatter # Add to file formatter
loghandler_file.setFormatter(logging.Formatter(CONFIG.logging_format)) loghandler_file.setFormatter(logging.Formatter(CONFIG.logging_format))
logger.addHandler(loghandler_file) logger.addHandler(loghandler_file)
# Set stdout formatter # Configure and add to stdout formatter
loghandler_sys.setFormatter(logging.Formatter(CONFIG.logging_format)) loghandler_sys.setFormatter(logging.Formatter(CONFIG.logging_format))
logger.addHandler(loghandler_sys) logger.addHandler(loghandler_sys)
logger.setLevel(CONFIG.logging_level) logger.setLevel(CONFIG.logging_level)
# Load module environment variables
# 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 # Print logged messages

View File

@@ -0,0 +1 @@
__version__ = '0.1.0'

View File

@@ -1,12 +1,13 @@
# Change MYMODULE
from typing import Callable, Union from typing import Callable, Union
import os import os
import toml import toml
from plex_posters.library import export from MYMODULE.library import export
from plex_posters.__header__ import __header__ as header from MYMODULE.__header__ import __header__ as header
__all__ = [] # __all__ = ['Config']
@export @export
@@ -19,7 +20,7 @@ class Config:
---------- ----------
config_file : dict config_file : dict
Contains the config options. See Contains the config options. See
:meth:`~plex_posters.config.config.Config.read_config` :meth:`~MYMODULE.config.Config.read_config`
for the data structure. for the data structure.
deferred_messages : list deferred_messages : list
A list containing the messages to be logged once the logger has been A list containing the messages to be logged once the logger has been
@@ -36,7 +37,7 @@ class Config:
def __init__(self, path: str) -> None: def __init__(self, path: str) -> None:
""" """
See :class:`~plex_posters.config.config.Config` for parameters. See :class:`~MYMODULE.config.config.Config` for parameters.
""" """
self.deferred_messages = [] self.deferred_messages = []
self.config_file = self.read_config(path) self.config_file = self.read_config(path)
@@ -50,7 +51,7 @@ class Config:
path : str path : str
Path to config file. Should not contain `config.toml` Path to config file. Should not contain `config.toml`
Example: ``path = '~/.config/plex_posters'`` Example: ``path = '~/.config/MYMODULE'``
Returns Returns
------- -------
@@ -63,14 +64,14 @@ class Config:
Example:: Example::
[plex_posters] [MYMODULE]
[plex_posters.foo] [MYMODULE.foo]
foo = bar foo = bar
Returns a dict: Returns a dict:
``{'plex_posters' : {foo: {'foo': 'bar'}}}`` ``{'MYMODULE' : {foo: {'foo': 'bar'}}}``
""" """
path += 'config.toml' if path[-1] == '/' else '/config.toml' path += 'config.toml' if path[-1] == '/' else '/config.toml'
@@ -78,6 +79,7 @@ class Config:
try: try:
with open(path, 'r+') as config_file: with open(path, 'r+') as config_file:
config_file = toml.load(config_file) config_file = toml.load(config_file)
self.defer_log(f'Config file found at {path}')
return config_file return config_file
except FileNotFoundError: except FileNotFoundError:
self.defer_log(f'Config file not found at {path}') self.defer_log(f'Config file not found at {path}')
@@ -87,19 +89,22 @@ class Config:
self, key: str, default: str = None, cast: Callable = None self, key: str, default: str = None, cast: Callable = None
) -> Union[str, None]: ) -> Union[str, None]:
"""Retrives the config variable from either the `config.toml` or an """Retrives the config variable from either the `config.toml` or an
environment variable. Will default to the default value if it is environment variable. Will default to the default value if nothing
provided. is found
Parameters Parameters
---------- ----------
key : str key : str
Key to the configuration variable. Should be in the form Key to the configuration variable. Should be in the form
`module.variable` which will be converted to `module_variable`. `MYMODULE.variable` or `MYMODULE.header.variable`.
When loaded, it will be accessable at
`Config.MYMODULE_variable` or
`Config.MYMODULE_header_variable`.
default : str, optional default : str, optional
The default value if nothing is found. The default value if nothing is found. Defaults to `None`.
cast : Callable, optional cast : Callable, optional
The type of the variable. E.g `int` or `float`. Should reference The type of the variable. E.g `int` or `float`. Should reference
the type object and not as string. the type object and not as string. Defaults to `None`.
Returns Returns
------- -------
@@ -107,14 +112,26 @@ class Config:
Will return the config variable if found, or the default. Will return the config variable if found, or the default.
""" """
env_key = f"{header.upper()}_{key.upper().replace('.', '_')}" env_key = f"{header.upper()}_{key.upper().replace('.', '_')}"
# self.defer_log(self.config_file)
try: try:
# look in the config.toml # look in the config.toml
try:
# look for subsections
section, name = key.lower().split('.') section, name = key.lower().split('.')
value = self.config_file[self.module_name][section][name] value = self.config_file[self.module_name][section][name]
self.defer_log(f'{env_key} found in config.toml') self.defer_log(f'{env_key} found in config.toml')
except ValueError:
# look under top level module header
name = key.lower()
value = self.config_file[self.module_name][name]
self.defer_log(f'{env_key} found in config.toml')
finally:
try:
# return if found in config.toml
return cast(value) if cast else value return cast(value) if cast else value
except UnboundLocalError:
# pass if nothing was found
pass
except KeyError: except KeyError:
self.defer_log(f'{env_key} not found in config.toml') self.defer_log(f'{env_key} not found in config.toml')
except TypeError: except TypeError:

View File

@@ -1,3 +1,4 @@
# Change MYMODULE
from __future__ import annotations from __future__ import annotations
import sys import sys
from typing import Any, TypeVar, Type, TYPE_CHECKING from typing import Any, TypeVar, Type, TYPE_CHECKING
@@ -6,7 +7,7 @@ if TYPE_CHECKING:
import logging import logging
config_inst_t = TypeVar('config_inst_t', bound='module.config.config.Config') config_inst_t = TypeVar('config_inst_t', bound='MYMODULE.config.Config')
def export(fn: callable) -> callable: def export(fn: callable) -> callable:
@@ -29,7 +30,7 @@ def set_config(
Parameters Parameters
---------- ----------
config_inst : Type[config_inst_t] config_inst : Type[config_inst_t]
Instance of the config class. Instance of the :class:`~MYMODULE.config.Config` class.
key : str key : str
The key referencing the config variable. The key referencing the config variable.
default : str, optional default : str, optional
@@ -46,14 +47,15 @@ def process_cached_logs(
config_inst: Type[config_inst_t], config_inst: Type[config_inst_t],
logger: logging.Logger logger: logging.Logger
): ):
"""Prints the cached messages from the CONFIG class and resets the cache. """Prints the cached messages from :class:`~MYMODULE.config.Config`
and resets the cache.
Parameters Parameters
---------- ----------
config_inst : Type[config_inst_t] config_inst : Type[config_inst_t]
Instance of the CONFIG class. Instance of :class:`~MYMODULE.config.Config`.
logger : logging.Logger logger : logging.Logger
Instance of the active logger. Instance of the logger.
""" """
for msg in config_inst.deferred_messages: for msg in config_inst.deferred_messages:
logger.info(msg) logger.info(msg)