|
@@ -15,12 +15,13 @@ import os
|
|
|
class PathConfig:
|
|
class PathConfig:
|
|
|
"""路径配置管理类"""
|
|
"""路径配置管理类"""
|
|
|
|
|
|
|
|
- def __init__(self, account_name: Optional[str] = None):
|
|
|
|
|
|
|
+ def __init__(self, account_name: Optional[str] = None, output_version: Optional[str] = None):
|
|
|
"""
|
|
"""
|
|
|
初始化路径配置
|
|
初始化路径配置
|
|
|
|
|
|
|
|
Args:
|
|
Args:
|
|
|
account_name: 账号名称,如果不指定则使用默认账号或环境变量
|
|
account_name: 账号名称,如果不指定则使用默认账号或环境变量
|
|
|
|
|
+ output_version: 输出版本,如果不指定则使用项目根目录名称
|
|
|
"""
|
|
"""
|
|
|
# 获取项目根目录
|
|
# 获取项目根目录
|
|
|
self.project_root = Path(__file__).parent.parent.parent
|
|
self.project_root = Path(__file__).parent.parent.parent
|
|
@@ -29,12 +30,18 @@ class PathConfig:
|
|
|
# 加载配置
|
|
# 加载配置
|
|
|
self._load_config()
|
|
self._load_config()
|
|
|
|
|
|
|
|
|
|
+ # 获取数据根目录
|
|
|
|
|
+ self.data_root = self._get_data_root()
|
|
|
|
|
+
|
|
|
# 确定账号名称
|
|
# 确定账号名称
|
|
|
self.account_name = self._determine_account_name(account_name)
|
|
self.account_name = self._determine_account_name(account_name)
|
|
|
|
|
|
|
|
|
|
+ # 确定输出版本(默认使用项目根目录名)
|
|
|
|
|
+ self.output_version = self._determine_output_version(output_version)
|
|
|
|
|
+
|
|
|
# 构建路径
|
|
# 构建路径
|
|
|
account_base = self.config["paths"]["account_base"]
|
|
account_base = self.config["paths"]["account_base"]
|
|
|
- self.account_dir = self.project_root / account_base / self.account_name
|
|
|
|
|
|
|
+ self.account_dir = self.data_root / account_base / self.account_name
|
|
|
|
|
|
|
|
def _load_config(self):
|
|
def _load_config(self):
|
|
|
"""加载配置文件"""
|
|
"""加载配置文件"""
|
|
@@ -44,6 +51,34 @@ class PathConfig:
|
|
|
with open(self.config_file, "r", encoding="utf-8") as f:
|
|
with open(self.config_file, "r", encoding="utf-8") as f:
|
|
|
self.config = json.load(f)
|
|
self.config = json.load(f)
|
|
|
|
|
|
|
|
|
|
+ def _get_data_root(self) -> Path:
|
|
|
|
|
+ """
|
|
|
|
|
+ 获取数据根目录
|
|
|
|
|
+
|
|
|
|
|
+ 优先级:
|
|
|
|
|
+ 1. 环境变量 DATA_ROOT
|
|
|
|
|
+ 2. 配置文件 data_root
|
|
|
|
|
+ 3. 默认值 project_root/data(向后兼容)
|
|
|
|
|
+ """
|
|
|
|
|
+ # 1. 环境变量
|
|
|
|
|
+ data_root = os.environ.get("DATA_ROOT")
|
|
|
|
|
+ if data_root:
|
|
|
|
|
+ return Path(os.path.expanduser(data_root))
|
|
|
|
|
+
|
|
|
|
|
+ # 2. 配置文件
|
|
|
|
|
+ data_root_config = self.config.get("data_root")
|
|
|
|
|
+ if data_root_config:
|
|
|
|
|
+ # 支持 ~ 和环境变量
|
|
|
|
|
+ expanded = os.path.expandvars(os.path.expanduser(data_root_config))
|
|
|
|
|
+ path = Path(expanded)
|
|
|
|
|
+ if path.is_absolute():
|
|
|
|
|
+ return path
|
|
|
|
|
+ else:
|
|
|
|
|
+ return self.project_root / path
|
|
|
|
|
+
|
|
|
|
|
+ # 3. 默认值(向后兼容)
|
|
|
|
|
+ return self.project_root / "data"
|
|
|
|
|
+
|
|
|
def _determine_account_name(self, account_name: Optional[str]) -> str:
|
|
def _determine_account_name(self, account_name: Optional[str]) -> str:
|
|
|
"""
|
|
"""
|
|
|
确定要使用的账号名称
|
|
确定要使用的账号名称
|
|
@@ -81,6 +116,38 @@ class PathConfig:
|
|
|
"3. 配置文件: 在 config/accounts.json 中设置 default_account"
|
|
"3. 配置文件: 在 config/accounts.json 中设置 default_account"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ def _determine_output_version(self, output_version: Optional[str]) -> str:
|
|
|
|
|
+ """
|
|
|
|
|
+ 确定输出版本
|
|
|
|
|
+
|
|
|
|
|
+ 优先级:
|
|
|
|
|
+ 1. 函数参数
|
|
|
|
|
+ 2. 环境变量 OUTPUT_VERSION
|
|
|
|
|
+ 3. 配置文件中的 output_version
|
|
|
|
|
+ 4. 项目根目录名称(默认)
|
|
|
|
|
+ """
|
|
|
|
|
+ # 1. 参数指定
|
|
|
|
|
+ if output_version:
|
|
|
|
|
+ return output_version
|
|
|
|
|
+
|
|
|
|
|
+ # 2. 环境变量
|
|
|
|
|
+ env_version = os.environ.get("OUTPUT_VERSION")
|
|
|
|
|
+ if env_version:
|
|
|
|
|
+ return env_version
|
|
|
|
|
+
|
|
|
|
|
+ # 3. 配置文件指定
|
|
|
|
|
+ config_version = self.config.get("output_version")
|
|
|
|
|
+ if config_version:
|
|
|
|
|
+ return config_version
|
|
|
|
|
+
|
|
|
|
|
+ # 4. 使用项目根目录名称(默认)
|
|
|
|
|
+ project_dir_name = self.project_root.name
|
|
|
|
|
+ return project_dir_name
|
|
|
|
|
+
|
|
|
|
|
+ def _replace_version_var(self, path_template: str) -> str:
|
|
|
|
|
+ """替换路径模板中的 {version} 变量"""
|
|
|
|
|
+ return path_template.replace("{version}", self.output_version)
|
|
|
|
|
+
|
|
|
def get_enabled_accounts(self) -> List[str]:
|
|
def get_enabled_accounts(self) -> List[str]:
|
|
|
"""获取所有启用的账号列表"""
|
|
"""获取所有启用的账号列表"""
|
|
|
accounts = self.config.get("accounts", [])
|
|
accounts = self.config.get("accounts", [])
|
|
@@ -130,6 +197,7 @@ class PathConfig:
|
|
|
def intermediate_dir(self) -> Path:
|
|
def intermediate_dir(self) -> Path:
|
|
|
"""中间结果目录"""
|
|
"""中间结果目录"""
|
|
|
rel_path = self.config["paths"]["output"]["intermediate"]
|
|
rel_path = self.config["paths"]["output"]["intermediate"]
|
|
|
|
|
+ rel_path = self._replace_version_var(rel_path)
|
|
|
return self.account_dir / rel_path
|
|
return self.account_dir / rel_path
|
|
|
|
|
|
|
|
@property
|
|
@property
|
|
@@ -156,12 +224,14 @@ class PathConfig:
|
|
|
def how_results_dir(self) -> Path:
|
|
def how_results_dir(self) -> Path:
|
|
|
"""how解构结果目录"""
|
|
"""how解构结果目录"""
|
|
|
rel_path = self.config["paths"]["output"]["how_results"]
|
|
rel_path = self.config["paths"]["output"]["how_results"]
|
|
|
|
|
+ rel_path = self._replace_version_var(rel_path)
|
|
|
return self.account_dir / rel_path
|
|
return self.account_dir / rel_path
|
|
|
|
|
|
|
|
@property
|
|
@property
|
|
|
def visualization_dir(self) -> Path:
|
|
def visualization_dir(self) -> Path:
|
|
|
"""可视化结果目录"""
|
|
"""可视化结果目录"""
|
|
|
rel_path = self.config["paths"]["output"]["visualization"]
|
|
rel_path = self.config["paths"]["output"]["visualization"]
|
|
|
|
|
+ rel_path = self._replace_version_var(rel_path)
|
|
|
return self.account_dir / rel_path
|
|
return self.account_dir / rel_path
|
|
|
|
|
|
|
|
@property
|
|
@property
|
|
@@ -194,6 +264,10 @@ class PathConfig:
|
|
|
def print_paths(self):
|
|
def print_paths(self):
|
|
|
"""打印所有路径信息(用于调试)"""
|
|
"""打印所有路径信息(用于调试)"""
|
|
|
print("="*60)
|
|
print("="*60)
|
|
|
|
|
+ print(f"项目根目录: {self.project_root}")
|
|
|
|
|
+ print(f"项目名称: {self.project_root.name}")
|
|
|
|
|
+ print(f"数据根目录: {self.data_root}")
|
|
|
|
|
+ print(f"输出版本: {self.output_version}")
|
|
|
print(f"账号: {self.account_name}")
|
|
print(f"账号: {self.account_name}")
|
|
|
print(f"过滤模式: {self.filter_mode}")
|
|
print(f"过滤模式: {self.filter_mode}")
|
|
|
print(f"账号根目录: {self.account_dir}")
|
|
print(f"账号根目录: {self.account_dir}")
|