refactor(redis_manager): 移除冗余的ConnectionError处理 refactor(event_handler): 优化Bot类型注解 refactor(factory): 移除未使用的GroupCardNoticeEvent test: 添加全面的单元测试覆盖 - 添加test_import.py测试模块导入 - 添加test_debug.py测试插件加载调试 - 添加test_plugin_error.py测试错误处理 - 添加test_config_loader.py测试配置加载 - 添加test_redis_manager.py测试Redis管理 - 添加test_bot.py测试Bot功能 - 扩展test_models.py测试消息模型 - 添加test_plugin_manager_coverage.py测试插件管理 - 添加test_executor.py测试代码执行器 - 添加test_ws.py测试WebSocket - 添加test_api.py测试API接口 - 添加test_core_managers.py测试核心管理模块 fix(plugin_manager): 修复插件加载日志变量问题 覆盖率已到达86%(忽略插件)
102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
"""
|
|
插件管理器模块
|
|
|
|
负责扫描、加载和管理 `plugins` 目录下的所有插件。
|
|
"""
|
|
import importlib
|
|
import os
|
|
import pkgutil
|
|
import sys
|
|
from typing import Set
|
|
|
|
from ..utils.exceptions import SyncHandlerError
|
|
from ..utils.logger import logger
|
|
|
|
# 确保logger在模块级别可见
|
|
__all__ = ['PluginManager', 'logger']
|
|
|
|
|
|
class PluginManager:
|
|
"""
|
|
插件管理器类
|
|
"""
|
|
def __init__(self, command_manager):
|
|
"""
|
|
初始化插件管理器
|
|
|
|
:param command_manager: CommandManager的实例
|
|
"""
|
|
self.command_manager = command_manager
|
|
self.loaded_plugins: Set[str] = set()
|
|
|
|
def load_all_plugins(self):
|
|
"""
|
|
扫描并加载 `plugins` 目录下的所有插件。
|
|
"""
|
|
# 使用 pathlib 获取更可靠的路径
|
|
# 当前文件: core/managers/plugin_manager.py
|
|
# 目标: plugins/
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
# 回退两级到项目根目录 (core/managers -> core -> root)
|
|
root_dir = os.path.dirname(os.path.dirname(current_dir))
|
|
plugin_dir = os.path.join(root_dir, "plugins")
|
|
|
|
package_name = "plugins"
|
|
|
|
if not os.path.exists(plugin_dir):
|
|
logger.error(f"插件目录不存在: {plugin_dir}")
|
|
return
|
|
|
|
logger.info(f"正在从 {package_name} 加载插件 (路径: {plugin_dir})...")
|
|
|
|
for _, module_name, is_pkg in pkgutil.iter_modules([plugin_dir]):
|
|
full_module_name = f"{package_name}.{module_name}"
|
|
|
|
action = "加载" # 初始化默认值
|
|
try:
|
|
if full_module_name in self.loaded_plugins:
|
|
self.command_manager.unload_plugin(full_module_name)
|
|
module = importlib.reload(sys.modules[full_module_name])
|
|
action = "重载"
|
|
else:
|
|
module = importlib.import_module(full_module_name)
|
|
action = "加载"
|
|
|
|
if hasattr(module, "__plugin_meta__"):
|
|
meta = getattr(module, "__plugin_meta__")
|
|
self.command_manager.plugins[full_module_name] = meta
|
|
|
|
self.loaded_plugins.add(full_module_name)
|
|
|
|
type_str = "包" if is_pkg else "文件"
|
|
logger.success(f" [{type_str}] 成功{action}: {module_name}")
|
|
except SyncHandlerError as e:
|
|
logger.error(f" 插件 {module_name} 加载失败: {e} (跳过此插件)")
|
|
except Exception as e:
|
|
logger.exception(
|
|
f" 加载插件 {module_name} 失败: {e}"
|
|
)
|
|
|
|
def reload_plugin(self, full_module_name: str):
|
|
"""
|
|
精确重载单个插件。
|
|
"""
|
|
if full_module_name not in self.loaded_plugins:
|
|
logger.warning(f"尝试重载一个未被加载的插件: {full_module_name},将按首次加载处理。")
|
|
|
|
if full_module_name not in sys.modules:
|
|
logger.error(f"重载失败: 模块 {full_module_name} 未在 sys.modules 中找到。")
|
|
return
|
|
|
|
try:
|
|
self.command_manager.unload_plugin(full_module_name)
|
|
module = importlib.reload(sys.modules[full_module_name])
|
|
|
|
if hasattr(module, "__plugin_meta__"):
|
|
meta = getattr(module, "__plugin_meta__")
|
|
self.command_manager.plugins[full_module_name] = meta
|
|
|
|
logger.success(f"插件 {full_module_name} 已成功重载。")
|
|
except Exception as e:
|
|
logger.exception(f"重载插件 {full_module_name} 时发生错误: {e}")
|