# HG changeset patch # User Franz Glasner # Date 1556281852 -7200 # Node ID 8138d56d7cd3cd52628b65d2864bc855768d9ea0 # Parent 1ff11462a5c19a6c81001f05bbdb0ccdcd00080d ".load" and ".safe_load" get a keyword parameter "defaults" that allows the provision of a configuration dictionary with default settings diff -r 1ff11462a5c1 -r 8138d56d7cd3 CHANGES.txt --- a/CHANGES.txt Thu Apr 25 17:00:09 2019 +0200 +++ b/CHANGES.txt Fri Apr 26 14:30:52 2019 +0200 @@ -20,11 +20,19 @@ :tags: breaking, feature The associations from filename extensions to parsers are - :py:mod:`fnmatch` style patterns now. + :py:mod:`fnmatch` style patterns now. Calling :py:func:`configmix.set_loader` prepends to the currently defined associations and therefore gets the highest priority. + .. change:: + :tags: feature + + :py:func:`configmix.load` and :py:func:`configmix.safe_load` got a + keyword argument `defaults` that allow the provision of an already + existing default configuration wheter all additional configuration + settings are merged into. + .. changelog:: :version: 0.6 diff -r 1ff11462a5c1 -r 8138d56d7cd3 configmix/__init__.py --- 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)