mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-14 13:42:56 +08:00
* ✨ 添加全局优先级hook * ✨ 添加基础配置api * ✨ 添加数据库连接测试 * 💬 提示重启 * 🩹 填充过配置时友好提示 * 🐛 首次生成简易配置后自动加载 * ✨ 添加配置后重启接口 * ✨ 添加重启标志文件 * ✨ 添加重启脚本命令 * ✨ 添加重启系统限制 * ✨ 首次配置判断是否为win系统 * 🔥 移除bat * ✨ 添加关于菜单 * ✨ 支持整合包插件安装和添加整合包文档 * 🩹 检测数据库路径 * 🩹 修改数据库路径检测 * 🩹 修改数据库路径检测 * 🩹 修复路径注入 * 🎨 显示添加优先级 * 🐛 修改PriorityLifecycle字典类名称 * ⚡ 修复路径问题 * ⚡ 修复路径检测 * ✨ 新增路径验证功能,确保用户输入的路径安全并在项目根目录内 * ✨ 优化路径验证功能,增加对非法字符和路径长度的检查,确保用户输入的路径更加安全 * 🚨 auto fix by pre-commit hooks * ✨ 优化获取文件列表的代码格式 * 📝 修改README中webui示例图 * ✨ 更新PriorityLifecycle.on_startup装饰器 * ✨ 简化安装依赖的命令构建逻辑 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from collections.abc import Callable
|
|
from typing import ClassVar
|
|
|
|
import nonebot
|
|
from nonebot.utils import is_coroutine_callable
|
|
|
|
from zhenxun.services.log import logger
|
|
from zhenxun.utils.enum import PriorityLifecycleType
|
|
from zhenxun.utils.exception import HookPriorityException
|
|
|
|
driver = nonebot.get_driver()
|
|
|
|
|
|
class PriorityLifecycle:
|
|
_data: ClassVar[dict[PriorityLifecycleType, dict[int, list[Callable]]]] = {}
|
|
|
|
@classmethod
|
|
def add(cls, hook_type: PriorityLifecycleType, func: Callable, priority: int):
|
|
if hook_type not in cls._data:
|
|
cls._data[hook_type] = {}
|
|
if priority not in cls._data[hook_type]:
|
|
cls._data[hook_type][priority] = []
|
|
cls._data[hook_type][priority].append(func)
|
|
|
|
@classmethod
|
|
def on_startup(cls, *, priority: int):
|
|
def wrapper(func):
|
|
cls.add(PriorityLifecycleType.STARTUP, func, priority)
|
|
return func
|
|
|
|
return wrapper
|
|
|
|
@classmethod
|
|
def on_shutdown(cls, *, priority: int):
|
|
def wrapper(func):
|
|
cls.add(PriorityLifecycleType.SHUTDOWN, func, priority)
|
|
return func
|
|
|
|
return wrapper
|
|
|
|
|
|
@driver.on_startup
|
|
async def _():
|
|
priority_data = PriorityLifecycle._data.get(PriorityLifecycleType.STARTUP)
|
|
if not priority_data:
|
|
return
|
|
priority_list = sorted(priority_data.keys())
|
|
priority = 0
|
|
try:
|
|
for priority in priority_list:
|
|
for func in priority_data[priority]:
|
|
if is_coroutine_callable(func):
|
|
await func()
|
|
else:
|
|
func()
|
|
except HookPriorityException as e:
|
|
logger.error(f"打断优先级 [{priority}] on_startup 方法. {type(e)}: {e}")
|