From 170c83dbea0f6936ae4e72cea069f8b77d115a43 Mon Sep 17 00:00:00 2001 From: DiaoDaiaChan Date: Sun, 24 Nov 2024 09:43:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8yaml=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/config.py | 20 ++++++++++---------- pyproject.toml | 2 ++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/nonebot_plugin_marshoai/config.py b/nonebot_plugin_marshoai/config.py index bf378d3..9392d52 100644 --- a/nonebot_plugin_marshoai/config.py +++ b/nonebot_plugin_marshoai/config.py @@ -52,16 +52,16 @@ destination_file = destination_folder / "config.yaml" def copy_config(source_template, destination_file): - ''' + """ 复制模板配置文件到config - ''' + """ shutil.copy(source_template, destination_file) def check_yaml_is_changed(source_template): - ''' + """ 检查配置文件是否需要更新 - ''' + """ with open(config_file_path, 'r', encoding="utf-8") as f: old = yaml.load(f) with open(source_template, 'r', encoding="utf-8") as f: @@ -75,9 +75,9 @@ def check_yaml_is_changed(source_template): def merge_configs(old_config, new_config): - ''' + """ 合并配置文件 - ''' + """ for key, value in new_config.items(): if key in old_config: continue @@ -95,19 +95,19 @@ else: logger.info("配置文件存在,正在读取") if check_yaml_is_changed(source_template): - yaml_2 = YAML() + yaml = YAML() logger.info("插件新的配置已更新, 正在更新") with open(config_file_path, 'r', encoding="utf-8") as f: - old_config = yaml_2.load(f) + old_config = yaml.load(f) with open(source_template, 'r', encoding="utf-8") as f: - new_config = yaml_2.load(f) + new_config = yaml.load(f) merged_config = merge_configs(old_config, new_config) with open(destination_file, 'w', encoding="utf-8") as f: - yaml_2.dump(merged_config, f) + yaml.dump(merged_config, f) with open(config_file_path, "r", encoding="utf-8") as f: yaml_config = yaml_.load(f, Loader=yaml_.FullLoader) diff --git a/pyproject.toml b/pyproject.toml index cd99c90..9da2407 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,8 @@ dependencies = [ "zhDatetime>=1.1.1", "aiohttp>=3.9", "httpx>=0.27.0", + "ruamel.yaml>=0.18.6" + ] license = { text = "MIT" }