Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / tools / tryselect / util / dicttools.py
blob465e4a43de8572559d1267756cd1c68757bc8e5b
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/.
6 import copy
9 def merge_to(source, dest):
10 """
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)
18 """
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]
24 continue
26 # Merge dict
27 if isinstance(value, dict):
28 merge_to(value, dest[key])
29 continue
31 if isinstance(value, list):
32 dest[key] = dest[key] + source[key]
33 continue
35 dest[key] = source[key]
37 return dest
40 def merge(*objects):
41 """
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.
47 """
48 if len(objects) == 1:
49 return copy.deepcopy(objects[0])
50 return merge_to(objects[-1], merge(*objects[:-1]))