cadrumo.core.toml module¶
Canonical TOML read, freeze and render helpers.
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. render_toml() is the inverse used to
present a parsed document back to a reader: its output parses to a value
equal to the one it was given.
read_toml() and freeze_toml() are used by the registry
source loader that parses committed registry definitions.
to_str_keyed_dict() is the narrow bridge from loosely typed
parsed TOML mappings into strict schema models that require string keys.
The parse is backed by the Rust rtoml parser, which is several times
faster than the standard-library parser on the registry tree and returns the
same native Python types for every TOML value shape
(str/int/float/bool/list/dict/date/datetime/time). It does not produce 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. parse_toml() and
load_toml() are the only places the parser is called; every other
module reads TOML through them.
- exception TomlDecodeError(message=None, *, context=None, translated_message=None)[source]¶
Bases:
CoreValidationErrorA document is not valid TOML.
- Parameters:
- Return type:
None
- code¶
- parse_toml(text)[source]¶
Parse TOML text into its top-level table.
The table is typed as the standard-library parser typed it, so callers that index a document they authored keep doing so;
read_toml()is the strictly typed boundary for committed registry input.- Raises:
TomlDecodeError – The text is not valid TOML.
- Return type:
- Parameters:
text (str)
- load_toml(source)[source]¶
Parse a TOML document from a path or an open binary file.
- Raises:
TomlDecodeError – The document is not valid TOML.
UnicodeDecodeError – The bytes are not UTF-8.
OSError – The path cannot be read.
- Return type:
- Parameters:
- 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 (TomlDecodeError).
- 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.
- render_toml(document, *, comments=None)[source]¶
Render a parsed TOML document as TOML text that parses back to an equal value.
Nested tables become
[a.b]headers and a non-empty array whose every element is a table becomes[[a.b]]blocks, so a registry table renders in the shape its source files take; every other value is written inline. Within a table, plain values precede its sub-tables because TOML binds a key to the most recent header. Sequences may be lists or the tuplesfreeze_toml()produces.- Parameters:
- Raises:
TypeError – When a key is not a string or a value has no TOML form.
- Return type: