From d7932595e86635a7d946d71a3d2452dc40e03d11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E6=A2=93=E6=9F=92?= <1787882683@qq.com> Date: Sat, 29 Nov 2025 14:24:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=20tomlkit=20?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=20toml=EF=BC=8C=E5=A2=9E=E5=BC=BA=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E7=9A=84=E8=AF=BB=E5=8F=96=E5=92=8C?= =?UTF-8?q?=E5=86=99=E5=85=A5=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E5=92=8C=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webui/config_routes.py | 4 ++-- src/webui/plugin_routes.py | 41 ++++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/webui/config_routes.py b/src/webui/config_routes.py index c4fca2d0..392f2d98 100644 --- a/src/webui/config_routes.py +++ b/src/webui/config_routes.py @@ -586,8 +586,8 @@ async def save_adapter_config(data: dict[str, str] = Body(...)): # 验证 TOML 格式 try: - import toml - toml.loads(content) + import tomlkit + tomlkit.loads(content) except Exception as e: raise HTTPException(status_code=400, detail=f"TOML 格式错误: {str(e)}") diff --git a/src/webui/plugin_routes.py b/src/webui/plugin_routes.py index 5c49483e..7480d65f 100644 --- a/src/webui/plugin_routes.py +++ b/src/webui/plugin_routes.py @@ -1235,8 +1235,9 @@ async def get_plugin_config_schema( config_path = plugin_path / "config.toml" current_config = {} if config_path.exists(): - import toml - current_config = toml.load(config_path) + import tomlkit + with open(config_path, "r", encoding="utf-8") as f: + current_config = tomlkit.load(f) # 构建基础 schema(无法获取完整的 ConfigField 信息) schema = { @@ -1342,10 +1343,11 @@ async def get_plugin_config( if not config_path.exists(): return {"success": True, "config": {}, "message": "配置文件不存在"} - import toml - config = toml.load(config_path) + import tomlkit + with open(config_path, "r", encoding="utf-8") as f: + config = tomlkit.load(f) - return {"success": True, "config": config} + return {"success": True, "config": dict(config)} except HTTPException: raise @@ -1405,10 +1407,18 @@ async def update_plugin_config( shutil.copy(config_path, backup_path) logger.info(f"已备份配置文件: {backup_path}") - # 写入新配置 - import toml + # 写入新配置(使用 tomlkit 保留注释) + import tomlkit + # 先读取原配置以保留注释和格式 + existing_doc = tomlkit.document() + if config_path.exists(): + with open(config_path, "r", encoding="utf-8") as f: + existing_doc = tomlkit.load(f) + # 更新值 + for key, value in request.config.items(): + existing_doc[key] = value with open(config_path, "w", encoding="utf-8") as f: - toml.dump(request.config, f) + tomlkit.dump(existing_doc, f) logger.info(f"已更新插件配置: {plugin_id}") @@ -1530,24 +1540,25 @@ async def toggle_plugin( config_path = plugin_path / "config.toml" - import toml + import tomlkit - # 读取当前配置 - config = {} + # 读取当前配置(保留注释和格式) + config = tomlkit.document() if config_path.exists(): - config = toml.load(config_path) + with open(config_path, "r", encoding="utf-8") as f: + config = tomlkit.load(f) # 切换 enabled 状态 if "plugin" not in config: - config["plugin"] = {} + config["plugin"] = tomlkit.table() current_enabled = config["plugin"].get("enabled", True) new_enabled = not current_enabled config["plugin"]["enabled"] = new_enabled - # 写入配置 + # 写入配置(保留注释) with open(config_path, "w", encoding="utf-8") as f: - toml.dump(config, f) + tomlkit.dump(config, f) status = "启用" if new_enabled else "禁用" logger.info(f"已{status}插件: {plugin_id}")