Lazy-load adapters at package boundaries and fail fast with install hints when extras are missing. Co-authored-by: Cursor <[email protected]>
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
"""python_repositories: Unified repository interfaces and adapters."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
from typing import TYPE_CHECKING
|
|
|
|
# Interfaces are always available; they have no optional backend dependencies.
|
|
from .interfaces import (
|
|
ConnectionAwareInterface,
|
|
ContextAwareInterface,
|
|
JsonRepositoryInterface,
|
|
ObjectRepositoryInterface,
|
|
)
|
|
|
|
# Adapters are imported only for static type checkers; runtime loading is deferred below.
|
|
if TYPE_CHECKING:
|
|
from .adapters.minio_adapter import MinioAdapter
|
|
from .adapters.redis_adapter import RedisAdapter
|
|
|
|
# Map public adapter names to their defining module and class.
|
|
# Each adapter module fails fast with an install hint if its extra is missing.
|
|
_LAZY_EXPORTS = {
|
|
"RedisAdapter": (".adapters.redis_adapter", "RedisAdapter"),
|
|
"MinioAdapter": (".adapters.minio_adapter", "MinioAdapter"),
|
|
}
|
|
|
|
__all__ = [
|
|
"ConnectionAwareInterface",
|
|
"ContextAwareInterface",
|
|
"JsonRepositoryInterface",
|
|
"ObjectRepositoryInterface",
|
|
"RedisAdapter",
|
|
"MinioAdapter",
|
|
]
|
|
|
|
|
|
def __getattr__(name: str) -> object:
|
|
"""Load adapters on first access so the base package installs without redis/minio."""
|
|
if name in _LAZY_EXPORTS:
|
|
module_path, attr = _LAZY_EXPORTS[name]
|
|
module = importlib.import_module(module_path, __package__)
|
|
return getattr(module, attr)
|
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
|
|
|
|
def __dir__() -> list[str]:
|
|
"""Expose lazy adapter names in tab completion and dir()."""
|
|
return sorted(__all__)
|