changeset 293:6f0bf673d4ff

Provide an optional "strict" flag to the top-level loader to pass it to low-level loaders that understand it. Currently only the YAML loader understands this flag.
author Franz Glasner <f.glasner@feldmann-mg.com>
date Wed, 10 Feb 2021 15:25:55 +0100
parents 6a044778371a
children 15f8401c4663
files configmix/__init__.py
diffstat 1 files changed, 18 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/__init__.py	Wed Feb 10 15:04:27 2021 +0100
+++ b/configmix/__init__.py	Wed Feb 10 15:25:55 2021 +0100
@@ -75,12 +75,16 @@
                      Use this for example to overwrite configuration file
                      settings from commandline arguments.
     :type extras: dict-alike or None
+    :keyword strict: enable strict parsing mode for parsers that support it
+                     (e.g. to prevent duplicate keys)
+    :type strict: bool
     :returns: the configuration
     :rtype: ~configmix.config.Configuration
 
     """
     defaults = kwargs.get("defaults")
     extras = kwargs.get("extras")
+    strict = kwargs.get("strict", False)
     if defaults is None:
         ex = Configuration()
     else:
@@ -88,11 +92,11 @@
     for f in files:
         if f.startswith(DIR_PREFIX):
             for f2 in _get_configuration_files_from_dir(f[5:]):
-                nx = _load_cfg_from_file(f2, ignore_unknown=True)
+                nx = _load_cfg_from_file(f2, ignore_unknown=True, strict=strict)
                 if nx is not None:
                     ex = merge(nx, ex)
         else:
-            nx = _load_cfg_from_file(f)
+            nx = _load_cfg_from_file(f, strict=strict)
             if nx is not None:
                 ex = merge(nx, ex)
     if extras:
@@ -107,6 +111,7 @@
     """
     defaults = kwargs.get("defaults")
     extras = kwargs.get("extras")
+    strict = kwargs.get("strict", False)
     if defaults is None:
         ex = Configuration()
     else:
@@ -114,11 +119,11 @@
     for f in files:
         if f.startswith(DIR_PREFIX):
             for f2 in _get_configuration_files_from_dir(f[5:]):
-                nx = _load_cfg_from_file(f2, ignore_unknown=True)
+                nx = _load_cfg_from_file(f2, ignore_unknown=True, strict=strict)
                 if nx is not None:
                     ex = safe_merge(nx, ex)
         else:
-            nx = _load_cfg_from_file(f)
+            nx = _load_cfg_from_file(f, strict=strict)
             if nx is not None:
                 ex = safe_merge(nx, ex)
     if extras:
@@ -146,33 +151,33 @@
     return files
 
 
-def _load_yaml(filename):
+def _load_yaml(filename, strict=False):
     from . import yaml
     with open(u2fs(filename), "rb") as yf:
-        return yaml.safe_load(yf)
+        return yaml.safe_load(yf, strict=strict)
 
 
-def _load_json(filename):
+def _load_json(filename, strict=False):
     from . import json
     return json.load(filename)
 
 
-def _load_py(filename):
+def _load_py(filename, strict=False):
     from . import py
     return py.load(filename)
 
 
-def _load_ini(filename):
+def _load_ini(filename, strict=False):
     from . import ini
     return ini.load(filename)
 
 
-def _load_toml(filename):
+def _load_toml(filename, strict=False):
     from . import toml
     return toml.load(filename)
 
 
-def _load_ignore(filename):
+def _load_ignore(filename, strict=False):
     """A loader that returns `None` just to ignore `filename`"""
     return None
 
@@ -359,7 +364,7 @@
             return      # nothing deleted -> done
 
 
-def _load_cfg_from_file(filename, ignore_unknown=False):
+def _load_cfg_from_file(filename, ignore_unknown=False, strict=False):
     """Determine the loader for file `filename` and return the loaded
     configuration dict.
 
@@ -375,7 +380,7 @@
                 m = m(filename)
                 if m is None:
                     continue
-            return mode_loaders[m](filename)
+            return mode_loaders[m](filename, strict=strict)
     else:
         if ignore_unknown:
             return None