# HG changeset patch # User Franz Glasner # Date 1521921462 -3600 # Node ID c50ad93eb5dc12baa67881d8842c95e9557a352d # Parent d51a18e5b0e3bc780a81419af4699dcad8fe5907 Implemented a "safe_load()" to load with safe merging diff -r d51a18e5b0e3 -r c50ad93eb5dc configmix/__init__.py --- 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"): diff -r d51a18e5b0e3 -r c50ad93eb5dc tests/test.py --- 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()