comicbox.schemas.cache

[docs] module comicbox.schemas.cache

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""Cache for marshmallow schema instances."""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from pathlib import Path

    from comicbox.schemas.base import BaseSchema

_schema_cache: dict[tuple, BaseSchema] = {}


_EMPTY_FROZENSET: frozenset = frozenset()


def get_schema(
    cls: type[BaseSchema],
    path: Path | str | None = None,
    exclude: frozenset | tuple | set = _EMPTY_FROZENSET,
) -> BaseSchema:
    """Get a cached schema instance, creating one if needed."""
    key = (cls, frozenset(exclude) if exclude else frozenset())
    if key not in _schema_cache:
        _schema_cache[key] = cls(path=path, exclude=exclude)
    schema = _schema_cache[key]
    schema.set_path(path)
    return schema