mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2024-11-30 09:14:52 +08:00
使用yaml配置文件
This commit is contained in:
parent
edc692297a
commit
170c83dbea
@ -52,16 +52,16 @@ destination_file = destination_folder / "config.yaml"
|
|||||||
|
|
||||||
|
|
||||||
def copy_config(source_template, destination_file):
|
def copy_config(source_template, destination_file):
|
||||||
'''
|
"""
|
||||||
复制模板配置文件到config
|
复制模板配置文件到config
|
||||||
'''
|
"""
|
||||||
shutil.copy(source_template, destination_file)
|
shutil.copy(source_template, destination_file)
|
||||||
|
|
||||||
|
|
||||||
def check_yaml_is_changed(source_template):
|
def check_yaml_is_changed(source_template):
|
||||||
'''
|
"""
|
||||||
检查配置文件是否需要更新
|
检查配置文件是否需要更新
|
||||||
'''
|
"""
|
||||||
with open(config_file_path, 'r', encoding="utf-8") as f:
|
with open(config_file_path, 'r', encoding="utf-8") as f:
|
||||||
old = yaml.load(f)
|
old = yaml.load(f)
|
||||||
with open(source_template, 'r', encoding="utf-8") as 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):
|
def merge_configs(old_config, new_config):
|
||||||
'''
|
"""
|
||||||
合并配置文件
|
合并配置文件
|
||||||
'''
|
"""
|
||||||
for key, value in new_config.items():
|
for key, value in new_config.items():
|
||||||
if key in old_config:
|
if key in old_config:
|
||||||
continue
|
continue
|
||||||
@ -95,19 +95,19 @@ else:
|
|||||||
logger.info("配置文件存在,正在读取")
|
logger.info("配置文件存在,正在读取")
|
||||||
|
|
||||||
if check_yaml_is_changed(source_template):
|
if check_yaml_is_changed(source_template):
|
||||||
yaml_2 = YAML()
|
yaml = YAML()
|
||||||
logger.info("插件新的配置已更新, 正在更新")
|
logger.info("插件新的配置已更新, 正在更新")
|
||||||
|
|
||||||
with open(config_file_path, 'r', encoding="utf-8") as f:
|
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:
|
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)
|
merged_config = merge_configs(old_config, new_config)
|
||||||
|
|
||||||
with open(destination_file, 'w', encoding="utf-8") as f:
|
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:
|
with open(config_file_path, "r", encoding="utf-8") as f:
|
||||||
yaml_config = yaml_.load(f, Loader=yaml_.FullLoader)
|
yaml_config = yaml_.load(f, Loader=yaml_.FullLoader)
|
||||||
|
@ -13,6 +13,8 @@ dependencies = [
|
|||||||
"zhDatetime>=1.1.1",
|
"zhDatetime>=1.1.1",
|
||||||
"aiohttp>=3.9",
|
"aiohttp>=3.9",
|
||||||
"httpx>=0.27.0",
|
"httpx>=0.27.0",
|
||||||
|
"ruamel.yaml>=0.18.6"
|
||||||
|
|
||||||
]
|
]
|
||||||
license = { text = "MIT" }
|
license = { text = "MIT" }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user