Rewritten dcd.cfg.Config

Config now supports full hierarchical and overlay-capable usage.
A generic tree overlay function is in aux.
This commit is contained in:
Martin Sekera 2019-10-18 01:12:15 +02:00
parent 139381e7fc
commit d72659088f
3 changed files with 54 additions and 31 deletions

View file

@ -77,3 +77,32 @@ def hex_to_raw(text, separator=" "):
return bytearray(output)
# ----------------------------------------------------------------
class DeleteOverlay:
pass
def overlay_tree(base, overlay):
"""
For every key path in `overlay`, sets the leaf value of the same
key path in `base`. Any missing non-leaf keys will be created.
To delete a key, set its overlay value to DeleteOverlay.
"""
stack = [(base, overlay)]
while stack:
base_p, over_p = stack.pop()
for k, v in over_p.items():
if v is DeleteOverlay:
del base_p[k]
else:
if type(v) is dict:
if k not in base_p or type(base_p[k]) is not dict:
base_p[k] = {}
stack.append((base_p[k], over_p[k]))
else:
base_p[k] = v