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

Commit cdae29b

Browse files
committed
Add merge_into
1 parent 15d04fa commit cdae29b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docker/configure_workers_and_start.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,31 @@ 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+
elif dest != new:
308+
raise ValueError(f"Cannot merge primitive values: {dest!r} != {new!r}")
309+
310+
286311
def convert(src: str, dst: str, **template_vars: object) -> None:
287312
"""Generate a file from a template
288313

0 commit comments

Comments
 (0)