zhenxun_bot/zhenxun/builtin_plugins/init/init_config.py

130 lines
4.5 KiB
Python
Raw Normal View History

2024-02-04 04:18:54 +08:00
from pathlib import Path
import nonebot
from nonebot import get_loaded_plugins
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
from zhenxun.configs.path_config import DATA_PATH
from zhenxun.configs.utils import RegisterConfig
from zhenxun.services.log import logger
:sparkles: 首次启动时提供使用web ui方式完全配置 (#1870) * :sparkles: 添加全局优先级hook * :sparkles: 添加基础配置api * :sparkles: 添加数据库连接测试 * :speech_balloon: 提示重启 * :adhesive_bandage: 填充过配置时友好提示 * :bug: 首次生成简易配置后自动加载 * :sparkles: 添加配置后重启接口 * :sparkles: 添加重启标志文件 * :sparkles: 添加重启脚本命令 * :sparkles: 添加重启系统限制 * :sparkles: 首次配置判断是否为win系统 * :fire: 移除bat * :sparkles: 添加关于菜单 * :sparkles: 支持整合包插件安装和添加整合包文档 * :adhesive_bandage: 检测数据库路径 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修复路径注入 * :art: 显示添加优先级 * :bug: 修改PriorityLifecycle字典类名称 * :zap: 修复路径问题 * :zap: 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * :rotating_light: auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * :memo: 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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)
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,
)
exists_module.append(f"{module}:{reg_config.key}".lower())
2024-02-04 04:18:54 +08:00
def _generate_simple_config(exists_module: list[str]):
2024-02-04 04:18:54 +08:00
"""
生成简易配置
异常:
AttributeError: _description_
"""
# 读取用户配置
_data = {}
_tmp_data = {}
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])
if f"{module}:{k}".lower() in exists_module:
_tmp_data[module][k] = Config.get_config(module, k)
2024-02-04 04:18:54 +08:00
except AttributeError as e:
raise AttributeError(f"{e}\n可能为config.yaml配置文件填写不规范") from e
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)
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:
logger.error("生成简易配置注释错误...", e=e)
2024-02-04 04:18:54 +08:00
if temp_file.exists():
temp_file.unlink()
:sparkles: 首次启动时提供使用web ui方式完全配置 (#1870) * :sparkles: 添加全局优先级hook * :sparkles: 添加基础配置api * :sparkles: 添加数据库连接测试 * :speech_balloon: 提示重启 * :adhesive_bandage: 填充过配置时友好提示 * :bug: 首次生成简易配置后自动加载 * :sparkles: 添加配置后重启接口 * :sparkles: 添加重启标志文件 * :sparkles: 添加重启脚本命令 * :sparkles: 添加重启系统限制 * :sparkles: 首次配置判断是否为win系统 * :fire: 移除bat * :sparkles: 添加关于菜单 * :sparkles: 支持整合包插件安装和添加整合包文档 * :adhesive_bandage: 检测数据库路径 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修复路径注入 * :art: 显示添加优先级 * :bug: 修改PriorityLifecycle字典类名称 * :zap: 修复路径问题 * :zap: 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * :rotating_light: auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * :memo: 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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"
exists_module = []
2024-02-04 04:18:54 +08:00
for plugin in get_loaded_plugins():
if plugin.metadata:
_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)
_generate_simple_config(exists_module)
:sparkles: 首次启动时提供使用web ui方式完全配置 (#1870) * :sparkles: 添加全局优先级hook * :sparkles: 添加基础配置api * :sparkles: 添加数据库连接测试 * :speech_balloon: 提示重启 * :adhesive_bandage: 填充过配置时友好提示 * :bug: 首次生成简易配置后自动加载 * :sparkles: 添加配置后重启接口 * :sparkles: 添加重启标志文件 * :sparkles: 添加重启脚本命令 * :sparkles: 添加重启系统限制 * :sparkles: 首次配置判断是否为win系统 * :fire: 移除bat * :sparkles: 添加关于菜单 * :sparkles: 支持整合包插件安装和添加整合包文档 * :adhesive_bandage: 检测数据库路径 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修改数据库路径检测 * :adhesive_bandage: 修复路径注入 * :art: 显示添加优先级 * :bug: 修改PriorityLifecycle字典类名称 * :zap: 修复路径问题 * :zap: 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * :rotating_light: auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * :memo: 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * :rotating_light: auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-06-16 09:11:41 +08:00
Config.reload()