mirror of
https://github.com/langflow-ai/langflow.git
synced 2026-07-23 17:06:46 +08:00
feat(execution): add executor registry (register/get only)
This commit is contained in:
27
src/lfx/src/lfx/execution/registry.py
Normal file
27
src/lfx/src/lfx/execution/registry.py
Normal 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
|
||||
31
src/lfx/tests/unit/execution/test_registry.py
Normal file
31
src/lfx/tests/unit/execution/test_registry.py
Normal 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
|
||||
Reference in New Issue
Block a user