整理代码

This commit is contained in:
strepsiades
2026-04-03 09:18:44 +08:00
parent 543c3ff906
commit f5729d5a18
50 changed files with 305 additions and 296 deletions
@@ -65,22 +65,10 @@ class ApiDataSource(BaseDataSource):
"""关闭数据源(关闭 HTTP 客户端)"""
await self.client.close()
def add_handler(self, handler: NodeHandler) -> None:
"""
添加业务 Handler
Args:
handler: API Handler 实例
"""
def _prepare_handler_for_registration(self, handler: NodeHandler) -> None:
handler.set_api_client(self.client)
handler.set_poll_mode(self.poll_mode)
# 调用基类添加方法
super().add_handler(handler)
# 如果 collection 已设置,确保新 handler 拿到引用
if self._collection is not None:
handler.set_collection(self._collection) # type: ignore[arg-type]
super()._prepare_handler_for_registration(handler)
@staticmethod
def _fmt_trace_value(value, max_len: int = 600) -> str:
+12 -3
View File
@@ -43,8 +43,17 @@ class BaseApiHandler(BaseNodeHandler[T]):
_schema: Any
def __init__(self, **handler_config: Any):
# 子类应该在调用 super().__init__() 之前或之后设置 _node_type, _node_class, _schema
super().__init__()
# 子类可直接传入 node_class;少数特殊场景可显式覆盖 schema/node_type。
node_class = handler_config.pop("node_class", None)
node_type = handler_config.pop("node_type", None)
schema = handler_config.pop("schema", None)
if node_class is not None and node_type is None:
node_type = getattr(node_class, "node_type", None)
if node_class is not None and schema is None:
schema = getattr(node_class, "schema", None)
super().__init__(node_type=node_type, node_class=node_class, schema=schema)
self.api_client: 'ApiClient' = None # type: ignore # 由 DataSource 注入
self.poll_mode: str = "async" # async | sync
self._collection: 'DataCollection' = None # type: ignore # 由 DataSource 注入
@@ -67,7 +76,7 @@ class BaseApiHandler(BaseNodeHandler[T]):
def set_collection(self, collection: 'DataCollection') -> None:
"""设置 Collection(由 DataSource 调用)"""
self._collection = collection
super().set_collection(collection)
def ensure_api_client(self) -> 'ApiClient':
if self.api_client is None:
@@ -163,6 +163,11 @@ class BaseDataSource(ABC):
"""Datasource-specific hook after a FAILED result is applied to node."""
return
def _prepare_handler_for_registration(self, handler: "NodeHandler") -> None:
"""Allow subclasses to inject datasource-specific dependencies before registration."""
if self._collection is not None:
handler.set_collection(self._collection)
async def _handle_success_result(self, node: "SyncNode", result: "TaskResult") -> None:
previous_data_id = node.data_id
ok = self._apply_sync_execute_state(
@@ -371,6 +376,7 @@ class BaseDataSource(ABC):
示例:
datasource.add_handler(ContractNodeHandler(api_client))
"""
self._prepare_handler_for_registration(handler)
self._handlers[handler.node_type] = handler
def get_handler(self, node_type: str) -> "NodeHandler":
+14 -4
View File
@@ -43,11 +43,21 @@ class BaseJsonlHandler(BaseNodeHandler):
def __init__(
self,
datasource: JsonlDataSource,
node_type: str,
node_class: type,
schema: type
node_class_or_node_type,
node_class: type | None = None,
schema: type | None = None,
node_type: str | None = None,
):
super().__init__(node_type, node_class, schema)
if isinstance(node_class_or_node_type, str):
resolved_node_type = node_class_or_node_type
resolved_node_class = node_class
resolved_schema = schema
else:
resolved_node_class = node_class_or_node_type
resolved_node_type = node_type or getattr(resolved_node_class, "node_type", None)
resolved_schema = schema or getattr(resolved_node_class, "schema", None)
super().__init__(resolved_node_type, resolved_node_class, resolved_schema)
self.datasource = datasource
async def load(self) -> List['SyncNode']: