aeat.core._toml module¶
Canonical TOML read + freeze helpers shared across loaders.
Multiple committed-TOML loaders (registry definitions, the user-profile schema) need the same two operations: parse a TOML file with errors re-raised as a domain-specific exception, and recursively freeze the parsed mapping so list values become tuples. These helpers are the single source of that behaviour.
read_toml() and freeze_toml() are used by the registry
loader in domain.calculations.registry._loader and the
user-profile schema loader in domain.user_profile._loader.
parse_toml_text() gives the same decode-error wrapping to callers that
already hold a TOML payload in memory, such as the secure bucket manifest
reader. to_str_keyed_dict() is the narrow bridge from loosely typed
parsed TOML mappings into strict schema models that require string keys.
The parse itself is backed by rtoml (a Rust-extension TOML parser),
not stdlib tomllib, per the registry-toml-parser ADR: ~2.4x
faster on the registry’s ~16k-fragment tree, with output proven
byte-identical to tomllib across every fragment the registry ships.
Both parsers return the same native Python types for every TOML value shape
(str/int/float/bool/list/dict/date/datetime/time); neither ever produces a
decimal.Decimal (TOML has no native decimal type), so registry money
and rate values continue to arrive as plain TOML strings, coerced to
Decimal downstream by pydantic field validators exactly as before.
- read_toml(path, *, error_factory)[source]¶
Parse a TOML file, re-raising failures via
error_factory.- Parameters:
- Return type:
- Returns:
The parsed top-level TOML mapping.
- Raises:
Exception – The exception built by
error_factorywhen the file cannot be read (OSError) or contains invalid TOML (rtoml.TomlParsingError).
- parse_toml_text(text, *, error_factory)[source]¶
Parse an in-memory TOML string, re-raising decode failures via
error_factory.The text-input sibling to
read_toml()for callers that have already loaded the bytes (e.g. throughpathlib.Path.read_text(), secure-object payload decoding, or in-memory test fixtures) and need consistent error wrapping without the file-open layer.- Parameters:
- Return type:
- Returns:
The parsed top-level TOML mapping.
- Raises:
Exception – The exception built by
error_factorywhen the payload is not valid TOML.
- to_str_keyed_dict(raw, *, error_factory)[source]¶
Convert a parsed TOML mapping to a str-keyed dict, rejecting non-string keys.
- Parameters:
- Return type:
- Returns:
A new dict with the same items and string keys.
- Raises:
Exception – The exception built by
error_factorywhen any key is not astr.
- freeze_toml_value(value)[source]¶
Recursively freeze one parsed TOML value, turning lists into tuples.
The TOML parser returns mutable Python lists for TOML arrays. Registry and profile-schema models use strict frozen contracts, so loader boundaries call this helper before validating nested payloads. Mappings stay mappings with their values frozen recursively; scalar TOML values are returned unchanged.
- freeze_toml(data)[source]¶
Recursively freeze a parsed TOML mapping (lists become tuples).
This is the mapping-level companion to
freeze_toml_value(), used afterread_toml()when a committed TOML document is about to be validated as a frozen registry, category, IVA, or user-profile schema object.