comparison configmix/__init__.py @ 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 21d92ff8cf31
comparison
equal deleted inserted replaced
111:d51a18e5b0e3 112:c50ad93eb5dc
21 import copy 21 import copy
22 22
23 from .config import Configuration 23 from .config import Configuration
24 24
25 25
26 __all__ = ["load", "Configuration"] 26 __all__ = ["load", "safe_load", "Configuration"]
27 27
28 28
29 def load(*files): 29 def load(*files):
30 """Load the given configuration files, merge them in the given order 30 """Load the given configuration files, merge them in the given order
31 and return the resulting configuration dictionary. 31 and return the resulting configuration dictionary.
39 return Configuration() 39 return Configuration()
40 else: 40 else:
41 ex = merge(None, _load_cfg_from_file(files[0])) 41 ex = merge(None, _load_cfg_from_file(files[0]))
42 for f in files[1:]: 42 for f in files[1:]:
43 ex = merge(_load_cfg_from_file(f), ex) 43 ex = merge(_load_cfg_from_file(f), ex)
44 return Configuration(ex)
45
46
47 def safe_load(*files):
48 """Analogous to :func:`load` but do merging with :func:`safe_merge`
49 instead of :func:`merge`
50
51 """
52 if not files:
53 return Configuration()
54 else:
55 ex = safe_merge(None, _load_cfg_from_file(files[0]))
56 for f in files[1:]:
57 ex = safe_merge(_load_cfg_from_file(f), ex)
44 return Configuration(ex) 58 return Configuration(ex)
45 59
46 60
47 def _load_cfg_from_file(filename): 61 def _load_cfg_from_file(filename):
48 fnl = filename.lower() 62 fnl = filename.lower()