Source code for simvx.core.assets.sources.file
"""Filesystem source: resolves ``file://`` URIs and bare relative paths."""
from __future__ import annotations
from collections.abc import Iterable
from pathlib import Path
[docs]
class FileSource:
"""Reads bytes from the local filesystem.
Accepts both explicit ``file:///abs/path`` URIs and bare paths; bare
paths are resolved against the current working directory.
"""
scheme = "file"
@staticmethod
def _path(uri: str) -> Path:
if uri.startswith("file://"):
return Path(uri[len("file://"):])
return Path(uri)
[docs]
def read_bytes(self, uri: str) -> bytes:
return self._path(uri).read_bytes()
[docs]
def version(self, uri: str) -> str | None:
try:
return f"mtime:{self._path(uri).stat().st_mtime_ns}"
except OSError:
return None
[docs]
def list(self, uri: str) -> Iterable[str]:
path = self._path(uri)
if not path.is_dir():
raise NotADirectoryError(f"FileSource.list expects a directory URI, got {uri!r}")
prefix = "file://" if uri.startswith("file://") else ""
for child in sorted(path.iterdir()):
if prefix:
yield prefix + str(child)
else:
yield str(child)