adding latest changes to read-from-config
This commit is contained in:
@@ -32,11 +32,11 @@ if CONFIG.logging_path:
|
||||
if CONFIG.logging_path[-1] == '/'
|
||||
else f'/{__header__}.log'
|
||||
)
|
||||
# Set default log file options
|
||||
# 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
|
||||
# Configure file handler
|
||||
loghandler_file = RotatingFileHandler(
|
||||
os.path.expanduser(CONFIG.logging_path),
|
||||
'a',
|
||||
@@ -44,20 +44,16 @@ if CONFIG.logging_path:
|
||||
CONFIG.logging_backup_count,
|
||||
)
|
||||
|
||||
# Set file formatter
|
||||
# Add to file formatter
|
||||
loghandler_file.setFormatter(logging.Formatter(CONFIG.logging_format))
|
||||
logger.addHandler(loghandler_file)
|
||||
|
||||
# Set stdout formatter
|
||||
# Configure and add to stdout formatter
|
||||
loghandler_sys.setFormatter(logging.Formatter(CONFIG.logging_format))
|
||||
logger.addHandler(loghandler_sys)
|
||||
logger.setLevel(CONFIG.logging_level)
|
||||
|
||||
|
||||
# Load Slack environment variables
|
||||
set_config(CONFIG, 'slack.client_id')
|
||||
set_config(CONFIG, 'slack.client_secret')
|
||||
set_config(CONFIG, 'slack.signing_secret')
|
||||
# Load module environment variables
|
||||
|
||||
|
||||
# Print logged messages
|
||||
|
||||
1
python/module-packages/read-from-config/__version__.py
Normal file
1
python/module-packages/read-from-config/__version__.py
Normal file
@@ -0,0 +1 @@
|
||||
__version__ = '0.1.0'
|
||||
@@ -1,12 +1,13 @@
|
||||
# Change MYMODULE
|
||||
from typing import Callable, Union
|
||||
import os
|
||||
import toml
|
||||
|
||||
from plex_posters.library import export
|
||||
from plex_posters.__header__ import __header__ as header
|
||||
from MYMODULE.library import export
|
||||
from MYMODULE.__header__ import __header__ as header
|
||||
|
||||
|
||||
__all__ = []
|
||||
# __all__ = ['Config']
|
||||
|
||||
|
||||
@export
|
||||
@@ -19,7 +20,7 @@ class Config:
|
||||
----------
|
||||
config_file : dict
|
||||
Contains the config options. See
|
||||
:meth:`~plex_posters.config.config.Config.read_config`
|
||||
:meth:`~MYMODULE.config.Config.read_config`
|
||||
for the data structure.
|
||||
deferred_messages : list
|
||||
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:
|
||||
"""
|
||||
See :class:`~plex_posters.config.config.Config` for parameters.
|
||||
See :class:`~MYMODULE.config.config.Config` for parameters.
|
||||
"""
|
||||
self.deferred_messages = []
|
||||
self.config_file = self.read_config(path)
|
||||
@@ -50,7 +51,7 @@ class Config:
|
||||
path : str
|
||||
Path to config file. Should not contain `config.toml`
|
||||
|
||||
Example: ``path = '~/.config/plex_posters'``
|
||||
Example: ``path = '~/.config/MYMODULE'``
|
||||
|
||||
Returns
|
||||
-------
|
||||
@@ -63,14 +64,14 @@ class Config:
|
||||
|
||||
Example::
|
||||
|
||||
[plex_posters]
|
||||
[MYMODULE]
|
||||
|
||||
[plex_posters.foo]
|
||||
[MYMODULE.foo]
|
||||
foo = bar
|
||||
|
||||
Returns a dict:
|
||||
|
||||
``{'plex_posters' : {foo: {'foo': 'bar'}}}``
|
||||
``{'MYMODULE' : {foo: {'foo': 'bar'}}}``
|
||||
"""
|
||||
|
||||
path += 'config.toml' if path[-1] == '/' else '/config.toml'
|
||||
@@ -78,6 +79,7 @@ class Config:
|
||||
try:
|
||||
with open(path, 'r+') as config_file:
|
||||
config_file = toml.load(config_file)
|
||||
self.defer_log(f'Config file found at {path}')
|
||||
return config_file
|
||||
except FileNotFoundError:
|
||||
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
|
||||
) -> Union[str, None]:
|
||||
"""Retrives the config variable from either the `config.toml` or an
|
||||
environment variable. Will default to the default value if it is
|
||||
provided.
|
||||
environment variable. Will default to the default value if nothing
|
||||
is found
|
||||
|
||||
Parameters
|
||||
----------
|
||||
key : str
|
||||
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
|
||||
The default value if nothing is found.
|
||||
The default value if nothing is found. Defaults to `None`.
|
||||
cast : Callable, optional
|
||||
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
|
||||
-------
|
||||
@@ -107,14 +112,26 @@ class Config:
|
||||
Will return the config variable if found, or the default.
|
||||
"""
|
||||
env_key = f"{header.upper()}_{key.upper().replace('.', '_')}"
|
||||
# self.defer_log(self.config_file)
|
||||
|
||||
try:
|
||||
# look in the config.toml
|
||||
section, name = key.lower().split('.')
|
||||
value = self.config_file[self.module_name][section][name]
|
||||
self.defer_log(f'{env_key} found in config.toml')
|
||||
return cast(value) if cast else value
|
||||
try:
|
||||
# look for subsections
|
||||
section, name = key.lower().split('.')
|
||||
value = self.config_file[self.module_name][section][name]
|
||||
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
|
||||
except UnboundLocalError:
|
||||
# pass if nothing was found
|
||||
pass
|
||||
except KeyError:
|
||||
self.defer_log(f'{env_key} not found in config.toml')
|
||||
except TypeError:
|
||||
@@ -1,3 +1,4 @@
|
||||
# Change MYMODULE
|
||||
from __future__ import annotations
|
||||
import sys
|
||||
from typing import Any, TypeVar, Type, TYPE_CHECKING
|
||||
@@ -6,7 +7,7 @@ if TYPE_CHECKING:
|
||||
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:
|
||||
@@ -29,7 +30,7 @@ def set_config(
|
||||
Parameters
|
||||
----------
|
||||
config_inst : Type[config_inst_t]
|
||||
Instance of the config class.
|
||||
Instance of the :class:`~MYMODULE.config.Config` class.
|
||||
key : str
|
||||
The key referencing the config variable.
|
||||
default : str, optional
|
||||
@@ -46,14 +47,15 @@ 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.
|
||||
"""Prints the cached messages from :class:`~MYMODULE.config.Config`
|
||||
and resets the cache.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
config_inst : Type[config_inst_t]
|
||||
Instance of the CONFIG class.
|
||||
Instance of :class:`~MYMODULE.config.Config`.
|
||||
logger : logging.Logger
|
||||
Instance of the active logger.
|
||||
Instance of the logger.
|
||||
"""
|
||||
for msg in config_inst.deferred_messages:
|
||||
logger.info(msg)
|
||||
Reference in New Issue
Block a user