documentation, cleanup

This commit is contained in:
Anatol Ulrich
2023-10-04 22:25:21 +02:00
parent fa52233404
commit d33b57cff8
7 changed files with 171 additions and 61 deletions

View File

@@ -8,17 +8,23 @@ or in a toml file using
foo = ...
bar_baz = ...
in the latter case, the section and name are uppercased and joined with `_`,
In the latter case, the section and name are uppercased and joined with `_`,
then inserted into the OS environment (in this case, resulting in
SECTION_FOO=... and SECTION_BAR_BAZ=... ).
The OS environment takes precedence - if a key is found in both env and config,
the config setting will be ignored.
"""
import tomllib
import logging
# stdlib imports
from os import environ
from functools import wraps
# dependencies imports
import tomllib
import logging
# app imports
import consts
logger = logging.getLogger(consts.LOG_ROOT)
@@ -26,25 +32,27 @@ logger = logging.getLogger(consts.LOG_ROOT)
def load(file):
"""
Load configuration from toml and add to environment
Load configuration from toml and add to environment.
Skip anything already present there.
Configuration key names are converted according to the module documentation.
"""
if file is None:
logger.debug('no config file provided.')
else:
logger.debug('loading config')
with open(file, 'rb') as fh:
config = tomllib.load(fh)
for k, v in config.items():
for k_inner, v_inner in v.items():
effective_key = f'{k.upper()}_{k_inner.upper()}'
if effective_key in environ:
logger.warning(
'%s already exists in os environment, skipping', effective_key)
else:
environ[effective_key] = str(v_inner)
logger.debug('raw: %s = %s', effective_key,
environ[effective_key])
with open(file, 'rb') as handle:
config = tomllib.load(handle)
for section, vals in config.items():
for key, val in vals.items():
effective_key = f'{section.upper()}_{key.upper()}'
if effective_key in environ:
logger.warning(
'%s already exists in os environment, skipping', effective_key)
else:
environ[effective_key] = str(val)
logger.debug('config/raw: %s = %s', effective_key,
environ[effective_key])
def env(key, default=None):
@@ -59,12 +67,12 @@ def env(key, default=None):
def logged(f):
"""
logging wrapper for resolved configuration settings
logging wrapper for resolved configuration values
"""
@wraps(f)
def decorated(*args, **kwargs):
res = f(*args, **kwargs)
logger.debug('resolved: %s = %s', args[0], res)
logger.debug('config/resolved: %s = %s', args[0], res)
return res
return decorated
@@ -79,7 +87,7 @@ def int_val(key, default=None):
def strtobool(val):
"""Convert a string representation of truth to true (1) or false (0).
"""Convert a string representation of truth to `True` or `False`.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.