2024-02-04 04:18:54 +08:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import nonebot
|
2024-10-01 00:05:43 +08:00
|
|
|
from nonebot import get_loaded_plugins
|
2024-12-10 19:49:11 +08:00
|
|
|
from nonebot.drivers import Driver
|
|
|
|
|
from nonebot.plugin import Plugin
|
|
|
|
|
from ruamel.yaml import YAML
|
2024-02-04 04:18:54 +08:00
|
|
|
from ruamel.yaml.comments import CommentedMap
|
|
|
|
|
|
|
|
|
|
from zhenxun.configs.config import Config
|
2024-10-01 00:05:43 +08:00
|
|
|
from zhenxun.configs.path_config import DATA_PATH
|
2024-12-10 19:49:11 +08:00
|
|
|
from zhenxun.configs.utils import RegisterConfig
|
|
|
|
|
from zhenxun.services.log import logger
|
2025-06-16 09:11:41 +08:00
|
|
|
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
_yaml = YAML(pure=True)
|
|
|
|
|
_yaml.allow_unicode = True
|
|
|
|
|
_yaml.indent = 2
|
|
|
|
|
|
|
|
|
|
driver: Driver = nonebot.get_driver()
|
|
|
|
|
|
|
|
|
|
SIMPLE_CONFIG_FILE = DATA_PATH / "config.yaml"
|
|
|
|
|
|
|
|
|
|
old_config_file = Path() / "zhenxun" / "configs" / "config.yaml"
|
|
|
|
|
if old_config_file.exists():
|
|
|
|
|
old_config_file.rename(SIMPLE_CONFIG_FILE)
|
|
|
|
|
|
|
|
|
|
|
2025-01-06 11:32:56 +08:00
|
|
|
def _handle_config(plugin: Plugin, exists_module: list[str]):
|
2024-02-04 04:18:54 +08:00
|
|
|
"""处理配置项
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
plugin: Plugin
|
|
|
|
|
"""
|
|
|
|
|
if plugin.metadata and plugin.metadata.extra:
|
|
|
|
|
extra = plugin.metadata.extra
|
|
|
|
|
if configs := extra.get("configs"):
|
|
|
|
|
for config in configs:
|
|
|
|
|
reg_config = RegisterConfig(**config)
|
|
|
|
|
module = reg_config.module or plugin.name
|
|
|
|
|
g_config = Config.get(module)
|
|
|
|
|
g_config.name = plugin.metadata.name
|
|
|
|
|
Config.add_plugin_config(
|
|
|
|
|
module,
|
|
|
|
|
reg_config.key,
|
|
|
|
|
reg_config.value,
|
|
|
|
|
help=reg_config.help,
|
|
|
|
|
default_value=reg_config.default_value,
|
|
|
|
|
type=reg_config.type,
|
|
|
|
|
arg_parser=reg_config.arg_parser,
|
|
|
|
|
_override=False,
|
|
|
|
|
)
|
2025-01-07 16:41:42 +08:00
|
|
|
exists_module.append(f"{module}:{reg_config.key}".lower())
|
2024-02-04 04:18:54 +08:00
|
|
|
|
|
|
|
|
|
2025-01-06 11:32:56 +08:00
|
|
|
def _generate_simple_config(exists_module: list[str]):
|
2024-02-04 04:18:54 +08:00
|
|
|
"""
|
|
|
|
|
生成简易配置
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
AttributeError: _description_
|
|
|
|
|
"""
|
|
|
|
|
# 读取用户配置
|
|
|
|
|
_data = {}
|
|
|
|
|
_tmp_data = {}
|
2025-01-16 09:13:28 +08:00
|
|
|
exists_module += Config.add_module
|
2024-02-04 04:18:54 +08:00
|
|
|
if SIMPLE_CONFIG_FILE.exists():
|
|
|
|
|
_data = _yaml.load(SIMPLE_CONFIG_FILE.open(encoding="utf8"))
|
|
|
|
|
# 将简易配置文件的数据填充到配置文件
|
|
|
|
|
for module in Config.keys():
|
|
|
|
|
_tmp_data[module] = {}
|
|
|
|
|
for k in Config[module].configs.keys():
|
|
|
|
|
try:
|
|
|
|
|
if _data.get(module) and k in _data[module].keys():
|
|
|
|
|
Config.set_config(module, k, _data[module][k])
|
2025-01-07 16:41:42 +08:00
|
|
|
if f"{module}:{k}".lower() in exists_module:
|
2025-01-06 11:32:56 +08:00
|
|
|
_tmp_data[module][k] = Config.get_config(module, k)
|
2024-02-04 04:18:54 +08:00
|
|
|
except AttributeError as e:
|
2024-10-01 00:05:43 +08:00
|
|
|
raise AttributeError(f"{e}\n可能为config.yaml配置文件填写不规范") from e
|
2025-01-06 11:32:56 +08:00
|
|
|
if not _tmp_data[module]:
|
|
|
|
|
_tmp_data.pop(module)
|
2024-02-04 04:18:54 +08:00
|
|
|
Config.save()
|
|
|
|
|
temp_file = DATA_PATH / "temp_config.yaml"
|
|
|
|
|
# 重新生成简易配置文件
|
|
|
|
|
try:
|
|
|
|
|
with open(temp_file, "w", encoding="utf8") as wf:
|
|
|
|
|
_yaml.dump(_tmp_data, wf)
|
2024-10-01 00:05:43 +08:00
|
|
|
with open(temp_file, encoding="utf8") as rf:
|
2024-02-04 04:18:54 +08:00
|
|
|
_data = _yaml.load(rf)
|
|
|
|
|
# 添加注释
|
|
|
|
|
for module in _data.keys():
|
|
|
|
|
help_text = ""
|
|
|
|
|
plugin_name = Config.get(module).name or module
|
|
|
|
|
help_text += plugin_name + "\n"
|
|
|
|
|
for k in _data[module].keys():
|
|
|
|
|
help_text += f"{k}: {Config[module].configs[k].help}" + "\n"
|
|
|
|
|
_data.yaml_set_comment_before_after_key(after=help_text[:-1], key=module)
|
|
|
|
|
with SIMPLE_CONFIG_FILE.open("w", encoding="utf8") as wf:
|
|
|
|
|
_yaml.dump(_data, wf)
|
|
|
|
|
except Exception as e:
|
2024-10-01 00:05:43 +08:00
|
|
|
logger.error("生成简易配置注释错误...", e=e)
|
2024-02-04 04:18:54 +08:00
|
|
|
if temp_file.exists():
|
|
|
|
|
temp_file.unlink()
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 09:11:41 +08:00
|
|
|
@PriorityLifecycle.on_startup(priority=0)
|
2024-02-04 04:18:54 +08:00
|
|
|
def _():
|
|
|
|
|
"""
|
|
|
|
|
初始化插件数据配置
|
|
|
|
|
"""
|
|
|
|
|
plugins2config_file = DATA_PATH / "configs" / "plugins2config.yaml"
|
2025-01-06 11:32:56 +08:00
|
|
|
exists_module = []
|
2024-02-04 04:18:54 +08:00
|
|
|
for plugin in get_loaded_plugins():
|
|
|
|
|
if plugin.metadata:
|
2025-01-06 11:32:56 +08:00
|
|
|
_handle_config(plugin, exists_module)
|
2024-02-04 04:18:54 +08:00
|
|
|
if not Config.is_empty():
|
|
|
|
|
Config.save()
|
|
|
|
|
_data: CommentedMap = _yaml.load(plugins2config_file.open(encoding="utf8"))
|
|
|
|
|
for module in _data.keys():
|
|
|
|
|
plugin_name = Config.get(module).name
|
|
|
|
|
_data.yaml_set_comment_before_after_key(
|
|
|
|
|
after=f"{plugin_name}",
|
|
|
|
|
key=module,
|
|
|
|
|
)
|
|
|
|
|
# 存完插件基本设置
|
|
|
|
|
with plugins2config_file.open("w", encoding="utf8") as wf:
|
|
|
|
|
_yaml.dump(_data, wf)
|
2025-01-06 11:32:56 +08:00
|
|
|
_generate_simple_config(exists_module)
|
2025-06-16 09:11:41 +08:00
|
|
|
Config.reload()
|