# HG changeset patch # User Franz Glasner # Date 1557677197 -7200 # Node ID f5011eec3b6ed894b665a59ed454a9b156a83d99 # Parent 8b1c52d5e7d5b0e9b0d2c1d8affff75a3a09c7c5 Added a loader with key "ignore" that ignores the given configuration file diff -r 8b1c52d5e7d5 -r f5011eec3b6e CHANGES.txt --- 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 diff -r 8b1c52d5e7d5 -r f5011eec3b6e configmix/__init__.py --- 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): diff -r 8b1c52d5e7d5 -r f5011eec3b6e tests/test.py --- 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"))