changeset 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
files CHANGES.txt configmix/__init__.py tests/test.py
diffstat 3 files changed, 51 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES.txt	Sun May 12 17:27:12 2019 +0200
+++ b/CHANGES.txt	Sun May 12 18:06:37 2019 +0200
@@ -14,7 +14,15 @@
 
 .. changelog::
    :version: 0.7.2.dev1
-   :released: unreleased             
+   :released: unreleased
+
+   .. change::
+      :tags: feature
+
+      Implemented a loader with key ``ignore`` effectively ignores the
+      contents of given file. No extensions is by default associated with
+      this loader.
+
 
 .. changelog::
    :version: 0.7.1
@@ -36,6 +44,7 @@
       :py:func:`configmix.safe_load` did some preliminary unsafe merges
       from `defaults` and an extra additional unneeded merge.
 
+
 .. changelog::
    :version: 0.7
    :released: 2019-05-06
--- 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):
--- a/tests/test.py	Sun May 12 17:27:12 2019 +0200
+++ b/tests/test.py	Sun May 12 18:06:37 2019 +0200
@@ -79,7 +79,7 @@
     def test01_toml_types(self):
         cfg = configmix.toml.load(os.path.join(TESTDATADIR, "conf1.toml"))
         self.__check_types(cfg)
-        self.__check_comment(cfg)        
+        self.__check_comment(cfg)
 
     def test02_py_types(self):
         cfg = configmix.py.load(os.path.join(TESTDATADIR, "conf1.py"))
@@ -253,7 +253,7 @@
         url = cfg.getvar_s("db.engines.ro.url")
         self.assertEqual(
             u("postgresql+psycopg2://the_database_user_2:the-database-password-2@3rd-host:5432/my_database_catalog"),
-            url)        
+            url)
 
     def test03_namespace(self):
         cfg = self._load(
@@ -372,6 +372,27 @@
 
         self.assertRaises(ValueError, _ld)
 
+    def test02_ignore_one_style(self):
+        configmix.clear_assoc()
+        configmix.set_assoc("*.conf", configmix.get_default_assoc("*.yml"))
+        configmix.set_assoc("*", "ignore", append=True)
+
+        cfg = configmix.load(os.path.join(TESTDATADIR, "conf1.ini"),
+                             os.path.join(TESTDATADIR, "conf30.conf"))
+        self.assertEqual(u("new value"), cfg.getvar_s("key-new"))
+        self.assertRaises(KeyError, cfg.getvar_s, "key1")
+
+    def test02_ignore_all(self):
+        configmix.clear_assoc()
+        configmix.set_assoc("*.conf", configmix.get_default_assoc("*.yml"))
+        # This inserts at index 0 effectively ignoring all configuration files!
+        configmix.set_assoc("*", "ignore")
+
+        cfg = configmix.load(os.path.join(TESTDATADIR, "conf1.ini"),
+                             os.path.join(TESTDATADIR, "conf30.conf"))
+        self.assertRaises(KeyError, cfg.getvar_s, "key-new")
+        self.assertRaises(KeyError, cfg.getvar_s, "key1")        
+
     def test03_only_style_corrrect_style(self):
         configmix.clear_assoc()
         configmix.set_assoc("*.conf", configmix.get_default_assoc("*.yml"))