"""数据处理工具的装配工厂 —— **唯一需要改动的替换入口**。 默认加载 ``pipeline.DefaultCustomsProcessor``。 如果要把整个数据处理工具换成另一套实现(例如改成 PySpark 版本、换成 流式处理、或换一个完全不同口径的统计引擎),只要满足 ``contract.CustomsProcessor`` 协议,然后用环境变量指向它: .. code-block:: bash set CUSTOMS_PROCESSOR=my_company.customs:MyProcessor 平台其余模块(BFF、Agent 工具、前端 /api/process)都不需要改动。 """ from __future__ import annotations import importlib import os from .contract import CustomsProcessor from .pipeline import DefaultCustomsProcessor DEFAULT_SPEC = "toolsvc.customs.pipeline:DefaultCustomsProcessor" def _load(spec: str) -> CustomsProcessor: module_name, _, attribute = spec.partition(":") if not module_name or not attribute: raise ValueError(f"CUSTOMS_PROCESSOR 格式应为 '模块:类名',当前为 {spec!r}") module = importlib.import_module(module_name) target = getattr(module, attribute) instance = target() if isinstance(target, type) else target for method in ("process",): if not hasattr(instance, method): raise TypeError(f"{spec} 未实现 CustomsProcessor.{method}") return instance _cached: CustomsProcessor | None = None def load_processor() -> CustomsProcessor: """返回单例处理器(线程内复用规则库缓存)。""" global _cached if _cached is None: _cached = _load(os.environ.get("CUSTOMS_PROCESSOR", DEFAULT_SPEC)) return _cached def reset_processor() -> None: """测试用:清掉缓存。""" global _cached _cached = None