Mercurial > hgrepos > Python > libs > ConfigMix
diff configmix/__init__.py @ 172:8138d56d7cd3
".load" and ".safe_load" get a keyword parameter "defaults" that allows the provision of a configuration dictionary with default settings
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Fri, 26 Apr 2019 14:30:52 +0200 |
| parents | 1ff11462a5c1 |
| children | b3ba2b0265b5 |
line wrap: on
line diff
--- a/configmix/__init__.py Thu Apr 25 17:00:09 2019 +0200 +++ b/configmix/__init__.py Fri Apr 26 14:30:52 2019 +0200 @@ -41,34 +41,57 @@ """ -def load(*files): +def load(*files, **kwargs): """Load the given configuration files, merge them in the given order and return the resulting configuration dictionary. :param files: the filenames of the configuration files to read and merge + :param defaults: optional configuration dictionary with some default + settings where the settings from `files` are merged + into + :type defaults: a configuration dictionary or `None` :returns: the configuration :rtype: ~configmix.config.Configuration """ + defaults = kwargs.get("defaults") if not files: - return Configuration() + if defaults is None: + return Configuration() + else: + return Configuration(defaults) else: - ex = merge(None, _load_cfg_from_file(files[0])) - for f in files[1:]: + if defaults is None: + start = 1 + ex = merge(None, _load_cfg_from_file(files[0])) + else: + start = 0 + ex = merge(None, defaults) + for f in files[start:]: ex = merge(_load_cfg_from_file(f), ex) return Configuration(ex) -def safe_load(*files): +def safe_load(*files, **kwargs): """Analogous to :func:`load` but do merging with :func:`safe_merge` instead of :func:`merge` """ + defaults = kwargs.get("defaults") if not files: - return Configuration() + if defaults is None: + return Configuration() + else: + return Configuration(defaults) else: + if defaults is None: + start = 1 + ex = merge(None, _load_cfg_from_file(files[0])) + else: + start = 0 + ex = merge(None, defaults) ex = safe_merge(None, _load_cfg_from_file(files[0])) - for f in files[1:]: + for f in files[start:]: ex = safe_merge(_load_cfg_from_file(f), ex) return Configuration(ex)
