Skip to content

yaml

yaml

Centralised YAML I/O — round-trip mode everywhere.

Facade over ruamel.yaml's ceremony-heavy YAML() class: callers get a minimal load / dump / YAMLError surface instead of instance creation, typ selection, stream management, and config attributes.

Adapter from ruamel.yaml's stream-oriented API to the string-based convention used throughout the ecosystem (path.read_text()load(text) → modify → dump(data)path.write_text(text)).

CommentedMap is a dict subclass — isinstance(x, dict), .get(), x["key"], .setdefault() all work transparently. Pydantic v2 model_validate() accepts dict subclasses, so read-side validation is unchanged.

__all__ = ['load', 'dump', 'YAMLError'] module-attribute

load(text)

Round-trip load from a YAML string, preserving comments and order.

Source code in src/terok_util/yaml.py
def load(text: str) -> Any:
    """Round-trip load from a YAML string, preserving comments and order."""
    return _rt().load(text)

dump(data, *, default_flow_style=False)

Round-trip dump to a YAML string, preserving comments and order.

Key order is preserved (insertion order for new dicts, original order for round-tripped data). sort_keys is always False — the caller never needs to pass it.

Source code in src/terok_util/yaml.py
def dump(data: Any, *, default_flow_style: bool = False) -> str:
    """Round-trip dump to a YAML string, preserving comments and order.

    Key order is preserved (insertion order for new dicts, original order for
    round-tripped data).  ``sort_keys`` is always ``False`` — the caller never
    needs to pass it.
    """
    emitter = _rt()
    if default_flow_style:
        emitter.default_flow_style = True
    buf = StringIO()
    emitter.dump(data, buf)
    return buf.getvalue()