更新了部分测试
This commit is contained in:
@@ -65,9 +65,9 @@ class ApiDataSource(BaseDataSource):
|
||||
"""关闭数据源(关闭 HTTP 客户端)"""
|
||||
await self.client.close()
|
||||
|
||||
def register_handler(self, handler: NodeHandler) -> None:
|
||||
def add_handler(self, handler: NodeHandler) -> None:
|
||||
"""
|
||||
注册业务 Handler
|
||||
添加业务 Handler
|
||||
|
||||
Args:
|
||||
handler: API Handler 实例
|
||||
@@ -75,8 +75,8 @@ class ApiDataSource(BaseDataSource):
|
||||
handler.set_api_client(self.client)
|
||||
handler.set_poll_mode(self.poll_mode)
|
||||
|
||||
# 调用基类注册方法
|
||||
super().register_handler(handler)
|
||||
# 调用基类添加方法
|
||||
super().add_handler(handler)
|
||||
|
||||
# 如果 collection 已设置,确保新 handler 拿到引用
|
||||
if self._collection is not None:
|
||||
|
||||
@@ -57,9 +57,9 @@ class BaseDataSource(ABC):
|
||||
# 1. 初始化 DataSource
|
||||
datasource = MyDataSource()
|
||||
|
||||
# 2. 注册 Handler
|
||||
datasource.register_handler(project_handler)
|
||||
datasource.register_handler(contract_handler)
|
||||
# 2. 添加 Handler
|
||||
datasource.add_handler(project_handler)
|
||||
datasource.add_handler(contract_handler)
|
||||
|
||||
# 3. 设置 Collection
|
||||
datasource.set_collection(collection)
|
||||
@@ -360,15 +360,15 @@ class BaseDataSource(ABC):
|
||||
|
||||
# ========== Handler 管理 ==========
|
||||
|
||||
def register_handler(self, handler: "NodeHandler") -> None:
|
||||
def add_handler(self, handler: "NodeHandler") -> None:
|
||||
"""
|
||||
注册 Handler
|
||||
添加 Handler 到当前 datasource
|
||||
|
||||
Args:
|
||||
handler: 节点处理器实例
|
||||
|
||||
示例:
|
||||
datasource.register_handler(ContractNodeHandler(api_client))
|
||||
datasource.add_handler(ContractNodeHandler(api_client))
|
||||
"""
|
||||
self._handlers[handler.node_type] = handler
|
||||
|
||||
|
||||
@@ -495,59 +495,6 @@ class BaseNodeHandler(ABC, Generic[T]):
|
||||
return self.create_node(data)
|
||||
|
||||
|
||||
class CompatibleNodeHandler(BaseNodeHandler[T]):
|
||||
def __init__(self, handler: BaseNodeHandler[T]):
|
||||
super().__init__(handler.node_type, handler.node_class, handler.schema)
|
||||
self._handler = handler
|
||||
|
||||
def set_collection(self, collection: "DataCollection") -> None:
|
||||
super().set_collection(collection)
|
||||
self._handler.set_collection(collection)
|
||||
|
||||
def set_handler_config(self, config: Dict[str, Any]) -> None:
|
||||
self._handler.set_handler_config(config)
|
||||
self.handler_config = dict(self._handler.handler_config)
|
||||
|
||||
def set_api_client(self, client: "ApiClient") -> None:
|
||||
self._handler.set_api_client(client)
|
||||
|
||||
def set_poll_mode(self, mode: str) -> None:
|
||||
self._handler.set_poll_mode(mode)
|
||||
|
||||
def get_update_fields(self, *, exclude: frozenset[str] = frozenset({"id"})) -> List[str]:
|
||||
return self._handler.get_update_fields(exclude=exclude)
|
||||
|
||||
async def load(self) -> List['SyncNode']:
|
||||
return await self._handler.load()
|
||||
|
||||
async def create_all(
|
||||
self,
|
||||
nodes: List["SyncNode"]
|
||||
) -> List[TaskResult]:
|
||||
return await self._handler.create_all(nodes)
|
||||
|
||||
async def update_all(
|
||||
self,
|
||||
nodes: List["SyncNode"]
|
||||
) -> List[TaskResult]:
|
||||
return await self._handler.update_all(nodes)
|
||||
|
||||
async def delete_all(
|
||||
self,
|
||||
nodes: List["SyncNode"]
|
||||
) -> List[TaskResult]:
|
||||
return await self._handler.delete_all(nodes)
|
||||
|
||||
async def poll_tasks(self, task_ids: List[str]) -> Dict[str, TaskResult]:
|
||||
return await self._handler.poll_tasks(task_ids)
|
||||
|
||||
async def validate(self, data: Dict[str, Any]) -> ValidationResult:
|
||||
return await self._handler.validate(data)
|
||||
|
||||
def extract_id(self, data: Dict[str, Any]) -> str:
|
||||
return self._handler.extract_id(data)
|
||||
|
||||
|
||||
class NodeHandlerRegistry:
|
||||
"""节点处理器注册表"""
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class JsonlDataSource(BaseDataSource):
|
||||
|
||||
示例:
|
||||
datasource = JsonlDataSource(Path("filtered_datasource/datasource/ecm-2025-12-02"))
|
||||
datasource.register_handler(ContractJsonlHandler(datasource))
|
||||
datasource.add_handler(ContractJsonlHandler(datasource))
|
||||
|
||||
collection = DataCollection()
|
||||
datasource.set_collection(collection)
|
||||
|
||||
@@ -4,13 +4,13 @@ Domain Handler Registry - 集中管理所有领域的 API 和 JSONL Handlers
|
||||
使用方式:
|
||||
from sync_state_machine.domain.registry import API_HANDLERS, JSONL_HANDLERS
|
||||
|
||||
# 注册所有 API handlers
|
||||
# 添加所有 API handlers
|
||||
for handler_class in API_HANDLERS:
|
||||
api_ds.register_handler(handler_class())
|
||||
api_ds.add_handler(handler_class())
|
||||
|
||||
# 注册所有 JSONL handlers
|
||||
# 添加所有 JSONL handlers
|
||||
for handler_class in JSONL_HANDLERS:
|
||||
jsonl_ds.register_handler(handler_class(jsonl_ds))
|
||||
jsonl_ds.add_handler(handler_class(jsonl_ds))
|
||||
"""
|
||||
|
||||
# ============================================
|
||||
|
||||
@@ -67,7 +67,7 @@ class CopyDataPipeline:
|
||||
supported_types = []
|
||||
for handler_class in JSONL_HANDLERS:
|
||||
handler = handler_class(self.source_datasource)
|
||||
self.source_datasource.register_handler(handler)
|
||||
self.source_datasource.add_handler(handler)
|
||||
supported_types.append(handler.node_type)
|
||||
self._owns_source = True
|
||||
self._supported_types = supported_types
|
||||
@@ -82,7 +82,7 @@ class CopyDataPipeline:
|
||||
self.target_datasource = JsonlDataSource(dir_path=Path(target_dir), read_only=False)
|
||||
# 自动注册所有 JSONL handlers
|
||||
for handler_class in JSONL_HANDLERS:
|
||||
self.target_datasource.register_handler(handler_class(self.target_datasource))
|
||||
self.target_datasource.add_handler(handler_class(self.target_datasource))
|
||||
self._owns_target = True
|
||||
else:
|
||||
raise ValueError("Must provide either target_datasource or target_dir")
|
||||
@@ -228,7 +228,7 @@ class CopyDataPipeline:
|
||||
handler_class = DomainRegistry.get_jsonl_handler(node_type)
|
||||
if handler_class:
|
||||
handler = handler_class(datasource)
|
||||
datasource.register_handler(handler)
|
||||
datasource.add_handler(handler)
|
||||
|
||||
def _print_summary(self):
|
||||
"""打印执行摘要"""
|
||||
|
||||
@@ -151,14 +151,14 @@ async def _initialize_datasource(datasource, *, endpoint_name: str) -> None:
|
||||
ensure_builtin_datasource_types_registered()
|
||||
|
||||
|
||||
def _register_handlers_for_endpoint(
|
||||
def _add_handlers_for_endpoint(
|
||||
*,
|
||||
datasource,
|
||||
ds_config: DataSourceConfig,
|
||||
node_types: List[str],
|
||||
) -> None:
|
||||
for node_type in node_types:
|
||||
datasource.register_handler(
|
||||
datasource.add_handler(
|
||||
_create_handler(
|
||||
node_type=node_type,
|
||||
ds_config=ds_config,
|
||||
@@ -167,18 +167,18 @@ def _register_handlers_for_endpoint(
|
||||
)
|
||||
|
||||
|
||||
def _register_handlers(
|
||||
def _add_handlers(
|
||||
config: PipelineRunConfig,
|
||||
local_ds,
|
||||
remote_ds,
|
||||
all_types: List[str],
|
||||
) -> None:
|
||||
_register_handlers_for_endpoint(
|
||||
_add_handlers_for_endpoint(
|
||||
datasource=local_ds,
|
||||
ds_config=config.local_datasource,
|
||||
node_types=all_types,
|
||||
)
|
||||
_register_handlers_for_endpoint(
|
||||
_add_handlers_for_endpoint(
|
||||
datasource=remote_ds,
|
||||
ds_config=config.remote_datasource,
|
||||
node_types=all_types,
|
||||
@@ -320,7 +320,7 @@ async def create_pipeline_from_config(
|
||||
project_root=_project_root(),
|
||||
)
|
||||
await _initialize_datasource(remote_ds, endpoint_name="remote")
|
||||
_register_handlers(config, local_ds, remote_ds, all_types)
|
||||
_add_handlers(config, local_ds, remote_ds, all_types)
|
||||
|
||||
local_collection, remote_collection, binding_manager = _build_collections_and_bindings(
|
||||
persistence=persistence,
|
||||
|
||||
@@ -172,15 +172,6 @@ class FullSyncPipeline:
|
||||
self._logger.error(f"❌ Strategy failed but pipeline continues: {node_type} | {type(exc).__name__}: {exc}")
|
||||
continue
|
||||
|
||||
@staticmethod
|
||||
def _is_noop_strategy(strategy: BaseSyncStrategy[Any]) -> bool:
|
||||
cfg = strategy.config
|
||||
return (
|
||||
cfg.local_orphan_action == OrphanAction.NONE
|
||||
and cfg.remote_orphan_action == OrphanAction.NONE
|
||||
and cfg.update_direction == UpdateDirection.NONE
|
||||
)
|
||||
|
||||
async def phase_bootstrap(self) -> None:
|
||||
await self._load_persistence_state()
|
||||
self._logger.info("✅ Bootstrap: persistence loaded")
|
||||
|
||||
Reference in New Issue
Block a user