Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 6f7583b

Browse files
committed
Add merge_into
1 parent 23775bf commit 6f7583b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

docker/configure_workers_and_start.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,32 @@ def flush_buffers() -> None:
283283
sys.stderr.flush()
284284

285285

286+
def merge_into(dest: Any, new: Any) -> None:
287+
"""
288+
Merges `new` into `dest` with the following rules:
289+
290+
- dicts: values with the same key will be merged recursively
291+
- lists: `new` will be appended to `dest`
292+
- primitives: they will be checked for equality and inequality will result
293+
in a ValueError
294+
295+
It is an error for `dest` and `new` to be of different types.
296+
"""
297+
if isinstance(dest, dict) and isinstance(new, dict):
298+
for k, v in new.items():
299+
if k in dest:
300+
merge_into(dest[k], v)
301+
else:
302+
dest[k] = v
303+
elif isinstance(dest, list) and isinstance(new, list):
304+
dest.extend(new)
305+
elif type(dest) != type(new):
306+
raise TypeError(f"Cannot merge {type(dest).__name__} and {type(new).__name__}")
307+
308+
if dest != new:
309+
raise ValueError(f"Cannot merge primitive values: {dest!r} != {new!r}")
310+
311+
286312
def convert(src: str, dst: str, **template_vars: object) -> None:
287313
"""Generate a file from a template
288314

0 commit comments

Comments
 (0)