""":class:`ResourceCacheRepository` base for the resource-management API.Every read-only bundled-data resource in the project is exposedthrough one :class:`ResourceRepository` implementation. Therepository owns its loader and its Identity Map cache; consumersgo through :class:`ResourceRegistry` instead of importing loaderfunctions directly.The base class implements the ``get(key)`` / ``clear_cache``contract on top of an unbounded ``dict[K, T]``. Subclassesoverride ``_load(key) -> T`` to perform the actual file readand Pydantic validation. The cache strategy is documented inthe resource-management-api ADR: process-lifetime memoisation,no eviction, because the bundled data is immutable per install."""from__future__importannotationsfromcollections.abcimportHashable,IterablefromtypingimportProtocol,runtime_checkable
[docs]defget(self,key:K)->T:"""Return the resource identified by ``key``."""...
[docs]defall(self)->Iterable[T]:"""Return every resource the repository can produce."""...
[docs]defclear_cache(self)->None:"""Empty this repository's Identity Map."""...
[docs]classResourceCacheRepository[T,K:Hashable]:"""Default Repository implementation with an Identity Map cache. Subclasses override :meth:`_load` to read and validate one resource per key. The base class owns the cache behind the :class:`ResourceRepository` protocol; subclasses never touch it directly. """def__init__(self)->None:self._cache:dict[K,T]={}
[docs]defget(self,key:K)->T:"""Return the resource for ``key``, loading on first access."""cached=self._cache.get(key)ifcachedisnotNone:returncachedloaded=self._load(key)self._cache[key]=loadedreturnloaded
[docs]defall(self)->Iterable[T]:"""Return every resource the repository can produce. The default implementation raises ``NotImplementedError`` because enumeration requires per-repository knowledge of the available keys. Subclasses with a finite key space (year-keyed catalogues, etc.) override. """raiseNotImplementedError(f"{type(self).__name__} does not implement all(); override per repository")
[docs]defclear_cache(self)->None:"""Empty the Identity Map. Tests that override Settings or otherwise change the bundled-data location between cases call this to force a reload on the next ``get``. Production code should not need to call this because the bundled data is immutable. """self._cache.clear()
def_load(self,key:K)->T:"""Load one resource for ``key``. Subclasses MUST override. The default raises ``NotImplementedError`` so a subclass that forgets to implement loading fails immediately on first access. """raiseNotImplementedError(f"{type(self).__name__}._load")