Skip to content

merge

Implements the node merging functionality

has_replace_hint(node)

Check for existence of config "hint" in the node's comments.

Only @hint: merge_replace is supported.

Source code in gamma/config/merge.py
@dispatch
def has_replace_hint(node: Node) -> bool:
    """Check for existence of config "hint" in the node's comments.

    Only `@hint: merge_replace` is supported.
    """

    node_comments = getattr(node, "comment", [])
    if not node_comments:
        return False

    # flatten comment hierarchy. can't use itertools.chain here :(
    hints = set()
    stack = [node_comments]
    while stack:
        item = stack.pop(0)

        if item and hasattr(item, "value"):
            match = hints_pattern.match(item.value)
            if match:
                hints.add(match.group(1))

        elif isinstance(item, list):
            for sub in item:
                if sub:
                    stack.append(sub)

    return "merge_replace" in hints

merge_nodes(l_key, l_node, r_key, r_node)

Merge sequence nodes recursively.

Right side has precedence. @hint: merge_replace overrides merging, returning right.

Source code in gamma/config/merge.py
@dispatch
def merge_nodes(l_key, l_node: SequenceNode, r_key, r_node: SequenceNode):
    """Merge `sequence` nodes recursively.

    Right side has precedence. `@hint: merge_replace` overrides merging, returning
    right.
    """

    if has_replace_hint(r_node):
        return r_key, r_node

    newvalue = list(get_values(l_node)).copy()
    for r_item in get_values(r_node):
        if not is_in(r_item, newvalue):
            newvalue.append(r_item)

    # by default, we use right side meta data
    newnode = deepcopy(r_node)
    newnode.value = newvalue
    return r_key, newnode