diff --git a/src/cli/blackboard.py b/src/cli/blackboard.py index 343285e..8c1aa9d 100644 --- a/src/cli/blackboard.py +++ b/src/cli/blackboard.py @@ -15,12 +15,26 @@ from src.blackboard.registry import ProjectRegistry def _find_project_root() -> Path: - """从环境变量或默认路径找项目根目录""" + """从环境变量、配置文件或默认路径找项目根目录""" import os + # 1. 环境变量最高优先级 root = os.environ.get("BLACKBOARD_ROOT") if root: return Path(root) - return Path.home() / ".sanguo_projects" / "sanguo_moziplus_v2" / "projects" + # 2. 从 config/default.yaml 读取 + try: + import yaml + config_path = Path(__file__).parent.parent / "config" / "default.yaml" + if config_path.exists(): + with open(config_path) as f: + cfg = yaml.safe_load(f) or {} + data_root = cfg.get("data_root") + if data_root: + return Path(data_root).expanduser() + except Exception: + pass + # 3. 相对于源码目录的 data/ + return Path(__file__).parent.parent / "data" def _get_bb(project_id: str) -> Blackboard: diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..9c3dac7 --- /dev/null +++ b/src/utils.py @@ -0,0 +1,37 @@ +"""路径解析工具 — 统一获取项目数据根目录 + +优先级: +1. BLACKBOARD_ROOT 环境变量 +2. config/default.yaml 的 data_root +3. 相对于源码目录的 data/ +""" + +from __future__ import annotations + +import os +from pathlib import Path +from typing import Optional + + +def get_data_root() -> Path: + """获取项目数据根目录""" + # 1. 环境变量最高优先级 + root = os.environ.get("BLACKBOARD_ROOT") + if root: + return Path(root) + + # 2. 从 config/default.yaml 读取 + try: + import yaml + config_path = Path(__file__).parent.parent / "config" / "default.yaml" + if config_path.exists(): + with open(config_path) as f: + cfg = yaml.safe_load(f) or {} + data_root = cfg.get("data_root") + if data_root: + return Path(data_root).expanduser() + except Exception: + pass + + # 3. 相对于源码目录的 data/ + return Path(__file__).parent.parent / "data"