集成help以及init到core内,修改baseplugin为plugin
This commit is contained in:
48
core/plugin_manager.py
Normal file
48
core/plugin_manager.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
插件管理器模块
|
||||
|
||||
负责扫描、加载和管理 `base_plugins` 目录下的所有插件。
|
||||
"""
|
||||
import importlib
|
||||
import os
|
||||
import pkgutil
|
||||
import sys
|
||||
|
||||
from core.command_manager import matcher
|
||||
|
||||
|
||||
def load_all_plugins():
|
||||
"""
|
||||
扫描并加载 `plugins` 目录下的所有插件。
|
||||
|
||||
该函数会遍历 `plugins` 目录下的所有模块:
|
||||
1. 如果模块已加载,则执行 reload 操作(用于热重载)。
|
||||
2. 如果模块未加载,则执行 import 操作。
|
||||
|
||||
加载过程中会提取插件元数据 `__plugin_meta__` 并注册到 CommandManager。
|
||||
"""
|
||||
plugin_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "plugins")
|
||||
package_name = "plugins"
|
||||
|
||||
print(f" 正在从 {package_name} 加载插件...")
|
||||
|
||||
for loader, module_name, is_pkg in pkgutil.iter_modules([plugin_dir]):
|
||||
full_module_name = f"{package_name}.{module_name}"
|
||||
|
||||
try:
|
||||
if full_module_name in sys.modules:
|
||||
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__")
|
||||
matcher.plugins[full_module_name] = meta
|
||||
|
||||
type_str = "包" if is_pkg else "文件"
|
||||
print(f" [{type_str}] 成功{action}: {module_name}")
|
||||
except Exception as e:
|
||||
print(f" {action if 'action' in locals() else '加载'}插件 {module_name} 失败: {e}")
|
||||
Reference in New Issue
Block a user