| 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/. |
| 4 |
|
| 5 |
from typing import Any, Dict, List |
| 6 |
|
| 7 |
from taskgraph.util.copy import deepcopy |
| 8 |
|
| 9 |
|
| 10 |
def merge_to(source, dest): |
| 11 |
""" |
| 12 |
Merge dict and arrays (override scalar values) |
| 13 |
|
| 14 |
Keys from source override keys from dest, and elements from lists in source |
| 15 |
are appended to lists in dest. |
| 16 |
|
| 17 |
:param dict source: to copy from |
| 18 |
:param dict dest: to copy to (modified in place) |
| 19 |
""" |
| 20 |
|
| 21 |
for key, value in source.items(): |
| 22 |
if ( |
| 23 |
isinstance(value, dict) |
| 24 |
and len(value) == 1 |
| 25 |
and list(value)[0].startswith("by-") |
| 26 |
): |
| 27 |
# Do not merge by-* values as it will almost certainly not do what |
| 28 |
# the user expects. |
| 29 |
dest[key] = value |
| 30 |
continue |
| 31 |
|
| 32 |
# Override mismatching or empty types |
| 33 |
if type(value) != type(dest.get(key)): # noqa |
| 34 |
dest[key] = value |
| 35 |
continue |
| 36 |
|
| 37 |
# Merge dict |
| 38 |
if isinstance(value, dict): |
| 39 |
merge_to(value, dest[key]) |
| 40 |
continue |
| 41 |
|
| 42 |
if isinstance(value, list): |
| 43 |
dest[key] = dest[key] + value |
| 44 |
continue |
| 45 |
|
| 46 |
dest[key] = value |
| 47 |
|
| 48 |
return dest |
| 49 |
|
| 50 |
|
| 51 |
def merge(*objects): |
| 52 |
""" |
| 53 |
Merge the given objects, using the semantics described for merge_to, with |
| 54 |
objects later in the list taking precedence. From an inheritance |
| 55 |
perspective, "parents" should be listed before "children". |
| 56 |
|
| 57 |
Returns the result without modifying any arguments. |
| 58 |
""" |
| 59 |
if len(objects) == 1: |
| 60 |
return deepcopy(objects[0]) |
| 61 |
return merge_to(objects[-1], merge(*objects[:-1])) |
| 62 |
|
| 63 |
|
| 64 |
def deep_get(dict_, field): |
| 65 |
container, subfield = dict_, field |
| 66 |
while "." in subfield: |
| 67 |
f, subfield = subfield.split(".", 1) |
| 68 |
if f not in container: |
| 69 |
return None |
| 70 |
|
| 71 |
container = container[f] |
| 72 |
|
| 73 |
return container.get(subfield) |
| 74 |
|
| 75 |
|
| 76 |
def substitute(item: Any, **subs: Dict[str, Any]) -> Any: |
| 77 |
if isinstance(item, list): |
| 78 |
for i in range(len(item)): |
| 79 |
item[i] = substitute(item[i], **subs) |
| 80 |
elif isinstance(item, dict): |
| 81 |
new_dict = {} |
| 82 |
for k, v in item.items(): |
| 83 |
k = k.format(**subs) |
| 84 |
new_dict[k] = substitute(v, **subs) |
| 85 |
item = new_dict |
| 86 |
elif isinstance(item, str): |
| 87 |
item = item.format(**subs) |
| 88 |
else: |
| 89 |
item = item |
| 90 |
|
| 91 |
return item |
| 92 |
|
| 93 |
|
| 94 |
def substitute_task_fields( |
| 95 |
task: Dict[str, Any], fields: List[str], **subs: Any |
| 96 |
) -> None: |
| 97 |
for field in fields: |
| 98 |
container, subfield = task, field |
| 99 |
while "." in subfield: |
| 100 |
f, subfield = subfield.split(".", 1) |
| 101 |
container = container[f] |
| 102 |
|
| 103 |
container[subfield] = substitute(container[subfield], **subs) |