load_yaml: Improve module and function docs
The module has no docstring, so the include/link/inplace-link extensions and their path rules are only discoverable by reading the code. _parse_link() claims a named link 'uses a single pipe' even though pipes may appear in the target path after the first one, and _expand_links() does not say that a jw.link node with sibling keys is not a link at all (the key is kept as-is), which is the behavior a typo in a link node silently produces. Add a module docstring describing the three extensions, the relative path rules, and the known limitations (dotted keys cannot be addressed, path navigation cannot traverse link nodes). Correct the _parse_link() pipe rule, note the sibling-key behavior and the error types in _expand_links(), and fix the 'stays at the current level' wording, since a single dot resolves within the dict that contains the link. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
092af336e3
commit
20c08f0957
1 changed files with 41 additions and 9 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue