zhenxun_bot/zhenxun/services/plugin_init.py

108 lines
3.5 KiB
Python
Raw Normal View History

from abc import ABC, abstractmethod
from collections.abc import Callable
import nonebot
from nonebot.utils import is_coroutine_callable
from pydantic import BaseModel
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
driver = nonebot.get_driver()
class PluginInit(ABC):
"""
插件安装与卸载模块
"""
def __init_subclass__(cls, **kwargs):
module_path = cls.__module__
install_func = getattr(cls, "install", None)
remove_func = getattr(cls, "remove", None)
if install_func or remove_func:
PluginInitManager.plugins[module_path] = PluginInitData(
module_path=module_path,
install=install_func,
remove=remove_func,
class_=cls,
)
@abstractmethod
async def install(self):
raise NotImplementedError
@abstractmethod
async def remove(self):
raise NotImplementedError
class PluginInitData(BaseModel):
module_path: str
"""模块名"""
install: Callable | None
"""安装方法"""
remove: Callable | None
"""卸载方法"""
class_: type[PluginInit]
""""""
class PluginInitManager:
plugins: dict[str, PluginInitData] = {} # noqa: RUF012
@classmethod
async def install_all(cls):
"""运行所有插件安装方法"""
if not cls.plugins:
return
for module_path, model in cls.plugins.items():
if model.install:
class_ = model.class_()
try:
logger.debug(f"开始执行: {module_path}:install 方法")
if is_coroutine_callable(class_.install):
await class_.install()
else:
class_.install() # type: ignore
logger.debug(f"执行: {module_path}:install 完成")
except Exception as e:
logger.error(f"执行: {module_path}:install 失败", e=e)
@classmethod
async def install(cls, module_path: str):
"""运行指定插件安装方法"""
if model := cls.plugins.get(module_path):
if model.install:
class_ = model.class_()
try:
logger.debug(f"开始执行: {module_path}:install 方法")
if is_coroutine_callable(class_.install):
await class_.install()
else:
class_.install() # type: ignore
logger.debug(f"执行: {module_path}:install 完成")
except Exception as e:
logger.error(f"执行: {module_path}:install 失败", e=e)
@classmethod
async def remove(cls, module_path: str):
"""运行指定插件移除方法"""
if model := cls.plugins.get(module_path):
if model.remove:
class_ = model.class_()
try:
logger.debug(f"开始执行: {module_path}:remove 方法")
if is_coroutine_callable(class_.remove):
await class_.remove()
else:
class_.remove() # type: ignore
logger.debug(f"执行: {module_path}:remove 完成")
except Exception as e:
logger.error(f"执行: {module_path}:remove 失败", e=e)
: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=5)
async def _():
await PluginInitManager.install_all()