diff configmix/__init__.py @ 227:f5011eec3b6e

Added a loader with key "ignore" that ignores the given configuration file
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 12 May 2019 18:06:37 +0200
parents 8b1c52d5e7d5
children b2c75efad9e4
line wrap: on
line diff
--- a/configmix/__init__.py	Sun May 12 17:27:12 2019 +0200
+++ b/configmix/__init__.py	Sun May 12 18:06:37 2019 +0200
@@ -70,7 +70,9 @@
     else:
         ex = merge(None, Configuration(defaults))
     for f in files:
-        ex = merge(_load_cfg_from_file(f), ex)
+        nx = _load_cfg_from_file(f)
+        if nx is not None:
+            ex = merge(nx, ex)
     if extras:
         ex = merge(Configuration(extras), ex)
     return Configuration(ex)
@@ -88,7 +90,9 @@
     else:
         ex = safe_merge(None, Configuration(defaults))
     for f in files:
-        ex = safe_merge(_load_cfg_from_file(f), ex)
+        nx = _load_cfg_from_file(f)
+        if nx is not None:
+            ex = safe_merge(nx, ex)
     if extras:
         ex = safe_merge(Configuration(extras), ex)
     return Configuration(ex)
@@ -120,6 +124,11 @@
     return toml.load(filename)
 
 
+def _load_ignore(filename):
+    """A loader that returns `None` just to ignore `filename`"""
+    return None
+
+
 EMACS_MODELINE = re.compile(r"-\*-(.*?)-\*-")
 EMACS_MODE = re.compile(r"(?:\A\s*|;\s*)mode[:=]\s*([-_.a-zA-Z0-9]+)")
 
@@ -162,6 +171,7 @@
     "conf-toml": _load_toml,
     "javascript": _load_json,
     "json": _load_json,
+    "ignore": _load_ignore,
 }
 """Default associations between file modes and loader functions"""
 
@@ -284,6 +294,12 @@
 
 
 def _load_cfg_from_file(filename):
+    """Determine the loader for file `filename` and return the loaded
+    configuration dict.
+
+    Can return `None` is the file should be ignored by the caller.
+
+    """
     for p, m in _extensions:
         if fnmatch.fnmatch(filename, p):
             if callable(m):