Source code for timetoshare.validation.order

"""Graphical display-order and band-order rules."""

from __future__ import annotations

from collections.abc import Collection, Iterator, Mapping, Sequence
from typing import Any

from .diagnostics import InstancePath, SchemaValidationError, ValidationCode, entries_of

_DEFAULT_BAND_ORDER = (
    "children",
    "conversion_maps",
    "line",
    "markers",
    "regions",
    "flow_control",
)


def _listing_problems(
    code: ValidationCode,
    owner: str,
    order: list[str],
    expected: Sequence[str],
    path: InstancePath,
    messages: tuple[str, str],
    required: Collection[str] | None = None,
) -> Iterator[SchemaValidationError]:
    """Yield each entry of ``order`` that ``expected`` lacks, then each entry
    of ``required`` (all of ``expected`` where it is ``None``) that ``order``
    lacks, in the order of ``expected``."""
    stranger, missing = messages
    allowed = set(expected)
    listed = set(order)
    needed = allowed if required is None else set(required)
    for index, entry in enumerate(order):
        if entry not in allowed:
            yield SchemaValidationError(
                code,
                (*path, index),
                stranger.format(entry=f"'{entry}'", owner=f"'{owner}'"),
            )
    for entry in expected:
        if entry in needed and entry not in listed:
            yield SchemaValidationError(
                code,
                path,
                missing.format(entry=f"'{entry}'", owner=f"'{owner}'"),
            )


[docs] def order_problems( manifest: Mapping[str, Any], timelines: Collection[str], virtual: Collection[str], first_group: Mapping[str, int], ) -> Iterator[SchemaValidationError]: """Yield unknown order keys and invalid display and band orders. A display order is a permutation of its group's members. A band order lists ``line`` and any of the five facets, each once; a facet it leaves out takes its place from the order the timeline follows without an entry. ``timelines`` holds every timeline ID, ``virtual`` every virtual timeline ID and ``first_group`` the index of the first group of each group ID, as the identity rules collected them. A virtual timeline's entry is a band order. """ groups = entries_of(manifest, "groups") orders = (manifest.get("graphical") or {}).get("order") or {} for key in sorted(orders): path = ("graphical", "order", key) if key in first_group: group = groups[first_group[key]] yield from _listing_problems( "display_order", key, orders[key], group["members"], path, ( "{entry} is not a member of group {owner}", "member {entry} of group {owner} is missing", ), ) elif key in timelines or key in virtual: yield from _listing_problems( "band_order", key, orders[key], _DEFAULT_BAND_ORDER, path, ( "{entry} is not 'line' or a facet, in the band order of {owner}", "{entry} is missing from the band order of {owner}", ), ("line",), ) else: yield SchemaValidationError( "order_key", path, f"'{key}' names no group, timeline or virtual timeline of this " "manifest", )