Files
iAOP/templates/ti-cl4/llm-scenarios/tests/_bootstrap.py
T

35 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""测试引导:加载连字符目录为可导入包,使场景模块可复用内核组件。
- `templates/ti-cl4/llm-scenarios` → 包名 ``ti_scenarios``;
- `core/llm-gateway` → 包名 ``llm_gateway``(场景依赖);
- `core/rag-kb` → 包名 ``rag_kb``(场景依赖)。
与仓库内各 core 模块的测试引导同款模式;这里用 importlib 完整加载包
(执行 __init__.py),保持 ``from rag_kb import ...`` 顶层导出可用。
"""
import importlib.util
import os
import sys
SCEN_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
def _load_package(name: str, path: str) -> None:
"""按文件路径完整加载一个包(执行其 __init__.py)。"""
if name in sys.modules:
return
init_py = os.path.join(path, "__init__.py")
spec = importlib.util.spec_from_file_location(
name, init_py, submodule_search_locations=[path])
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
_load_package("rag_kb", os.path.join(REPO_ROOT, "core", "rag-kb"))
_load_package("llm_gateway", os.path.join(REPO_ROOT, "core", "llm-gateway"))
_load_package("ti_scenarios", SCEN_DIR)