使用yaml配置文件

This commit is contained in:
DiaoDaiaChan 2024-11-24 09:43:11 +08:00
parent edc692297a
commit 170c83dbea
2 changed files with 12 additions and 10 deletions

View File

@ -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)

View File

@ -13,6 +13,8 @@ dependencies = [
"zhDatetime>=1.1.1",
"aiohttp>=3.9",
"httpx>=0.27.0",
"ruamel.yaml>=0.18.6"
]
license = { text = "MIT" }