oc_connect
Loading...
Searching...
No Matches
registry.py
Go to the documentation of this file.
1"""Explicit-import connector registration.
2
3Dependencies
4------------
5- oc_diagnostics (_dbg structured logger)
6- oc_connect.connectors.telegram (lazy; the shipped Telegram bundle, imported for its registration side effect)
7"""
8from __future__ import annotations
9
10from oc_diagnostics import _dbg
11
12
14 """Small in-memory registry with fail-loud conflict handling."""
15
16 def __init__(self) -> None:
17 self._connectors: dict[str, object] = {}
18
19 def register(self, name: str, connector: object) -> object:
20 """Register a connector object, allowing only identical repeats."""
21 if not name.strip():
22 raise ValueError("connector name must not be blank")
23 existing = self._connectors.get(name)
24 if existing is not None and existing is not connector:
25 raise ValueError(f"connector {name!r} is already registered")
26 self._connectors[name] = connector
27 _dbg("OC_CONNECT.REGISTRY", "register", f"registered connector {name}", 3)
28 return connector
29
30 def get(self, name: str) -> object:
31 """Return a registered connector or fail with its public name."""
32 try:
33 return self._connectors[name]
34 except KeyError:
35 raise KeyError(f"connector {name!r} is not registered") from None
36
37 def names(self) -> tuple[str, ...]:
38 """Return registered names in deterministic order."""
39 return tuple(sorted(self._connectors))
40
41 def clear(self) -> None:
42 """Remove registrations; intended for isolated tests."""
43 self._connectors.clear()
44
45
47
48
49def load_builtin_connectors() -> ConnectorRegistry:
50 """Load shipped bundles by explicit import, never entry points."""
51 from .connectors import telegram as _telegram
52
53 _dbg(
54 "OC_CONNECT.REGISTRY",
55 "load_builtin_connectors",
56 f"loaded bundle module {_telegram.__name__}",
57 3,
58 )
59 return registry
60
61
62__all__ = ["ConnectorRegistry", "load_builtin_connectors", "registry"]
object get(self, str name)
Definition registry.py:30
object register(self, str name, object connector)
Definition registry.py:19
ConnectorRegistry load_builtin_connectors()
Definition registry.py:49