From 94559201419d20d65e8fc385cc518e5b95b87c4d Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 30 Oct 2020 23:59:07 +0100 Subject: [PATCH] [System] config now deep update when multiple config files are used --- flaschengeist/config.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/flaschengeist/config.py b/flaschengeist/config.py index cda1e4f..100d6e3 100644 --- a/flaschengeist/config.py +++ b/flaschengeist/config.py @@ -1,24 +1,37 @@ -import logging.config import os import toml +import logging.config +import collections.abc + from pathlib import Path from werkzeug.middleware.proxy_fix import ProxyFix from flaschengeist import _module_path, logger + +def __update(d, u): + for k, v in u.items(): + if isinstance(v, collections.abc.Mapping): + d[k] = __update(d.get(k, {}), v) + else: + d[k] = v + return d + + # Default config: config = {} -paths = [_module_path, Path.home() / ".config"] + +__paths = [_module_path, Path.home() / ".config"] if "FLASCHENGEIST_CONF" in os.environ: - paths.append(Path(os.environ.get("FLASCHENGEIST_CONF"))) -for loc in paths: + __paths.append(Path(os.environ.get("FLASCHENGEIST_CONF"))) +for loc in __paths: try: with (loc / "flaschengeist.toml").open() as source: logger.info("Reading config file from >{}<".format(loc)) - config.update(toml.load(source)) + __update(config, toml.load(source)) except IOError: pass # Always enable this builtin plugins! -config.update({"auth": {"enabled": True}, "roles": {"enabled": True}, "users": {"enabled": True}}) +__update(config, {"auth": {"enabled": True}, "roles": {"enabled": True}, "users": {"enabled": True}}) def configure_app(app):