2026-08-16 12:23:59 +02:00
|
|
|
"""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
|
2026-08-25 20:08:23 +02:00
|
|
|
``-`` marks the include as optional (a missing file contributes
|
2026-08-25 20:09:55 +02:00
|
|
|
nothing). In a YAML block list the list dash is not the marker;
|
|
|
|
|
the item itself must carry it, as in ``- -file.yaml`` or
|
|
|
|
|
``- "-file.yaml"``. File names may use glob characters (``*``, ``?``,
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
``[...]``); all matches are included in lexical order, and a
|
|
|
|
|
non-optional glob that matches no files raises ValueError. A spec
|
|
|
|
|
that starts with a glob character such as ``*`` or ``[`` must be
|
2026-11-13 13:01:08 +01:00
|
|
|
quoted in YAML. In the top-level document, the file part of an
|
|
|
|
|
include spec may reference environment variables as ``$(VAR)`` or
|
|
|
|
|
``${VAR}``; an undefined variable raises ValueError. Specs in
|
|
|
|
|
included documents are never expanded.
|
2026-08-16 12:23:59 +02:00
|
|
|
- ``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.
|
|
|
|
|
"""
|
|
|
|
|
|
2026-06-13 20:31:42 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-11-13 13:01:08 +01:00
|
|
|
import os
|
2026-08-16 12:20:12 +02:00
|
|
|
import re
|
2026-08-11 11:54:28 +02:00
|
|
|
|
2026-06-13 20:31:42 +02:00
|
|
|
from copy import deepcopy
|
|
|
|
|
from pathlib import Path
|
2026-08-11 11:54:28 +02:00
|
|
|
from typing import Any, TypeAlias, cast
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
2026-06-13 20:31:42 +02:00
|
|
|
import yaml # type: ignore[import-untyped]
|
|
|
|
|
|
2026-08-16 12:14:03 +02:00
|
|
|
# -- Constants ------------------------------------------------------------------
|
|
|
|
|
|
2026-07-08 15:37:06 +02:00
|
|
|
IncludeSpec: TypeAlias = tuple[bool, Path, tuple[str, ...]]
|
|
|
|
|
Includes: TypeAlias = list[IncludeSpec]
|
2026-06-13 20:31:42 +02:00
|
|
|
|
2026-08-16 12:14:03 +02:00
|
|
|
_LINK_NAME_RE = re.compile(r'^([a-zA-Z0-9_-]+)\|')
|
2026-08-16 12:19:23 +02:00
|
|
|
_INCLUDE_PATH_RE = re.compile(r'^([a-zA-Z0-9_.-]+)\|')
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
_GLOB_CHARS = frozenset('*?[')
|
2026-08-16 12:14:03 +02:00
|
|
|
|
2026-08-13 10:19:48 +02:00
|
|
|
# -- Low-level helpers (call graph leaves) --------------------------------------
|
|
|
|
|
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
def _deep_merge(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]:
|
2026-06-13 20:31:42 +02:00
|
|
|
ret = deepcopy(base)
|
|
|
|
|
for key, val in override.items():
|
|
|
|
|
if (key in ret and isinstance(ret[key], dict) and isinstance(val, dict)):
|
|
|
|
|
ret[key] = _deep_merge(ret[key], val)
|
|
|
|
|
continue
|
|
|
|
|
ret[key] = deepcopy(val)
|
|
|
|
|
return ret
|
|
|
|
|
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
def _parse_link(value: str) -> tuple[bool, str | None, str]:
|
|
|
|
|
"""Parse a link value into (is_named, name_or_None, path_string).
|
|
|
|
|
|
2026-08-16 12:23:59 +02:00
|
|
|
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.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""
|
|
|
|
|
m = _LINK_NAME_RE.match(value)
|
|
|
|
|
if m:
|
|
|
|
|
return (True, m.group(1), value[len(m.group(1)) + 1:])
|
|
|
|
|
return (False, None, value)
|
|
|
|
|
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
def _resolve_path(
|
|
|
|
|
path_str: str,
|
|
|
|
|
current_path: tuple[str, ...],
|
|
|
|
|
root: dict[str, Any],
|
|
|
|
|
) -> tuple[tuple[str, ...], Any]:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""Resolve *path_str* and return ``(path, value)`` at that location.
|
|
|
|
|
|
|
|
|
|
Absolute paths are resolved from the document root; relative paths
|
|
|
|
|
(starting with ``.``) are resolved from *current_path*. Path segments
|
|
|
|
|
are separated by dots.
|
|
|
|
|
"""
|
|
|
|
|
if not path_str:
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
resolved_path: tuple[str, ...] = current_path
|
|
|
|
|
target: Any = root
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
for key in current_path:
|
|
|
|
|
target = target[key]
|
|
|
|
|
return (resolved_path, target)
|
|
|
|
|
|
|
|
|
|
if path_str.startswith('.'):
|
|
|
|
|
# Count consecutive leading dots to determine levels to go up
|
|
|
|
|
# from the link's parent. 1 dot = stay, 2 dots = up 1, etc.
|
|
|
|
|
dot_count = 0
|
|
|
|
|
for ch in path_str:
|
|
|
|
|
if ch == '.':
|
|
|
|
|
dot_count += 1
|
|
|
|
|
else:
|
|
|
|
|
break
|
|
|
|
|
levels_up = dot_count - 1
|
|
|
|
|
rest = path_str[dot_count:]
|
|
|
|
|
|
|
|
|
|
# Start from the link's parent (the dict that contains the link)
|
|
|
|
|
if not current_path:
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
parent_path: tuple[str, ...] = ()
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
else:
|
|
|
|
|
parent_path = current_path[:-1]
|
|
|
|
|
|
|
|
|
|
if levels_up > 0:
|
|
|
|
|
if levels_up > len(parent_path):
|
|
|
|
|
raise ValueError(f"Link target path goes above root: {path_str!r}")
|
|
|
|
|
new_path = parent_path[:-levels_up]
|
|
|
|
|
else:
|
|
|
|
|
new_path = parent_path
|
|
|
|
|
|
|
|
|
|
# Navigate from root to the base (new_path)
|
|
|
|
|
resolved_path = new_path
|
|
|
|
|
target = root
|
|
|
|
|
for key in new_path:
|
|
|
|
|
target = target[key]
|
|
|
|
|
|
|
|
|
|
if rest:
|
|
|
|
|
for key in rest.split('.'):
|
|
|
|
|
resolved_path = resolved_path + (key, )
|
|
|
|
|
target = target[key]
|
|
|
|
|
|
|
|
|
|
return (resolved_path, target)
|
|
|
|
|
# Absolute path from root
|
|
|
|
|
resolved_path = tuple(path_str.split('.'))
|
|
|
|
|
target = root
|
|
|
|
|
for key in path_str.split('.'):
|
|
|
|
|
target = target[key]
|
|
|
|
|
return (resolved_path, target)
|
|
|
|
|
|
2026-08-13 10:19:48 +02:00
|
|
|
# -- Include helpers ------------------------------------------------------------
|
|
|
|
|
|
2026-11-13 13:01:08 +01:00
|
|
|
_VAR_RE = re.compile(r'\$\(([A-Za-z_][A-Za-z0-9_]*)\)|\$\{([A-Za-z_][A-Za-z0-9_]*)\}')
|
|
|
|
|
|
|
|
|
|
def _expand_include_vars(value: str, where: str) -> str:
|
|
|
|
|
"""Expand $(VAR) and ${VAR} references in *value* from the environment.
|
|
|
|
|
|
|
|
|
|
Raises ValueError naming the reference and *where* when a
|
|
|
|
|
variable is not set.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __sub(m: re.Match[str]) -> str:
|
|
|
|
|
name = m.group(1) if m.group(1) is not None else m.group(2)
|
|
|
|
|
if name not in os.environ:
|
|
|
|
|
raise ValueError(f'Undefined variable {m.group(0)} in {where}')
|
|
|
|
|
return os.environ[name]
|
|
|
|
|
|
|
|
|
|
return _VAR_RE.sub(__sub, value)
|
|
|
|
|
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
def _glob_include(file_spec: str, parent: Path) -> list[Path]:
|
|
|
|
|
"""Resolve *file_spec* against *parent* and expand glob characters.
|
|
|
|
|
|
|
|
|
|
A spec without glob characters resolves to a single path, which
|
|
|
|
|
need not exist yet. A spec with glob characters is expanded to
|
|
|
|
|
the matching paths in lexical order (possibly an empty list).
|
|
|
|
|
"""
|
|
|
|
|
parts = file_spec.split('/')
|
|
|
|
|
for i, part in enumerate(parts):
|
|
|
|
|
if _GLOB_CHARS & set(part):
|
|
|
|
|
prefix = '/'.join(parts[:i])
|
|
|
|
|
if file_spec.startswith('/'):
|
|
|
|
|
base = Path(prefix)
|
|
|
|
|
else:
|
|
|
|
|
base = parent / prefix
|
|
|
|
|
return sorted(base.glob('/'.join(parts[i:])))
|
|
|
|
|
path = Path(file_spec)
|
|
|
|
|
return [path if path.is_absolute() else parent / path]
|
|
|
|
|
|
2026-11-13 13:01:08 +01:00
|
|
|
def _parse_include_spec(
|
|
|
|
|
spec: str,
|
|
|
|
|
path: Path,
|
|
|
|
|
expand_vars: bool,
|
|
|
|
|
) -> list[IncludeSpec]:
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""Parse a jw.include spec into one or more IncludeSpec entries.
|
|
|
|
|
|
|
|
|
|
The file part may use glob characters; every match becomes a
|
|
|
|
|
separate entry. A non-optional glob that matches no files raises
|
2026-11-13 13:01:08 +01:00
|
|
|
ValueError. When *expand_vars* is true (the top-level document
|
|
|
|
|
only), $(VAR) and ${VAR} references in the file part are expanded
|
|
|
|
|
from the environment before resolution.
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
"""
|
2026-08-13 10:19:48 +02:00
|
|
|
if not spec:
|
|
|
|
|
raise ValueError('Empty include spec')
|
|
|
|
|
optional = spec.startswith('-')
|
|
|
|
|
if optional:
|
|
|
|
|
spec = spec[1:]
|
|
|
|
|
if not spec:
|
|
|
|
|
raise ValueError('Empty include spec after removing optional marker')
|
|
|
|
|
# Check for target path syntax: "path.to.target|/file.yaml" (like named links)
|
|
|
|
|
target_path: tuple[str, ...] = ()
|
|
|
|
|
if '|' in spec:
|
|
|
|
|
m = _INCLUDE_PATH_RE.match(spec)
|
|
|
|
|
if m:
|
|
|
|
|
target_path = tuple(m.group(1).split('.'))
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
spec = spec[len(m.group(1)) + 1:]
|
2026-11-13 13:01:08 +01:00
|
|
|
if expand_vars:
|
|
|
|
|
spec = _expand_include_vars(spec, str(path))
|
|
|
|
|
paths = _glob_include(spec, path.parent)
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
if not paths and not optional:
|
2026-11-13 13:01:08 +01:00
|
|
|
raise ValueError(f'Include glob {spec!r} matches no files in {path}')
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
return [(optional, path, target_path) for path in paths]
|
2026-08-13 10:19:48 +02:00
|
|
|
|
2026-11-13 13:01:08 +01:00
|
|
|
def _normalize_include_specs(
|
|
|
|
|
specs: Any,
|
|
|
|
|
path: Path,
|
|
|
|
|
expand_vars: bool,
|
|
|
|
|
) -> Includes:
|
2026-08-13 10:19:48 +02:00
|
|
|
if specs is None:
|
|
|
|
|
return []
|
|
|
|
|
if isinstance(specs, str):
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
specs = [specs]
|
|
|
|
|
if not isinstance(specs, list):
|
|
|
|
|
raise TypeError(f'Unsupported include format: {specs!r}')
|
|
|
|
|
ret: Includes = []
|
|
|
|
|
for spec in specs:
|
|
|
|
|
if not isinstance(spec, str):
|
|
|
|
|
raise TypeError(f'Unsupported include entry: {spec!r}')
|
2026-11-13 13:01:08 +01:00
|
|
|
ret.extend(_parse_include_spec(spec, path, expand_vars))
|
load_yaml: Add globbing to jw.include specs
The jw.include file spec is resolved as a single literal file name,
so a set of files under one directory has to be listed one by one.
Let the file part of a spec use glob characters (*, ?, [...]).
_normalize_include_specs() expands every match into its own
IncludeSpec entry, so matches are loaded and deep-merged in lexical
order: at the root, later files win; at a target path, the existing
content wins as with a single include. A non-optional glob that
matches no files raises ValueError; the leading - prefix keeps a
glob optional. Document the globbing in the module docstring,
including the YAML quoting requirement for specs that start with a
glob character, and add tests covering the glob syntax, lexical
order, the no-match and optional cases, target-path merging,
absolute paths, and self-inclusion.
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-16 13:03:32 +02:00
|
|
|
return ret
|
2026-08-13 10:19:48 +02:00
|
|
|
|
|
|
|
|
# -- Include target-path merging -----------------------------------------------
|
|
|
|
|
|
|
|
|
|
def _merge_at_path(root: dict[str, Any], path: tuple[str, ...],
|
|
|
|
|
value: Any) -> dict[str, Any]:
|
|
|
|
|
"""Deep-merge *value* into *root* at the given *path*.
|
|
|
|
|
|
2026-08-16 12:15:47 +02:00
|
|
|
Creates intermediate dicts as needed. Raises ``ValueError`` when
|
|
|
|
|
an existing value on or at the target path is not a mapping.
|
|
|
|
|
Returns the modified root.
|
2026-08-13 10:19:48 +02:00
|
|
|
"""
|
|
|
|
|
if not path:
|
|
|
|
|
return _deep_merge(root, value)
|
2026-08-16 12:15:47 +02:00
|
|
|
path_str = '.'.join(path)
|
2026-08-13 10:19:48 +02:00
|
|
|
current = root
|
|
|
|
|
for i, key in enumerate(path[:-1]):
|
2026-08-16 12:15:47 +02:00
|
|
|
if key in current and not isinstance(current[key], dict):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f'Include target path {path_str}: value at '
|
|
|
|
|
f'{".".join(path[:i + 1])} is not a mapping'
|
|
|
|
|
)
|
|
|
|
|
if key not in current:
|
2026-08-13 10:19:48 +02:00
|
|
|
current[key] = {}
|
|
|
|
|
current = current[key]
|
|
|
|
|
last_key = path[-1]
|
2026-08-16 12:15:47 +02:00
|
|
|
if last_key in current:
|
|
|
|
|
if not isinstance(current[last_key], dict) or not isinstance(value, dict):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f'Include target path {path_str}: value at {path_str} '
|
|
|
|
|
f'is not a mapping'
|
|
|
|
|
)
|
2026-08-13 10:19:48 +02:00
|
|
|
# root's existing content wins (merge value into root)
|
|
|
|
|
current[last_key] = _deep_merge(value, current[last_key])
|
|
|
|
|
else:
|
|
|
|
|
current[last_key] = deepcopy(value)
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
# -- Link expansion -------------------------------------------------------------
|
|
|
|
|
|
2026-08-16 12:18:22 +02:00
|
|
|
def _resolve_link(
|
|
|
|
|
kind: str,
|
|
|
|
|
spec: Any,
|
|
|
|
|
current_path: tuple[str | int, ...],
|
|
|
|
|
root: dict[str, Any],
|
|
|
|
|
) -> tuple[bool, str | None, tuple[str, ...], Any]:
|
|
|
|
|
"""Parse *spec* and resolve its target path against *root*.
|
|
|
|
|
|
2026-08-16 12:18:55 +02:00
|
|
|
Returns ``(is_named, name, target_path, target_value)``. A
|
|
|
|
|
non-string spec raises ``TypeError``; a missing or invalid target
|
|
|
|
|
raises ``ValueError`` naming the link and its location. Path
|
|
|
|
|
navigation operates on the unexpanded tree, so intermediate
|
|
|
|
|
segments must not be link nodes themselves.
|
2026-08-16 12:18:22 +02:00
|
|
|
"""
|
2026-08-16 12:18:55 +02:00
|
|
|
if not isinstance(spec, str):
|
|
|
|
|
raise TypeError(
|
|
|
|
|
f'{kind} value must be a string, got {type(spec).__name__}: {spec!r}'
|
|
|
|
|
)
|
2026-08-16 12:18:22 +02:00
|
|
|
is_named, name, path = _parse_link(spec)
|
|
|
|
|
where = '.'.join(str(part) for part in current_path)
|
|
|
|
|
if not where:
|
|
|
|
|
where = '<root>'
|
|
|
|
|
try:
|
|
|
|
|
# current_path may contain int indices from list traversal,
|
|
|
|
|
# but _resolve_path only uses string keys for navigation
|
|
|
|
|
target_path, target_value = _resolve_path(
|
|
|
|
|
path, cast('tuple[str, ...]', current_path), root
|
|
|
|
|
)
|
|
|
|
|
except (KeyError, IndexError, TypeError) as exc:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f'{kind} {spec!r} at {where}: cannot resolve target path: {exc}'
|
|
|
|
|
) from None
|
|
|
|
|
return (is_named, name, target_path, target_value)
|
|
|
|
|
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
def _expand_links_recursive(
|
|
|
|
|
node: Any,
|
|
|
|
|
root: dict[str, Any],
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
current_path: tuple[str | int, ...],
|
|
|
|
|
resolved: dict[tuple[tuple[str | int, ...], str], Any],
|
|
|
|
|
resolving: set[tuple[tuple[str | int, ...], str]],
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
) -> Any:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
"""Recursively walk *node* and expand all link markers.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
|
|
|
|
``resolved`` is a memoisation cache mapping ``(path, link_spec)`` →
|
|
|
|
|
expanded value. ``resolving`` tracks links currently being expanded
|
|
|
|
|
to detect cycles.
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(node, dict):
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
# --- jw.inplace-link: can have siblings, merges into resolved target ---
|
|
|
|
|
if 'jw.inplace-link' in node:
|
|
|
|
|
link_spec = node['jw.inplace-link']
|
|
|
|
|
link_key = (current_path, link_spec)
|
|
|
|
|
|
|
|
|
|
if link_key in resolving:
|
|
|
|
|
raise ValueError(f"Cyclic link detected: {link_spec!r}")
|
|
|
|
|
if link_key in resolved:
|
2026-08-16 12:21:47 +02:00
|
|
|
# Deepcopy so that cached results are not aliased into
|
|
|
|
|
# multiple places of the output tree
|
|
|
|
|
base_value: Any = deepcopy(resolved[link_key])
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
else:
|
|
|
|
|
resolving.add(link_key)
|
2026-08-16 12:18:22 +02:00
|
|
|
_, _, target_path, target_value = _resolve_link(
|
|
|
|
|
'jw.inplace-link', link_spec, current_path, root
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
)
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
base_value = _expand_links_recursive(
|
2026-07-08 15:37:06 +02:00
|
|
|
target_value, root, target_path, resolved, resolving
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
)
|
|
|
|
|
resolved[link_key] = base_value
|
|
|
|
|
resolving.discard(link_key)
|
|
|
|
|
|
|
|
|
|
# Expand sibling keys
|
|
|
|
|
expanded_remaining: dict[str, Any] = {}
|
|
|
|
|
for key, value in node.items():
|
|
|
|
|
if key == 'jw.inplace-link':
|
|
|
|
|
continue
|
|
|
|
|
expanded_remaining[key] = _expand_links_recursive(
|
|
|
|
|
value, root, current_path + (key, ), resolved, resolving
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-16 12:16:57 +02:00
|
|
|
if not expanded_remaining:
|
|
|
|
|
return base_value
|
|
|
|
|
if not isinstance(base_value, dict):
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f'jw.inplace-link {link_spec!r}: resolved value is not '
|
|
|
|
|
f'a mapping, cannot merge siblings'
|
|
|
|
|
)
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
# Deep-merge siblings into resolved dict (siblings win on conflict)
|
2026-08-16 12:16:57 +02:00
|
|
|
return _deep_merge(base_value, expanded_remaining)
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
|
|
|
|
|
# --- jw.link (sole key): inplace or named replacement ---
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
if 'jw.link' in node and len(node) == 1:
|
|
|
|
|
link_spec = node['jw.link']
|
|
|
|
|
link_key = (current_path, link_spec)
|
|
|
|
|
|
|
|
|
|
if link_key in resolving:
|
|
|
|
|
raise ValueError(f"Cyclic link detected: {link_spec!r}")
|
|
|
|
|
if link_key in resolved:
|
2026-08-16 12:21:47 +02:00
|
|
|
# Deepcopy so that cached results are not aliased into
|
|
|
|
|
# multiple places of the output tree
|
|
|
|
|
return deepcopy(resolved[link_key])
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
|
|
|
|
resolving.add(link_key)
|
2026-08-16 12:18:22 +02:00
|
|
|
is_named, name, target_path, target_value = _resolve_link(
|
|
|
|
|
'jw.link', link_spec, current_path, root
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
)
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
expanded_target = _expand_links_recursive(
|
|
|
|
|
target_value, root, target_path, resolved, resolving
|
|
|
|
|
)
|
|
|
|
|
resolving.discard(link_key)
|
|
|
|
|
|
|
|
|
|
if is_named:
|
|
|
|
|
result = {name: expanded_target}
|
|
|
|
|
else:
|
|
|
|
|
result = expanded_target
|
|
|
|
|
|
|
|
|
|
resolved[link_key] = result
|
|
|
|
|
return result
|
|
|
|
|
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
# --- Normal dict — expand values recursively ---
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
expanded: dict[str, Any] = {}
|
|
|
|
|
for key, value in node.items():
|
|
|
|
|
expanded[key] = _expand_links_recursive(
|
|
|
|
|
value, root, current_path + (key, ), resolved, resolving
|
|
|
|
|
)
|
|
|
|
|
return expanded
|
|
|
|
|
|
|
|
|
|
if isinstance(node, list):
|
|
|
|
|
return [
|
|
|
|
|
_expand_links_recursive(
|
|
|
|
|
item, root, current_path + (i, ), resolved, resolving
|
|
|
|
|
) for i, item in enumerate(node)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return node # scalar or None
|
|
|
|
|
|
|
|
|
|
def _expand_links(root: dict[str, Any]) -> dict[str, Any]:
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
r"""Expand all link markers in *root* and return the result.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
Three link forms are supported:
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
- **Inplace** ``jw.link: "target.path"`` — the ``jw.link`` key
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
disappears and is replaced by the resolved value, inline.
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
- **Named** ``jw.link: "name|target.path"`` — the ``jw.link`` key
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
is renamed to ``name`` and its value is the resolved target.
|
load_yaml: Add jw.inplace-link with sibling merge
jw.inplace-link behaves like jw.link but can have sibling keys that
are deep-merged into the resolved target dict (siblings override target
on conflict).
Usage:
entry:
jw.inplace-link: path.to.target
sibling_key: value
Expands to:
entry:
<merged result of path.to.target + sibling_key: value>
This solves the common use case of pulling in a dict from elsewhere
while overriding specific keys inline.
Added 12 unit tests covering: no siblings, dict siblings, scalar
siblings, non-dict targets, relative paths, cycle detection, chains,
list siblings, list items, deep nesting, sibling override, and
integration with jw.include.
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-08 12:55:05 +02:00
|
|
|
- **Inplace** ``jw.inplace-link: "target.path"`` — the ``jw.inplace-link``
|
|
|
|
|
key disappears and its value is deep-merged into the resolved target
|
|
|
|
|
dict. Sibling keys (if any) override the target on conflict.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
2026-08-16 12:23:59 +02:00
|
|
|
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.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
2026-08-16 12:23:59 +02:00
|
|
|
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``.
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
"""
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
resolved: dict[tuple[tuple[str | int, ...], str], Any] = {}
|
|
|
|
|
resolving: set[tuple[tuple[str | int, ...], str]] = set()
|
2026-07-24 21:44:20 +02:00
|
|
|
return cast(
|
|
|
|
|
'dict[str, Any]', _expand_links_recursive(root, root, (), resolved, resolving)
|
|
|
|
|
)
|
load_yaml: Implement jw.link expansion
Implement link expansion in _expand_links() to resolve jw.link markers
in YAML documents after the include/merge phase. Two link forms are
supported:
- Inplace links jw.link: "path.to.target" replace the marker entirely
with the resolved value at the target path.
- Named links jw.link: "name|path.to.target" replace the marker with
{name: resolved_value}, using the first path segment as the key.
Paths use dot-separated segments. Relative paths start with ".."
(like Python imports): "." stays at the current level, ".." goes up
one level, "..." two, and so on. A single pipe character in the link
value separates the link name from the target path for named links.
The implementation includes:
- _parse_link(): splits a link value into (is_named, name, path)
- _resolve_path(): resolves an absolute or relative path to a value
- _expand_links_recursive(): walks the structure, expands links,
detects cycles via a resolving set, and memoises via a resolved dict.
Cyclic links and missing targets both raise ValueError/KeyError.
jw.link markers with sibling keys are left unexpanded.
Add 86 unit tests covering the entire load_yaml module:
- Basic YAML loading, optional loading, root type checking
- Include directive (single, multiple, deep merge, circular detect)
- Inplace links, named links, relative paths (up, descend, same level)
- Transitive links, cycle detection, missing targets
- Regression tests, combined include+links, edge cases, type preservation
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-03 11:26:06 +02:00
|
|
|
|
2026-08-13 10:19:48 +02:00
|
|
|
# -- Top-level entry points -----------------------------------------------------
|
|
|
|
|
|
2026-11-13 13:01:08 +01:00
|
|
|
def _load_yaml(
|
|
|
|
|
path: str | Path,
|
|
|
|
|
optional: bool,
|
|
|
|
|
seen: set[Path],
|
|
|
|
|
depth: int,
|
|
|
|
|
) -> dict[str, Any]:
|
2026-08-13 10:19:48 +02:00
|
|
|
path = Path(path).resolve()
|
|
|
|
|
if path in seen:
|
|
|
|
|
raise ValueError(f'Cyclic YAML include detected: {path}')
|
|
|
|
|
seen.add(path)
|
|
|
|
|
try:
|
|
|
|
|
try:
|
2026-08-16 12:20:42 +02:00
|
|
|
with open(path, encoding = 'utf-8') as f:
|
2026-08-13 10:19:48 +02:00
|
|
|
loaded = yaml.safe_load(f)
|
|
|
|
|
if loaded is None:
|
|
|
|
|
ret: dict[str, Any] = {}
|
|
|
|
|
elif isinstance(loaded, dict):
|
|
|
|
|
ret = loaded
|
|
|
|
|
else:
|
|
|
|
|
raise TypeError(
|
|
|
|
|
f'YAML root must be a mapping in {path!r}, '
|
|
|
|
|
f'got {type(loaded).__name__}'
|
|
|
|
|
)
|
|
|
|
|
ret = dict(ret)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
if optional:
|
|
|
|
|
return {}
|
|
|
|
|
raise
|
2026-11-13 13:01:08 +01:00
|
|
|
includes = _normalize_include_specs(
|
|
|
|
|
ret.get('jw.include', []), path, expand_vars = depth == 0
|
|
|
|
|
)
|
2026-08-13 10:19:48 +02:00
|
|
|
ret.pop('jw.include', None)
|
|
|
|
|
included_all: dict[str, Any] = {}
|
|
|
|
|
for include in includes:
|
|
|
|
|
opt, inc_path, target = include
|
2026-11-13 13:01:08 +01:00
|
|
|
included_next = _load_yaml(
|
|
|
|
|
inc_path, optional = opt, seen = seen, depth = depth + 1
|
|
|
|
|
)
|
2026-08-25 20:08:23 +02:00
|
|
|
if not included_next:
|
|
|
|
|
# An empty document contributes nothing; in particular
|
|
|
|
|
# it must not create the target key of a path-addressed
|
|
|
|
|
# include
|
|
|
|
|
continue
|
2026-08-13 10:19:48 +02:00
|
|
|
if target:
|
|
|
|
|
# Merge at specific target path in the main document
|
|
|
|
|
ret = _merge_at_path(ret, target, included_next)
|
|
|
|
|
else:
|
|
|
|
|
included_all = _deep_merge(included_all, included_next)
|
|
|
|
|
return _deep_merge(included_all, ret)
|
|
|
|
|
finally:
|
|
|
|
|
seen.remove(path)
|
|
|
|
|
|
2026-06-13 20:31:42 +02:00
|
|
|
def load_yaml(path: str | Path, optional: bool = False) -> dict[str, Any]: # export
|
|
|
|
|
seen: set[Path] = set()
|
2026-11-13 13:01:08 +01:00
|
|
|
return _expand_links(_load_yaml(path, optional, seen, depth = 0))
|
2026-06-13 20:31:42 +02:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
|
2026-08-16 12:22:17 +02:00
|
|
|
if len(sys.argv) != 2:
|
|
|
|
|
sys.exit(f'usage: {sys.argv[0]} FILE.yaml')
|
|
|
|
|
print(json.dumps(load_yaml(sys.argv[1]), indent = 2, default = str))
|