This repository was archived by the owner on Apr 26, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff 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+
286311def convert (src : str , dst : str , ** template_vars : object ) -> None :
287312 """Generate a file from a template
288313
You can’t perform that action at this time.
0 commit comments