jabara.codecs.registry のソースコード

"""The codec registry: it maps a codec id and parameters to a configured codec."""

from __future__ import annotations

from collections.abc import Mapping
from typing import TypeVar

from pydantic import JsonValue

from jabara.codecs.base import Codec
from jabara.errors import UnknownCodecError

C = TypeVar("C", bound=type[Codec])

_CODECS: dict[str, type[Codec]] = {}


[ドキュメント] def register_codec(cls: C) -> C: """Class decorator that registers a codec under its `id`. Raises `ValueError` if the id is already registered.""" if cls.id in _CODECS: raise ValueError(f"codec {cls.id!r} is already registered") _CODECS[cls.id] = cls return cls
[ドキュメント] def get_codec(codec_id: str, params: Mapping[str, JsonValue] | None = None) -> Codec: """Return a codec instance for `codec_id`, configured with `params`, a JSON object that the codec's `Params` model checks. Raises `UnknownCodecError` for an unknown id and `pydantic.ValidationError` for invalid parameters. """ try: cls = _CODECS[codec_id] except KeyError: raise UnknownCodecError(f"unknown codec {codec_id!r}") from None return cls(cls.Params.model_validate(params or {}))
[ドキュメント] def codec_ids() -> list[str]: """Return the registered codec ids, sorted.""" return sorted(_CODECS)