From 2c88a36f323df8b8d74e51e16d45b3026504ef85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AC=A3=E5=8D=97=E7=A7=91=E6=8A=80?= Date: Fri, 28 Feb 2025 19:17:36 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=90=AF=E5=8A=A8=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=20(#155)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: hrz <1710360675@qq.com> --- app.py | 9 ++++--- config/settings.py | 65 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index f2288840..bc53ba6b 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ import asyncio from config.logger import setup_logging -from config.settings import load_config +from config.settings import load_config, check_config_file from core.websocket_server import WebSocketServer from manager.http_server import WebUI from aiohttp import web @@ -8,10 +8,12 @@ from core.utils.util import get_local_ip TAG = __name__ + async def main(): + check_config_file() logger = setup_logging() config = load_config() - + # 启动 WebSocket 服务器 ws_server = WebSocketServer(config) ws_task = asyncio.create_task(ws_server.start()) @@ -33,7 +35,7 @@ async def main(): logger.bind(tag=TAG).info(f"WebUI server is running at http://{local_ip}:{port}") except Exception as e: logger.bind(tag=TAG).error(f"Failed to start WebUI server: {e}") - + try: # 等待 WebSocket 服务器运行 await ws_task @@ -42,5 +44,6 @@ async def main(): if webui_runner: await webui_runner.cleanup() + if __name__ == "__main__": asyncio.run(main()) diff --git a/config/settings.py b/config/settings.py index a1e23ab7..0582bf76 100644 --- a/config/settings.py +++ b/config/settings.py @@ -1,22 +1,26 @@ import os import argparse from ruamel.yaml import YAML +from collections.abc import Mapping from core.utils.util import read_config, get_project_dir +default_config_file = "config.yaml" + def get_config_file(): - default_config_file = "config.yaml" + global default_config_file # 判断是否存在私有的配置文件 + config_file = default_config_file if os.path.exists(get_project_dir() + "data/." + default_config_file): - default_config_file = "data/." + default_config_file - return default_config_file + config_file = "data/." + default_config_file + return config_file def load_config(): """加载配置文件""" parser = argparse.ArgumentParser(description="Server configuration") - default_config_file = get_config_file() - parser.add_argument("--config_path", type=str, default=default_config_file) + config_file = get_config_file() + parser.add_argument("--config_path", type=str, default=config_file) args = parser.parse_args() return read_config(args.config_path) @@ -27,3 +31,54 @@ def update_config(config): """将配置保存到YAML文件""" with open(get_config_file(), 'w') as f: yaml.dump(config, f) + + +def find_missing_keys(new_config, old_config, parent_key=''): + """ + 递归查找缺失的配置项 + 返回格式:[缺失配置路径] + """ + missing_keys = [] + + if not isinstance(new_config, Mapping): + return missing_keys + + for key, value in new_config.items(): + # 构建当前配置路径 + full_path = f"{parent_key}.{key}" if parent_key else key + + # 检查键是否存在 + if key not in old_config: + missing_keys.append(full_path) + continue + + # 递归检查嵌套字典 + if isinstance(value, Mapping): + sub_missing = find_missing_keys( + value, + old_config[key], + parent_key=full_path + ) + missing_keys.extend(sub_missing) + + return missing_keys + + +def check_config_file(): + old_config_file = get_config_file() + global default_config_file + if not old_config_file.startswith('data'): + return + old_config = read_config(get_project_dir() + old_config_file) + new_config = read_config(get_project_dir() + default_config_file) + # 查找缺失的配置项 + missing_keys = find_missing_keys(new_config, old_config) + + if missing_keys: + error_msg = "您的配置文件太旧了,缺少了:\n" + error_msg += "\n".join(f"- {key}" for key in missing_keys) + error_msg += "\n建议您:\n" + error_msg += "1、备份data/.config.yaml文件\n" + error_msg += "2、将根目录的config.yaml文件复制到data下,重命名为.config.yaml\n" + error_msg += "3、将密钥逐个复制到新的配置文件中\n" + raise ValueError(error_msg)