Files
sanguo_moziplus_v2/src/utils.py
T
2026-05-17 07:28:45 +08:00

38 lines
988 B
Python

"""路径解析工具 — 统一获取项目数据根目录
优先级:
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"