wardrobe-us / src /detector /_registry.py
Ox1's picture
refactor(detector): replace monolithic detector with pluggable multi-backend package
746317a
Raw
History Blame Contribute Delete
1.53 kB
"""Backend registry for garment detectors."""
import logging
from typing import Type
from ._types import DetectorBackend
logger = logging.getLogger(__name__)
_BACKENDS: dict[str, Type[DetectorBackend]] = {}
_INSTANCES: dict[str, DetectorBackend] = {}
def register(name: str):
"""Decorator to register a detector backend class."""
def decorator(cls: Type[DetectorBackend]):
_BACKENDS[name] = cls
return cls
return decorator
def get_backend(name: str) -> DetectorBackend:
"""Get a backend instance by name (lazy-instantiated singleton)."""
if name not in _BACKENDS:
available = list_available()
raise ValueError(
f"Unknown detector backend: '{name}'. "
f"Available: {available}"
)
if name not in _INSTANCES:
_INSTANCES[name] = _BACKENDS[name]()
return _INSTANCES[name]
def list_registered() -> list[str]:
"""List all registered backend names (even if deps are missing)."""
return list(_BACKENDS.keys())
def list_available() -> list[tuple[str, str]]:
"""List backends whose dependencies are installed.
Returns (name, description) tuples for UI display.
"""
available = []
for name, cls in _BACKENDS.items():
try:
instance = get_backend(name)
if instance.is_available():
available.append((instance.description, name))
except Exception as e:
logger.debug("Backend '%s' not available: %s", name, e)
return available