SPB Git forge

spb/ai-atlas

Public
41commits 1branches 0releases
4.6 MBsize
maindefault branch
12 days agolast push
HTML 77.2% TypeScript 10.5% Python 9.6% JavaScript 2.5%
1.6 KB · 47 lines python
Raw Blame History
1"""Connector registry. Every module under `aiatlas.connectors.*` that defines `CONNECTORS = [cls, …]` is auto-registered.23Add a connector: create `aiatlas/connectors/<group>/<name>.py` with a `BaseConnector` subclass and list it in the module's4`CONNECTORS`. Register its source in `registry/sources.yaml`, then `aia seed` and `aia run <name>`.5"""6from __future__ import annotations78import importlib9import logging10import pkgutil11from functools import lru_cache1213from aiatlas.sdk.connector import BaseConnector1415log = logging.getLogger(__name__)161718@lru_cache19def registry() -> dict[str, type[BaseConnector]]:20    import aiatlas.connectors as pkg2122    found: dict[str, type[BaseConnector]] = {}23    for mod in pkgutil.walk_packages(pkg.__path__, prefix="aiatlas.connectors."):24        try:25            module = importlib.import_module(mod.name)26        except Exception as exc:  # noqa: BLE00127            log.error("cannot import connector module", extra={"module_name": mod.name, "error": str(exc)})28            continue29        for cls in getattr(module, "CONNECTORS", []):30            if not cls.name:31                raise RuntimeError(f"{cls} has no name")32            if cls.name in found and found[cls.name] is not cls:33                raise RuntimeError(f"duplicate connector name {cls.name}")34            found[cls.name] = cls35    return dict(sorted(found.items()))363738def get(name: str, config: dict | None = None) -> BaseConnector:39    try:40        cls = registry()[name]41    except KeyError:42        raise KeyError(f"unknown connector {name!r}; known: {', '.join(registry())}") from None43    return cls(config)444546__all__ = ["get", "registry"]47