进一步清理代码和注册过程

This commit is contained in:
strepsiades
2026-03-18 17:23:31 +08:00
parent 32fa374e86
commit f9f175ee79
7 changed files with 42 additions and 49 deletions
+4 -8
View File
@@ -76,7 +76,7 @@ class MemoryHandler(BaseNodeHandler[MemorySchema]):
return []
class MemoryRemoteDbHandler(BaseNodeHandler[MemorySchema]):
class MemoryRemoteHandler(BaseNodeHandler[MemorySchema]):
__test__ = False
def __init__(self, datasource: BaseDataSource):
@@ -102,8 +102,7 @@ def _ensure_memory_domain_registered() -> None:
reg.schema = MemorySchema
reg.node_class = MemoryNode
reg.strategy_class = MemoryStrategy
reg.db_handler_class = MemoryRemoteDbHandler
reg.handler_classes["db"] = MemoryRemoteDbHandler
reg.handler_classes["memory_remote"] = MemoryRemoteHandler
reg.handler_classes["memory"] = MemoryHandler
return
@@ -112,9 +111,9 @@ def _ensure_memory_domain_registered() -> None:
schema=MemorySchema,
node_class=MemoryNode,
strategy_class=MemoryStrategy,
db_handler_class=MemoryRemoteDbHandler,
)
DomainRegistry.register_handler("memory_demo", "memory", MemoryHandler)
DomainRegistry.register_handler("memory_demo", "memory_remote", MemoryRemoteHandler)
def _memory_factory(config: MemoryConfig, endpoint_name: str) -> MemoryDataSource:
@@ -154,14 +153,11 @@ async def test_create_pipeline_from_config_supports_registered_custom_datasource
await pipeline.close()
def test_build_config_supports_same_file_datasource_bootstrap(tmp_path: Path) -> None:
def test_build_config_supports_custom_datasource_type_loading(tmp_path: Path) -> None:
register_datasource_type("memory_bootstrap", config_model=MemoryConfig, factory=_memory_factory, replace=True)
profile_path = tmp_path / "custom-memory.yaml"
profile_path.write_text(
"extensions:\n"
" bootstraps:\n"
" - memory_bootstrap_demo:register\n"
"node_types: []\n"
"local_datasource:\n"
" type: memory_bootstrap\n"
@@ -20,9 +20,9 @@ from sync_state_machine.pipeline.factory import create_pipeline_from_config
from sync_state_machine.sync_system.strategy import DefaultSyncStrategy
class EndpointInitDbConfig(BaseDataSourceConfig):
class EndpointInitConfig(BaseDataSourceConfig):
__test__ = False
type: str = "endpoint_init_db"
type: str = "endpoint_init_memory"
class EndpointInitSchema(BaseModel):
@@ -42,7 +42,7 @@ class EndpointInitStrategy(DefaultSyncStrategy[EndpointInitSchema]):
schema = EndpointInitSchema
class EndpointInitDbHandler(BaseNodeHandler[EndpointInitSchema]):
class EndpointInitHandler(BaseNodeHandler[EndpointInitSchema]):
__test__ = False
def __init__(self, datasource: BaseDataSource):
@@ -69,8 +69,7 @@ def _ensure_registered() -> None:
reg.schema = EndpointInitSchema
reg.node_class = EndpointInitNode
reg.strategy_class = EndpointInitStrategy
reg.db_handler_class = EndpointInitDbHandler
reg.handler_classes["endpoint_init_db"] = EndpointInitDbHandler
reg.handler_classes["endpoint_init_memory"] = EndpointInitHandler
return
DomainRegistry.register(
@@ -78,15 +77,14 @@ def _ensure_registered() -> None:
schema=EndpointInitSchema,
node_class=EndpointInitNode,
strategy_class=EndpointInitStrategy,
db_handler_class=EndpointInitDbHandler,
)
DomainRegistry.register_handler(node_type, "endpoint_init_db", EndpointInitDbHandler)
DomainRegistry.register_handler(node_type, "endpoint_init_memory", EndpointInitHandler)
def _register_endpoint_init_datasource_type() -> None:
register_datasource_type(
"endpoint_init_db",
config_model=EndpointInitDbConfig,
"endpoint_init_memory",
config_model=EndpointInitConfig,
factory=lambda config, endpoint_name: BaseDataSource(),
replace=True,
)
@@ -102,13 +100,13 @@ async def test_factory_initializes_same_datasource_type_with_distinct_handler_co
config = PipelineRunConfig(
node_types=["factory_endpoint_demo"],
local_datasource=EndpointInitDbConfig(
type="endpoint_init_db",
local_datasource=EndpointInitConfig(
type="endpoint_init_memory",
handler_configs={"factory_endpoint_demo": {"side": "local", "batch_size": 10}},
target_project_ids=["project_local"],
),
remote_datasource=EndpointInitDbConfig(
type="endpoint_init_db",
remote_datasource=EndpointInitConfig(
type="endpoint_init_memory",
handler_configs={"factory_endpoint_demo": {"side": "remote", "batch_size": 20}},
target_project_ids=["project_remote"],
),
@@ -122,8 +120,8 @@ async def test_factory_initializes_same_datasource_type_with_distinct_handler_co
local_handler = pipeline.local_datasource.get_handler("factory_endpoint_demo")
remote_handler = pipeline.remote_datasource.get_handler("factory_endpoint_demo")
assert isinstance(local_handler, EndpointInitDbHandler)
assert isinstance(remote_handler, EndpointInitDbHandler)
assert isinstance(local_handler, EndpointInitHandler)
assert isinstance(remote_handler, EndpointInitHandler)
assert local_handler.handler_config == {"side": "local", "batch_size": 10}
assert remote_handler.handler_config == {"side": "remote", "batch_size": 20}
finally:
@@ -22,7 +22,7 @@ class RegistryLookupApiHandler:
__test__ = False
class RegistryLookupDbHandler:
class RegistryLookupCustomHandler:
__test__ = False
@@ -33,7 +33,7 @@ def test_registry_supports_unified_handler_lookup() -> None:
reg = DomainRegistry.get_registration(node_type)
reg.jsonl_handler_class = RegistryLookupJsonlHandler
reg.api_handler_class = RegistryLookupApiHandler
reg.db_handler_class = RegistryLookupDbHandler
reg.handler_classes["custom"] = RegistryLookupCustomHandler
else:
DomainRegistry.register(
node_type=node_type,
@@ -41,9 +41,9 @@ def test_registry_supports_unified_handler_lookup() -> None:
node_class=RegistryLookupNode,
jsonl_handler_class=RegistryLookupJsonlHandler,
api_handler_class=RegistryLookupApiHandler,
db_handler_class=RegistryLookupDbHandler,
)
DomainRegistry.register_handler(node_type, "custom", RegistryLookupCustomHandler)
assert DomainRegistry.get_handler(node_type, "jsonl") is RegistryLookupJsonlHandler
assert DomainRegistry.get_handler(node_type, "api") is RegistryLookupApiHandler
assert DomainRegistry.get_handler(node_type, "db") is RegistryLookupDbHandler
assert DomainRegistry.get_handler(node_type, "custom") is RegistryLookupCustomHandler