changeset 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
files CHANGES.txt configmix/__init__.py
diffstat 2 files changed, 39 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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)