Skip to content

rawnodes

Module implementing convenience methods for dealing with ruamel.yaml Nodes

get_entries(node)

Return all entries (key, value) in this map node

Source code in gamma/config/rawnodes.py
@dispatch
def get_entries(node: MappingNode) -> Iterable[Entry]:
    """Return all entries (key, value) in this `map` node"""
    for item_key, item_value in node.value:
        yield item_key, item_value

get_entry(node, key, *, default=Ellipsis)

Get a (key, value) entry from a map node.

Parameters:

Name Type Description Default
default

return this value instead of KeyError if not found.

Ellipsis
Raise

KeyError if key not found and default not provided

Source code in gamma/config/rawnodes.py
@dispatch
def get_entry(node: MappingNode, key, *, default=...) -> Entry:
    """Get a (key, value) entry from a `map` node.

    Args:
        default: return this value instead of `KeyError` if not found.

    Raise:
        `KeyError` if key not found and `default` not provided
    """

    merge_tag = tags.Merge().name
    map_tag = tags.Map().name

    # collect possible values, handling anchors
    values = []
    for item_key, item_value in node.value:
        # handle anchor merge
        if item_key.tag == merge_tag and item_value.tag == map_tag:
            for sub_key, sub_value in item_value.value:
                values.append((sub_key, sub_value))
        else:
            values.append((item_key, item_value))

    # iterate on items in *reverse* order due to potential duplicates from anchor
    # overrides
    for item_key, item_value in values[::-1]:
        if is_equal(key, item_key):
            return item_key, item_value

    if default is not Ellipsis:
        return as_node(key), default

    raise KeyError(key)

get_id(a)

Return an object that allows to ScalarNodes to be hashed and compared

Source code in gamma/config/rawnodes.py
@dispatch
def get_id(a: ScalarNode) -> Hashable:
    """Return an object that allows to ScalarNodes to be hashed and compared"""
    return (a.tag, a.value)

get_item(node, key, *, default=Ellipsis)

Get a single child node item from map node

Source code in gamma/config/rawnodes.py
@dispatch
def get_item(node: MappingNode, key, *, default=...) -> Optional[Node]:
    """Get a single child node item from `map` node"""
    for item_key, item_value in node.value:
        if is_equal(key, item_key):
            return item_value

    if default is not Ellipsis:
        return default

    raise KeyError(key)  # pragma: no cover

get_keys(node)

Get all keys on a map node

Source code in gamma/config/rawnodes.py
@dispatch
def get_keys(node: MappingNode) -> Iterable[Node]:
    """Get all keys on a `map` node"""
    for item_key, _ in node.value:
        yield item_key

get_values(node)

Return all values in this map node

Source code in gamma/config/rawnodes.py
@dispatch
def get_values(node: MappingNode) -> Iterable[Node]:
    """Return all values in this `map` node"""
    for _, item_value in node.value:
        yield item_value

is_in(node, container)

Return true if node is in container

Source code in gamma/config/rawnodes.py
@dispatch
def is_in(node: Node, container: Iterable[Any]) -> bool:
    """Return true if `node` is in `container`"""
    for item in container:
        if is_equal(item, node):
            return True
    return False

union_nodes(first, second)

Union two sets of nodes.

By default we keep the ones in first if equals.

Source code in gamma/config/rawnodes.py
@dispatch
def union_nodes(first: Iterable, second: Iterable) -> Iterable:
    """Union two sets of nodes.

    By default we keep the ones in `first` if equals."""

    out = dict()

    for a in first:
        out[get_id(a)] = a

    for b in second:
        if b not in out:
            out[get_id(b)] = b

    return list(out.values())