implement 'subconfigs' option (#4440)

This commit is contained in:
Mike Fährmann
2023-08-21 21:18:40 +02:00
parent 2b88ad19e9
commit 410f783a33
3 changed files with 21 additions and 4 deletions

View File

@@ -5124,6 +5124,16 @@ Description
as signal handler for.
subconfigs
----------
Type
``list`` of |Path|_
Example
``["~/cfg-twitter.json", "~/cfg-reddit.json"]``
Description
Additional configuration files to load.
warnings
--------
Type

View File

@@ -44,13 +44,13 @@ def main():
config.load(args.configs_json, strict=True)
if args.configs_yaml:
import yaml
config.load(args.configs_yaml, strict=True, load=yaml.safe_load)
config.load(args.configs_yaml, strict=True, loads=yaml.safe_load)
if args.configs_toml:
try:
import tomllib as toml
except ImportError:
import toml
config.load(args.configs_toml, strict=True, load=toml.loads)
config.load(args.configs_toml, strict=True, loads=toml.loads)
if args.filename:
filename = args.filename
if filename == "/O":

View File

@@ -90,13 +90,13 @@ def initialize():
return 0
def load(files=None, strict=False, load=util.json_loads):
def load(files=None, strict=False, loads=util.json_loads):
"""Load JSON configuration files"""
for pathfmt in files or _default_configs:
path = util.expand_path(pathfmt)
try:
with open(path, encoding="utf-8") as file:
conf = load(file.read())
conf = loads(file.read())
except OSError as exc:
if strict:
log.error(exc)
@@ -113,6 +113,13 @@ def load(files=None, strict=False, load=util.json_loads):
util.combine_dict(_config, conf)
_files.append(pathfmt)
if "subconfigs" in conf:
subconfigs = conf["subconfigs"]
if subconfigs:
if isinstance(subconfigs, str):
subconfigs = (subconfigs,)
load(subconfigs, strict, loads)
def clear():
"""Reset configuration to an empty state"""