comparison configmix/__init__.py @ 220:2034da70f8fd

Simplify the implementation of configmix.load() and .safe_load(): this also prepare for the coming implementation of a "last" user configuration (analogout to "defaults" as the first configuration).
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 09 May 2019 09:27:23 +0200
parents b869e792310e
children 6f0f39a9a46f
comparison
equal deleted inserted replaced
219:0477186d1cba 220:2034da70f8fd
56 :returns: the configuration 56 :returns: the configuration
57 :rtype: ~configmix.config.Configuration 57 :rtype: ~configmix.config.Configuration
58 58
59 """ 59 """
60 defaults = kwargs.get("defaults") 60 defaults = kwargs.get("defaults")
61 if not files:
62 if defaults is None:
63 return Configuration()
64 else:
65 return Configuration(defaults)
66 if defaults is None: 61 if defaults is None:
67 start = 1 62 ex = Configuration()
68 ex = merge(None, _load_cfg_from_file(files[0])) 63 else:
69 else: 64 ex = merge(None, Configuration(defaults))
70 start = 0 65 for f in files:
71 ex = merge(None, defaults)
72 for f in files[start:]:
73 ex = merge(_load_cfg_from_file(f), ex) 66 ex = merge(_load_cfg_from_file(f), ex)
74 return Configuration(ex) 67 return Configuration(ex)
75 68
76 69
77 def safe_load(*files, **kwargs): 70 def safe_load(*files, **kwargs):
78 """Analogous to :func:`load` but do merging with :func:`safe_merge` 71 """Analogous to :func:`load` but do merging with :func:`safe_merge`
79 instead of :func:`merge` 72 instead of :func:`merge`
80 73
81 """ 74 """
82 defaults = kwargs.get("defaults") 75 defaults = kwargs.get("defaults")
83 if not files:
84 if defaults is None:
85 return Configuration()
86 else:
87 return Configuration(copy.deepcopy(defaults))
88 if defaults is None: 76 if defaults is None:
89 start = 1 77 ex = Configuration()
90 ex = safe_merge(None, _load_cfg_from_file(files[0])) 78 else:
91 else: 79 ex = safe_merge(None, Configuration(defaults))
92 start = 0 80 for f in files:
93 ex = safe_merge(None, defaults)
94 for f in files[start:]:
95 ex = safe_merge(_load_cfg_from_file(f), ex) 81 ex = safe_merge(_load_cfg_from_file(f), ex)
96 return Configuration(ex) 82 return Configuration(ex)
97 83
98 84
99 def _load_yaml(filename): 85 def _load_yaml(filename):