diff --git a/src/python/jw/util/load_yaml.py b/src/python/jw/util/load_yaml.py index d8bdade..dde069c 100644 --- a/src/python/jw/util/load_yaml.py +++ b/src/python/jw/util/load_yaml.py @@ -1,3 +1,27 @@ +"""YAML loading with includes, links, and deep merging. + +load_yaml() loads a YAML file whose root must be a mapping and +supports three extensions: + +- ``jw.include`` — deep-merges other YAML files into the document, + either at the root or at a dot-separated target path. A leading + ``-`` marks the include as optional (a missing file contributes an + empty document). +- ``jw.link`` — replaces a node with the value found at a dot-separated + target path. A prefix before the first ``|`` names the resulting + key instead of replacing the node in place. +- ``jw.inplace-link`` — like ``jw.link``, but sibling keys of the link + node are deep-merged into the resolved target mapping. + +Target paths use dot-separated segments. Relative paths start with +``.`` and work like Python imports: a single dot stays in the dict +that contains the link, each additional dot goes up one level. +Because segments are separated by dots, keys that themselves contain +a dot cannot be addressed. Path navigation operates on the +unexpanded tree, so intermediate segments must not be link nodes +themselves. +""" + from __future__ import annotations import re @@ -30,9 +54,12 @@ def _deep_merge(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any def _parse_link(value: str) -> tuple[bool, str | None, str]: """Parse a link value into (is_named, name_or_None, path_string). - A named link uses a single pipe to separate the link name from the - target path, e.g. ``"name|path|to|target"``. Otherwise the entire - string is treated as an absolute or relative target path. + A named link is recognized by a prefix matching ``[a-zA-Z0-9_-]+`` + followed by a pipe. The first pipe then separates the link name + from the target path, e.g. ``"name|path|to|target"`` yields the + name ``name`` and the target path ``path|to|target``. Otherwise + the entire string is treated as an absolute or relative target + path. """ m = _LINK_NAME_RE.match(value) if m: @@ -327,13 +354,18 @@ def _expand_links(root: dict[str, Any]) -> dict[str, Any]: key disappears and its value is deep-merged into the resolved target dict. Sibling keys (if any) override the target on conflict. - Paths use dot-separated segments. A relative path starts with ``.`` - (Python-import style): ``.`` stays at the current level, ``..`` goes - up one level, ``...`` two, and so on. The first ``|`` in the link - value (only when the prefix matches ``[a-zA-Z0-9_-]+|``) separates - the link name from the target path for named links. + Paths use dot-separated segments. A relative path starts with + ``.`` (Python-import style): a single dot stays in the dict that + contains the link, ``..`` goes up one level, ``...`` two, and so + on. The first ``|`` in the link value (only when the prefix + matches ``[a-zA-Z0-9_-]+|``) separates the link name from the + target path for named links. - Cyclic links raise ``ValueError``; missing targets also raise. + A ``jw.link`` node with additional sibling keys is not a link. The + ``jw.link`` key and its raw value are left as-is in the output. + + Cyclic links and unresolvable targets raise ``ValueError`` naming + the link. Non-string link values raise ``TypeError``. """ resolved: dict[tuple[tuple[str | int, ...], str], Any] = {} resolving: set[tuple[tuple[str | int, ...], str]] = set()