Payload builders for TOML-domain machine-readable output.
This module contains pure helpers that build strongly typed payload dataclasses
for machine-readable TOML provenance output.
Responsibilities
This module performs no I/O and does not shape JSON envelopes or NDJSON
records.
build_toml_provenance_payload
build_toml_provenance_payload(resolved_toml)
Build a machine-readable layered TOML provenance payload.
Parameters:
Returns:
Raises:
| Type |
Description |
ValueError
|
If config provenance layers do not align with the resolved
TOML sources, or if config provenance layer resolved to an invalid
TOML source.
|
TomlRenderError
|
If an invalid files serialization mode is specified
while rendering API- or CLI-originated layers back to TopMark TOML.
|
Source code in src/topmark/toml/machine/payloads.py
| def build_toml_provenance_payload(
resolved_toml: ResolvedTopmarkTomlSources,
) -> TomlProvenancePayload:
"""Build a machine-readable layered TOML provenance payload.
Args:
resolved_toml: Resolved TOML sources for the current run.
Returns:
Provenance payload with ordered layers, starting with the built-in
defaults layer when present.
Raises:
ValueError: If config provenance layers do not align with the resolved
TOML sources, or if config provenance layer resolved to an invalid
TOML source.
TomlRenderError: If an invalid files serialization mode is specified
while rendering API- or CLI-originated layers back to TopMark TOML.
""" # noqa: DOC503
layers: list[ConfigLayer] = build_config_layers_from_resolved_toml_sources(
resolved_toml.sources
)
# Keep payload layers aligned with the resolved/config-derived layer order so
# the machine-readable provenance view mirrors the effective precedence model.
out_layers: list[TomlProvenanceLayerPayload] = []
# File-backed sources are consumed only for non-synthetic layers. Synthetic
# default/API/CLI layers are reconstructed directly from the in-memory config.
remaining_sources: Iterator[ResolvedTopmarkTomlSource] = iter(resolved_toml.sources)
for layer in layers:
scope_root: str | None = (
format_machine_path(layer.scope_root) if layer.scope_root is not None else None
)
if layer.kind == ConfigLayerKind.DEFAULT:
toml_fragment: dict[str, object] = _normalize_toml_fragment(
build_default_topmark_toml_table()
)
elif layer.kind in {ConfigLayerKind.API, ConfigLayerKind.CLI}:
toml_fragment = _normalize_toml_fragment(
config_to_topmark_toml_table(layer.config.freeze()),
) # May raise TomlRenderError for invalid file-serialization settings.
else:
source: ResolvedTopmarkTomlSource | None = next(remaining_sources, None)
while source is not None and source.parsed is None:
source = next(remaining_sources, None)
if source is None:
raise ValueError("config provenance layers do not align with resolved TOML sources")
parsed: ParsedTopmarkToml | None = source.parsed
if parsed is None:
raise ValueError("config provenance layer resolved to an invalid TOML source")
toml_fragment = _normalize_toml_fragment(parsed.toml_fragment)
out_layers.append(
TomlProvenanceLayerPayload(
origin=format_config_source_path(layer.origin),
kind=layer.kind.value,
precedence=layer.precedence,
scope_root=scope_root,
toml=toml_fragment,
)
)
discovery_anchor: str | None = (
format_machine_path(resolved_toml.discovery_anchor)
if resolved_toml.discovery_anchor is not None
else None
)
return TomlProvenancePayload(
layers=out_layers,
discovery_anchor=discovery_anchor,
)
|