Merge branch 'dev' into runny827
feat(training): 新增精益培训视频与周任务管理 # Conflicts: # backend/app/models/__init__.py
This commit is contained in:
1 parent
85d27e9ffa
commit
91b7233872
812 files changed
+73763
-18908
No files matched your search
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
backend/app/services/voucher_check/adapters/registry.py
|
||||
|
||||
用途:按 source_type / adapter 名解析已注册 Adapter。
|
||||
依赖:adapters.base。
|
||||
逻辑:模块 import 时 register;引擎禁止使用未登记的 source_type。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable
|
||||
|
||||
from app.core.errors import ValidationError
|
||||
|
||||
_REGISTRY: dict[str, Callable[[], Any]] = {}
|
||||
|
||||
|
||||
def register_adapter(name: str, factory: Callable[[], Any]) -> None:
|
||||
"""登记 Adapter 工厂。同名后注册覆盖,便于测试注入。"""
|
||||
key = str(name or "").strip()
|
||||
if not key:
|
||||
raise ValidationError("Adapter 名不能为空", code="VOUCHER_CHECK.CONFIG_INVALID")
|
||||
_REGISTRY[key] = factory
|
||||
|
||||
|
||||
def get_adapter(name: str) -> Any:
|
||||
"""
|
||||
按名称构造 Adapter。
|
||||
核心逻辑:未注册则配置失败,避免引擎落到未知来源。
|
||||
"""
|
||||
key = str(name or "").strip()
|
||||
factory = _REGISTRY.get(key)
|
||||
if factory is None:
|
||||
raise ValidationError(
|
||||
f"未注册的 Adapter: {key}",
|
||||
code="VOUCHER_CHECK.CONFIG_INVALID",
|
||||
details={"adapter": key},
|
||||
)
|
||||
return factory()
|
||||
|
||||
|
||||
def registered_adapter_names() -> tuple[str, ...]:
|
||||
"""返回已登记名称,供测试断言。"""
|
||||
return tuple(sorted(_REGISTRY))
|
||||
Reference in new issue
Block a user