Source code for simvx.core.assets.sources.mem
"""In-memory source: for tests and dynamically generated assets.
URI form: ``mem://<key>``. The ``MemSource`` instance owns a dict
mapping keys to bytes; tests pre-populate it and the AssetServer
resolves loads from there.
"""
from __future__ import annotations
from collections.abc import Iterable
[docs]
class MemSource:
scheme = "mem"
def __init__(self) -> None:
self.data: dict[str, bytes] = {}
[docs]
def put(self, uri: str, contents: bytes) -> None:
if not uri.startswith("mem://"):
raise ValueError(f"MemSource expects mem:// URI, got {uri!r}")
self.data[uri] = contents
[docs]
def read_bytes(self, uri: str) -> bytes:
if uri not in self.data:
raise FileNotFoundError(uri)
return self.data[uri]
[docs]
def version(self, uri: str) -> str | None:
if uri not in self.data:
return None
return f"len:{len(self.data[uri])}"
[docs]
def list(self, uri: str) -> Iterable[str]:
if not uri.endswith("/"):
uri = uri + "/"
for key in self.data:
if key.startswith(uri) and key != uri:
yield key