Add in the capability to merge two config dicts (so that manually set settings aren't overwritten).

This commit is contained in:
Mike Auty
2017-07-20 00:49:36 +01:00
parent fe66301cf3
commit 03cdd91fbf
3 changed files with 33 additions and 1 deletions
@@ -186,6 +186,25 @@ class HierarchicalDict(collections.abc.Mapping):
raise TypeError("Splice requires a string key and HierarchicalDict value")
self._setitem(key, value, False)
def merge(self, key, value, overwrite = False):
"""Acts similarly to splice, but maintains previous values
If overwrite is true, then entries in the new value are used over those that exist within key already
@param key: The location within the hierarchy at which to merge the `value`
@type key: str
@param value: HierarchicalDict to be merged under the key node
@type value: HierarchicalDict
"""
if not isinstance(key, str) or not isinstance(value, HierarchicalDict):
raise TypeError("Splice requires a string key and HierarchicalDict value")
for item in dict(value):
if self.get(key + self._separator + item, None):
if overwrite:
self[key + self._separator + item] = value[item]
else:
self[key + self._separator + item] = value[item]
def clone(self):
"""Duplicates the configuration, allowing changes without affecting the original"""
return copy.deepcopy(self)