"""Connector registry — single source of truth for source_type -> connector.""" from __future__ import annotations from typing import Any _CONNECTORS: dict[str, Any] = {} def register_connector(connector: Any) -> None: """Register a SourceConnector instance under its ``source_type``. Calling twice with the same ``source_type`` replaces the prior entry — useful for tests and hot-reload, but in production each connector should be registered exactly once at startup. """ if not getattr(connector, "source_type", None): raise ValueError("Connector must declare a non-empty source_type") _CONNECTORS[connector.source_type] = connector def get_connector(source_type: str) -> Any: """Return the registered connector for ``source_type`` or raise KeyError.""" try: return _CONNECTORS[source_type] except KeyError as exc: raise KeyError(f"No connector registered for source_type {source_type!r}") from exc def _reset_for_tests() -> None: """Clear the registry — for use in pytest fixtures only.""" _CONNECTORS.clear()