Skip to content

Commit 535d4a1

Browse files
rm subscript
1 parent db7b503 commit 535d4a1

3 files changed

Lines changed: 138 additions & 151 deletions

File tree

‎src/config/set_config.py‎

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,110 @@
55
每个脚本的 config 格式、路径、字段名都不同,
66
各脚本子类单独适配,上层无需关心差异。
77
8-
config 路径推导由 subscript_utils 统一处理:
8+
config 路径推导:
99
1. 从 config.yml 中取得各脚本的 script_path(exe 路径),取其父目录作为脚本根目录
1010
2. _CONFIG_REL_PATHS 标注了各 config 相对于脚本根目录的路径
1111
3. 拼接根目录 + 相对路径 = config 文件绝对路径
1212
"""
1313

1414
import os
1515
import json
16+
import yaml
17+
from typing import Any
1618

17-
from src.subscript_utils import load_config, save_config
18-
from src.utils import get_our_bgi_user_dir
19+
from src.utils import get_config_yml_path_under_root, get_our_bgi_user_dir
20+
21+
22+
# ============================================================
23+
# 各脚本 config 相对路径映射
24+
# ============================================================
25+
26+
_CONFIG_REL_PATHS: dict[str, str] = {
27+
"鸣潮": "data/apps/ok-ww/working/configs/DailyTask.json",
28+
"原神": "User/OneDragon/默认配置.json",
29+
"终末地": "data/apps/ok-ef/working/configs/DailyTask.json",
30+
"绝区零": "config/01/one_dragon/charge_plan.yml",
31+
"崩铁": "config.yaml",
32+
"异环": "data/apps/ok-nte/working/configs/DailyTask.json",
33+
"粥": "config/gui.new.json",
34+
}
35+
36+
37+
def _load_config_yml() -> dict:
38+
"""读取主配置 config.yml"""
39+
with open(get_config_yml_path_under_root(), 'r', encoding='utf-8') as f:
40+
return yaml.safe_load(f)
41+
42+
43+
# ============================================================
44+
# 脚本根目录 & config 路径解析
45+
# ============================================================
46+
47+
def _get_script_root_dir(script_display_name: str) -> str:
48+
"""
49+
从 config.yml 中找到指定脚本的 script_path,
50+
取其父目录作为脚本项目根目录。
51+
52+
script_path 可能是 Windows 风格路径(C:\\...\\xxx.exe),
53+
统一将反斜杠转为正斜杠后再取 dirname,确保跨平台可用。
54+
"""
55+
config_data = _load_config_yml()
56+
for script in config_data.get('script_list', []):
57+
if script.get('display_name') == script_display_name:
58+
script_path = script.get('script_path', '')
59+
assert script_path, f"[set_config] config.yml 中 {script_display_name} 的 script_path 为空"
60+
normalized = script_path.replace('\\', '/')
61+
return os.path.dirname(normalized)
62+
assert False, f"[set_config] config.yml 中找不到脚本: {script_display_name}"
63+
64+
65+
def get_config_path(script_display_name: str) -> str:
66+
"""
67+
获取指定脚本的 config 文件绝对路径。
68+
拼接脚本根目录 + config 相对路径。
69+
"""
70+
assert script_display_name in _CONFIG_REL_PATHS, \
71+
f"[set_config] 未适配脚本: {script_display_name}"
72+
root = _get_script_root_dir(script_display_name)
73+
rel = _CONFIG_REL_PATHS[script_display_name]
74+
config_path = os.path.join(root, rel)
75+
assert os.path.exists(config_path), f"[set_config] config 文件不存在: {config_path}"
76+
return config_path
77+
78+
79+
# ============================================================
80+
# config 读写
81+
# ============================================================
82+
83+
def load_config(script_display_name: str) -> Any:
84+
"""
85+
读取指定脚本的 config 文件,返回解析后的 dict/list。
86+
支持 .json 和 .yaml/.yml 格式。
87+
"""
88+
path = get_config_path(script_display_name)
89+
ext = os.path.splitext(path)[1].lower()
90+
with open(path, 'r', encoding='utf-8') as f:
91+
if ext == '.json':
92+
return json.load(f)
93+
elif ext in ('.yaml', '.yml'):
94+
return yaml.safe_load(f)
95+
raise ValueError(f"[set_config] 不支持的 config 格式: {ext}")
96+
97+
98+
def save_config(script_display_name: str, data: Any) -> None:
99+
"""
100+
将数据写回指定脚本的 config 文件。
101+
保持原始格式(json / yaml)。
102+
"""
103+
path = get_config_path(script_display_name)
104+
ext = os.path.splitext(path)[1].lower()
105+
with open(path, 'w', encoding='utf-8') as f:
106+
if ext == '.json':
107+
json.dump(data, f, ensure_ascii=False, indent=4)
108+
elif ext in ('.yaml', '.yml'):
109+
yaml.dump(data, f, allow_unicode=True, sort_keys=False)
110+
else:
111+
raise ValueError(f"[set_config] 不支持的 config 格式: {ext}")
19112

20113

21114
# ============================================================

‎src/subscript_utils.py‎

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)