feat(execution): add executor registry (register/get only)

This commit is contained in:
ogabrielluiz
2026-05-04 17:24:32 -03:00
parent 9a4421e1eb
commit 79786880fe
2 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,27 @@
"""Executor registry."""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from lfx.execution.executor import Executor
class ExecutorNotFoundError(LookupError):
pass
class ExecutorRegistry:
def __init__(self) -> None:
self._by_kind: dict[str, Executor] = {}
def register(self, executor: Executor) -> None:
self._by_kind[executor.kind] = executor
def get(self, kind: str) -> Executor:
try:
return self._by_kind[kind]
except KeyError as exc:
msg = f"No executor registered for kind={kind!r}"
raise ExecutorNotFoundError(msg) from exc

View File

@ -0,0 +1,31 @@
import pytest
from lfx.execution.executor import Executor
from lfx.execution.registry import ExecutorNotFoundError, ExecutorRegistry
class _Stub(Executor):
kind = "stub"
async def execute(self, unit): # noqa: ARG002
return
yield
def test_register_and_get_by_kind():
registry = ExecutorRegistry()
registry.register(_Stub())
assert registry.get("stub").kind == "stub"
def test_get_unknown_raises():
with pytest.raises(ExecutorNotFoundError, match="nope"):
ExecutorRegistry().get("nope")
def test_register_replaces_same_kind():
registry = ExecutorRegistry()
a = _Stub()
b = _Stub()
registry.register(a)
registry.register(b)
assert registry.get("stub") is b