feat: 将配置导出功能从JSON改为TOML格式并添加保存对话框

This commit is contained in:
dmy
2026-03-02 22:18:32 +08:00
parent 3f3527f7af
commit 47d3b7b6b4
2 changed files with 127 additions and 13 deletions

View File

@@ -340,17 +340,86 @@ class EGMWebApp:
}
}
def export_config(self, params: Dict[str, Any]) -> str:
def dict_to_toml(self, obj: Dict[str, Any], indent: str = '') -> str:
"""
导出配置为 JSON 字符串
将字典转换为 TOML 格式字符串
Args:
obj: 参数字典
indent: 缩进字符串
Returns:
TOML 格式字符串
"""
result = ''
for key, value in obj.items():
if value is None:
continue
if isinstance(value, list):
result += f'{indent}{key} = [{", ".join(str(v) for v in value)}]\n'
elif isinstance(value, dict):
result += f'\n{indent}[{key}]\n'
result += self.dict_to_toml(value, indent)
elif isinstance(value, str):
result += f'{indent}{key} = "{value}"\n'
elif isinstance(value, bool):
result += f'{indent}{key} = {str(value).lower()}\n'
else:
result += f'{indent}{key} = {value}\n'
return result
def export_config(self, params: Dict[str, Any]) -> Dict[str, Any]:
"""
导出配置为 TOML 文件,弹出保存对话框
Args:
params: 参数字典
Returns:
JSON 字符串
包含保存状态和路径的字典
"""
return json.dumps(params, indent=2, ensure_ascii=False)
try:
# 生成默认文件名
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
default_filename = f'egm_config_{timestamp}.toml'
# 打开保存文件对话框
result = self.window.create_file_dialog(
webview.SAVE_DIALOG,
directory='',
save_filename=default_filename,
file_types=('TOML Files (*.toml)', 'All files (*.*)')
)
if result and len(result) > 0:
file_path = result[0]
# 转换为 TOML 格式
toml_content = self.dict_to_toml(params)
# 写入文件
with open(file_path, 'w', encoding='utf-8') as f:
f.write(toml_content)
return {
"success": True,
"message": f"配置已保存到: {file_path}",
"file_path": file_path
}
else:
return {
"success": False,
"message": "用户取消了保存操作"
}
except Exception as e:
logger.error(f"导出配置失败: {str(e)}")
return {
"success": False,
"message": f"保存失败: {str(e)}"
}
def get_default_config(self) -> Dict[str, Any]:
"""
@@ -421,6 +490,9 @@ def start_webview():
resizable=True,
min_size=(800, 600)
)
# 将窗口对象传递给 API
api.window = window
# 启动
logger.info("启动 EGM Web 界面...")