Skip to content

Config

High-level wrapper exposing query access over a parsed configuration.

Config

Config(nodes: list[ConfigNode])

Bases: _Queryable

A parsed configuration: its ordered top-level lines and their subtrees.

Returned by parse. A configuration owns the top-level lines (those at column 0). find searches the whole tree, while find_child and has_child consider only top-level lines.

Create a configuration from its ordered top-level nodes.

Source code in networkconfparse/config.py
def __init__(self, nodes: list[ConfigNode]) -> None:
    """Create a configuration from its ordered top-level nodes."""
    self.nodes = nodes

nodes instance-attribute

nodes = nodes

walk

walk() -> Iterator[ConfigNode]

Yield every descendant node, depth-first (pre-order).

The object itself is not yielded; iteration descends into the immediate child nodes in configuration order.

Source code in networkconfparse/node.py
def walk(self) -> Iterator[ConfigNode]:
    """Yield every descendant node, depth-first (pre-order).

    The object itself is not yielded; iteration descends into the immediate
    child nodes in configuration order.
    """
    for node in self._query_nodes:
        yield node
        yield from node.walk()

find

find(
    pattern: str | Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> list[ConfigNode]

Return all descendant nodes matching pattern and/or where.

Searches the entire subtree, not just immediate children.

Source code in networkconfparse/node.py
def find(
    self,
    pattern: str | re.Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> list[ConfigNode]:
    """Return all descendant nodes matching ``pattern`` and/or ``where``.

    Searches the entire subtree, not just immediate children.
    """
    test = self._matcher(pattern, where)
    return [node for node in self.walk() if test(node)]

find_one

find_one(
    pattern: str | Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> ConfigNode | None

Return the first descendant matching pattern/where, or None.

Searches the whole subtree in pre-order.

Source code in networkconfparse/node.py
def find_one(
    self,
    pattern: str | re.Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> ConfigNode | None:
    """Return the first descendant matching ``pattern``/``where``, or ``None``.

    Searches the whole subtree in pre-order.
    """
    test = self._matcher(pattern, where)
    for node in self.walk():
        if test(node):
            return node
    return None

find_child

find_child(
    pattern: str | Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> ConfigNode | None

Return the first immediate child matching pattern/where.

Only immediate children are considered, in configuration order; returns None if none match.

Source code in networkconfparse/node.py
def find_child(
    self,
    pattern: str | re.Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> ConfigNode | None:
    """Return the first immediate child matching ``pattern``/``where``.

    Only immediate children are considered, in configuration order; returns
    ``None`` if none match.
    """
    test = self._matcher(pattern, where)
    for node in self._query_nodes:
        if test(node):
            return node
    return None

has_child

has_child(
    pattern: str | Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> bool

Return whether any immediate child matches pattern/where.

Source code in networkconfparse/node.py
def has_child(
    self,
    pattern: str | re.Pattern[str] | None = None,
    *,
    where: Predicate | None = None,
) -> bool:
    """Return whether any immediate child matches ``pattern``/``where``."""
    return self.find_child(pattern, where=where) is not None

find_with_child

find_with_child(
    pattern: str | Pattern[str] | None, child: Patterns
) -> list[ConfigNode]

Return nodes matching pattern that have child as direct children.

child is one regex or an iterable of them; every pattern must match some direct child (logical AND). Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_with_child(
    self,
    pattern: str | re.Pattern[str] | None,
    child: Patterns,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` that have ``child`` as direct children.

    ``child`` is one regex or an iterable of them; every pattern must match
    some direct child (logical AND). Implemented with `find` and a
    ``where`` predicate.
    """
    children = _as_list(child)
    return self.find(
        pattern,
        where=lambda node: all(node.has_child(c) for c in children),
    )

find_with_descendant

find_with_descendant(
    pattern: str | Pattern[str] | None, descendant: Patterns
) -> list[ConfigNode]

Return nodes matching pattern with descendant anywhere below.

descendant is one regex or an iterable of them; every pattern must match some descendant at any depth (logical AND). Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_with_descendant(
    self,
    pattern: str | re.Pattern[str] | None,
    descendant: Patterns,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` with ``descendant`` anywhere below.

    ``descendant`` is one regex or an iterable of them; every pattern must
    match some descendant at any depth (logical AND). Implemented with
    `find` and a ``where`` predicate.
    """
    descendants = _as_list(descendant)
    return self.find(
        pattern,
        where=lambda node: all(node.find_one(d) is not None for d in descendants),
    )

find_with_parent

find_with_parent(
    pattern: str | Pattern[str] | None, parent: Patterns
) -> list[ConfigNode]

Return nodes matching pattern whose direct parent matches parent.

parent is one regex or an iterable of them; every pattern must match the single parent line (logical AND). Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_with_parent(
    self,
    pattern: str | re.Pattern[str] | None,
    parent: Patterns,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` whose direct parent matches ``parent``.

    ``parent`` is one regex or an iterable of them; every pattern must match
    the single parent line (logical AND). Implemented with `find` and
    a ``where`` predicate.
    """
    parents = _as_list(parent)
    return self.find(
        pattern,
        where=lambda node: _parent_matches_all(node, parents),
    )

find_with_ancestor

find_with_ancestor(
    pattern: str | Pattern[str] | None,
    ancestor: Patterns,
    *,
    adjacent: bool = False,
) -> list[ConfigNode]

Return nodes matching pattern with ancestor above them.

ancestor is one regex or an iterable of them, all required (AND). With adjacent=False (the default) each pattern must match some ancestor, in any order. With adjacent=True the patterns must form a consecutive chain matched nearest-first: the first against the direct parent, the next against its parent, and so on. Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_with_ancestor(
    self,
    pattern: str | re.Pattern[str] | None,
    ancestor: Patterns,
    *,
    adjacent: bool = False,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` with ``ancestor`` above them.

    ``ancestor`` is one regex or an iterable of them, all required (AND).
    With ``adjacent=False`` (the default) each pattern must match some
    ancestor, in any order. With ``adjacent=True`` the patterns must form a
    consecutive chain matched nearest-first: the first against the direct
    parent, the next against its parent, and so on. Implemented with
    `find` and a ``where`` predicate.
    """
    ancestors = _as_list(ancestor)
    if adjacent:
        return self.find(
            pattern,
            where=lambda node: _adjacent_ancestors(node, ancestors),
        )
    return self.find(
        pattern,
        where=lambda node: _any_ancestor_each(node, ancestors),
    )

find_without_child

find_without_child(
    pattern: str | Pattern[str] | None, child: Patterns
) -> list[ConfigNode]

Return nodes matching pattern that lack every child pattern.

The negative counterpart of find_with_child. child is one regex or an iterable of them; a node matches when none of them match any direct child (the "none present" / NOR rule). For [a, b] a node qualifies only if it has no direct child matching a and none matching b - so a node carrying just one of them is excluded. Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_without_child(
    self,
    pattern: str | re.Pattern[str] | None,
    child: Patterns,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` that lack every ``child`` pattern.

    The negative counterpart of `find_with_child`. ``child`` is one regex
    or an iterable of them; a node matches when **none** of them match any
    direct child (the "none present" / NOR rule). For ``[a, b]`` a node
    qualifies only if it has no direct child matching ``a`` and none
    matching ``b`` - so a node carrying just one of them is excluded.
    Implemented with `find` and a ``where`` predicate.
    """
    children = _as_list(child)
    return self.find(
        pattern,
        where=lambda node: not any(node.has_child(c) for c in children),
    )

find_without_descendant

find_without_descendant(
    pattern: str | Pattern[str] | None, descendant: Patterns
) -> list[ConfigNode]

Return nodes matching pattern with none of descendant below.

The negative counterpart of find_with_descendant. descendant is one regex or an iterable of them; a node matches when none of them match any descendant at any depth (the "none present" / NOR rule). For [a, b] a node carrying just one of the two below it is excluded. Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_without_descendant(
    self,
    pattern: str | re.Pattern[str] | None,
    descendant: Patterns,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` with none of ``descendant`` below.

    The negative counterpart of `find_with_descendant`. ``descendant`` is
    one regex or an iterable of them; a node matches when **none** of them
    match any descendant at any depth (the "none present" / NOR rule). For
    ``[a, b]`` a node carrying just one of the two below it is excluded.
    Implemented with `find` and a ``where`` predicate.
    """
    descendants = _as_list(descendant)
    return self.find(
        pattern,
        where=lambda node: (
            not any(node.find_one(d) is not None for d in descendants)
        ),
    )

find_without_parent

find_without_parent(
    pattern: str | Pattern[str] | None, parent: Patterns
) -> list[ConfigNode]

Return nodes matching pattern whose parent matches no parent.

The negative counterpart of find_with_parent. parent is one regex or an iterable of them; a node matches when its direct parent matches none of them (the "none present" / NOR rule). A top-level node has no parent, so it can never sit beneath a matching one and always qualifies. For [a, b] a node whose parent matches just one of the two is excluded. Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_without_parent(
    self,
    pattern: str | re.Pattern[str] | None,
    parent: Patterns,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` whose parent matches no ``parent``.

    The negative counterpart of `find_with_parent`. ``parent`` is one regex
    or an iterable of them; a node matches when its direct parent matches
    **none** of them (the "none present" / NOR rule). A top-level node has
    no parent, so it can never sit beneath a matching one and always
    qualifies. For ``[a, b]`` a node whose parent matches just one of the
    two is excluded. Implemented with `find` and a ``where`` predicate.
    """
    parents = _as_list(parent)
    return self.find(
        pattern,
        where=lambda node: _parent_matches_none(node, parents),
    )

find_without_ancestor

find_without_ancestor(
    pattern: str | Pattern[str] | None, ancestor: Patterns
) -> list[ConfigNode]

Return nodes matching pattern with none of ancestor above.

The negative counterpart of find_with_ancestor. ancestor is one regex or an iterable of them; a node matches when none of them match any ancestor at any depth (the "none present" / NOR rule). A top-level node has no ancestors and always qualifies. For [a, b] a node with just one of the two anywhere above it is excluded.

Unlike find_with_ancestor, this takes no adjacent flag: negating an adjacent-chain match is rarely meaningful, so the only negative offered is "none of these appear anywhere in the ancestry." Implemented with find and a where predicate.

Source code in networkconfparse/node.py
def find_without_ancestor(
    self,
    pattern: str | re.Pattern[str] | None,
    ancestor: Patterns,
) -> list[ConfigNode]:
    """Return nodes matching ``pattern`` with none of ``ancestor`` above.

    The negative counterpart of `find_with_ancestor`. ``ancestor`` is one
    regex or an iterable of them; a node matches when **none** of them
    match any ancestor at any depth (the "none present" / NOR rule). A
    top-level node has no ancestors and always qualifies. For ``[a, b]`` a
    node with just one of the two anywhere above it is excluded.

    Unlike `find_with_ancestor`, this takes no ``adjacent`` flag: negating
    an adjacent-chain match is rarely meaningful, so the only negative
    offered is "none of these appear anywhere in the ancestry." Implemented
    with `find` and a ``where`` predicate.
    """
    ancestors = _as_list(ancestor)
    return self.find(
        pattern,
        where=lambda node: _no_ancestor_matches(node, ancestors),
    )