1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 def merge_to(source
, dest
):
11 Merge dict and arrays (override scalar values)
13 Keys from source override keys from dest, and elements from lists in source
14 are appended to lists in dest.
16 :param dict source: to copy from
17 :param dict dest: to copy to (modified in place)
20 for key
, value
in source
.items():
21 # Override mismatching or empty types
22 if type(value
) != type(dest
.get(key
)): # noqa
23 dest
[key
] = source
[key
]
27 if isinstance(value
, dict):
28 merge_to(value
, dest
[key
])
31 if isinstance(value
, list):
32 dest
[key
] = dest
[key
] + source
[key
]
35 dest
[key
] = source
[key
]
42 Merge the given objects, using the semantics described for merge_to, with
43 objects later in the list taking precedence. From an inheritance
44 perspective, "parents" should be listed before "children".
46 Returns the result without modifying any arguments.
49 return copy
.deepcopy(objects
[0])
50 return merge_to(objects
[-1], merge(*objects
[:-1]))