Mercurial > hgrepos > Python > libs > ConfigMix
changeset 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 | 5b667c252f8c |
| files | configmix/__init__.py tests/test.py |
| diffstat | 2 files changed, 30 insertions(+), 4 deletions(-) [+] |
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"):
--- a/tests/test.py Sat Mar 24 18:47:54 2018 +0100 +++ b/tests/test.py Sat Mar 24 20:57:42 2018 +0100 @@ -90,10 +90,10 @@ self.__check_tree(cfg) -class T02LoadAndMerge(unittest.TestCase): +class _T02MixinLoadAndMerge: def test01_load(self): - cfg = configmix.load( + cfg = self._load( os.path.join(TESTDATADIR, "conf20.yml"), os.path.join(TESTDATADIR, "conf21.yml")) @@ -128,7 +128,7 @@ cfg.getvar_s("test.Str")) def test02_load_with_ini(self): - cfg = configmix.load( + cfg = self._load( os.path.join(TESTDATADIR, "conf20.yml"), os.path.join(TESTDATADIR, "conf21.yml"), os.path.join(TESTDATADIR, "conf22.ini")) @@ -154,5 +154,17 @@ url) +class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): + + def setUp(self): + self._load = configmix.load + + +class T03SafeLoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase): + + def setUp(self): + self._load = configmix.safe_load + + if __name__ == "__main__": unittest.main()
