diff configmix/__init__.py @ 112:c50ad93eb5dc

Implemented a "safe_load()" to load with safe merging
author Franz Glasner <hg@dom66.de>
date Sat, 24 Mar 2018 20:57:42 +0100
parents d51a18e5b0e3
children 21d92ff8cf31
line wrap: on
line diff
--- a/configmix/__init__.py	Sat Mar 24 18:47:54 2018 +0100
+++ b/configmix/__init__.py	Sat Mar 24 20:57:42 2018 +0100
@@ -23,7 +23,7 @@
 from .config import Configuration
 
 
-__all__ = ["load", "Configuration"]
+__all__ = ["load", "safe_load", "Configuration"]
 
 
 def load(*files):
@@ -44,6 +44,20 @@
         return Configuration(ex)
 
 
+def safe_load(*files):
+    """Analogous to :func:`load` but do merging with :func:`safe_merge`
+    instead of :func:`merge`
+
+    """
+    if not files:
+        return Configuration()
+    else:
+        ex = safe_merge(None, _load_cfg_from_file(files[0]))
+        for f in files[1:]:
+            ex = safe_merge(_load_cfg_from_file(f), ex)
+        return Configuration(ex)
+
+
 def _load_cfg_from_file(filename):
     fnl = filename.lower()
     if fnl.endswith(".yml") or fnl.endswith("yaml"):