update function signature for config.load()
This commit is contained in:
@@ -118,9 +118,9 @@ def main():
|
|||||||
if args.load_config:
|
if args.load_config:
|
||||||
config.load()
|
config.load()
|
||||||
if args.cfgfiles:
|
if args.cfgfiles:
|
||||||
config.load(*args.cfgfiles, strict=True)
|
config.load(args.cfgfiles, strict=True)
|
||||||
if args.yamlfiles:
|
if args.yamlfiles:
|
||||||
config.load(*args.yamlfiles, format="yaml", strict=True)
|
config.load(args.yamlfiles, strict=True, fmt="yaml")
|
||||||
for key, value in args.options:
|
for key, value in args.options:
|
||||||
config.set(key, value)
|
config.set(key, value)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Copyright 2015-2018 Mike Fährmann
|
# Copyright 2015-2019 Mike Fährmann
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License version 2 as
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
@@ -39,11 +39,9 @@ else:
|
|||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# public interface
|
# public interface
|
||||||
|
|
||||||
def load(*files, format="json", strict=False):
|
def load(files=None, strict=False, fmt="json"):
|
||||||
"""Load JSON configuration files"""
|
"""Load JSON configuration files"""
|
||||||
configfiles = files or _default_configs
|
if fmt == "yaml":
|
||||||
|
|
||||||
if format == "yaml":
|
|
||||||
try:
|
try:
|
||||||
import yaml
|
import yaml
|
||||||
parsefunc = yaml.safe_load
|
parsefunc = yaml.safe_load
|
||||||
@@ -53,23 +51,24 @@ def load(*files, format="json", strict=False):
|
|||||||
else:
|
else:
|
||||||
parsefunc = json.load
|
parsefunc = json.load
|
||||||
|
|
||||||
for conf in configfiles:
|
for path in files or _default_configs:
|
||||||
|
path = util.expand_path(path)
|
||||||
try:
|
try:
|
||||||
path = util.expand_path(conf)
|
|
||||||
with open(path, encoding="utf-8") as file:
|
with open(path, encoding="utf-8") as file:
|
||||||
confdict = parsefunc(file)
|
confdict = parsefunc(file)
|
||||||
|
except OSError as exc:
|
||||||
|
if strict:
|
||||||
|
log.error("%s", exc)
|
||||||
|
sys.exit(1)
|
||||||
|
except Exception as exc:
|
||||||
|
log.warning("Could not parse '%s': %s", path, exc)
|
||||||
|
if strict:
|
||||||
|
sys.exit(2)
|
||||||
|
else:
|
||||||
if not _config:
|
if not _config:
|
||||||
_config.update(confdict)
|
_config.update(confdict)
|
||||||
else:
|
else:
|
||||||
util.combine_dict(_config, confdict)
|
util.combine_dict(_config, confdict)
|
||||||
except FileNotFoundError:
|
|
||||||
if strict:
|
|
||||||
log.error("Configuration file '%s' not found", path)
|
|
||||||
sys.exit(1)
|
|
||||||
except Exception as exc:
|
|
||||||
log.warning("Could not parse '%s': %s", path, exc)
|
|
||||||
if strict:
|
|
||||||
sys.exit(2)
|
|
||||||
|
|
||||||
|
|
||||||
def clear():
|
def clear():
|
||||||
@@ -137,7 +136,7 @@ def unset(keys, conf=_config):
|
|||||||
|
|
||||||
|
|
||||||
class apply():
|
class apply():
|
||||||
"""Context Manager to temporarily apply a collection of key-value pairs"""
|
"""Context Manager: apply a collection of key-value pairs"""
|
||||||
_sentinel = object()
|
_sentinel = object()
|
||||||
|
|
||||||
def __init__(self, kvlist):
|
def __init__(self, kvlist):
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class TestConfig(unittest.TestCase):
|
|||||||
fd, self._configfile = tempfile.mkstemp()
|
fd, self._configfile = tempfile.mkstemp()
|
||||||
with os.fdopen(fd, "w") as file:
|
with os.fdopen(fd, "w") as file:
|
||||||
file.write('{"a": "1", "b": {"a": 2, "c": "text"}}')
|
file.write('{"a": "1", "b": {"a": 2, "c": "text"}}')
|
||||||
config.load(self._configfile)
|
config.load((self._configfile,))
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
config.clear()
|
config.clear()
|
||||||
|
|||||||
Reference in New Issue
Block a user