11import asyncio
22import importlib
33import inspect
4- import logging
54import os
65import sys
76from typing import TYPE_CHECKING , Dict , Any , Optional , Type
109if TYPE_CHECKING :
1110 from .vup_next_core import VupNextCore
1211
13- logger = logging .getLogger (__name__ )
12+ from src .utils .logger import logger
13+
1414
1515# --- 插件基类 (可选但推荐) ---
1616class BasePlugin :
1717 """所有插件的基础类,定义插件的基本接口。"""
18+
1819 # 添加一个类级别的标记属性
1920 _is_vup_next_plugin : bool = True
2021
21- def __init__ (self , core : ' VupNextCore' , plugin_config : Dict [str , Any ]):
22+ def __init__ (self , core : " VupNextCore" , plugin_config : Dict [str , Any ]):
2223 """
2324 初始化插件。
2425
@@ -28,7 +29,7 @@ def __init__(self, core: 'VupNextCore', plugin_config: Dict[str, Any]):
2829 """
2930 self .core = core
3031 self .plugin_config = plugin_config
31- self .logger = logging . getLogger ( f"Plugin. { self . __class__ . __name__ } " )
32+ self .logger = logger
3233 self .logger .info (f"初始化插件: { self .__class__ .__name__ } " )
3334
3435 async def setup (self ):
@@ -45,9 +46,11 @@ async def cleanup(self):
4546 # 子类应在此处实现清理逻辑
4647 pass
4748
49+
4850class PluginManager :
4951 """负责加载、管理和卸载插件。"""
50- def __init__ (self , core : 'VupNextCore' , global_plugin_config : Dict [str , Any ]):
52+
53+ def __init__ (self , core : "VupNextCore" , global_plugin_config : Dict [str , Any ]):
5154 """
5255 初始化插件管理器。
5356
@@ -59,7 +62,7 @@ def __init__(self, core: 'VupNextCore', global_plugin_config: Dict[str, Any]):
5962 self .global_plugin_config = global_plugin_config
6063 self .loaded_plugins : Dict [str , BasePlugin ] = {}
6164 # 初始化 PluginManager 自己的 logger
62- self .logger = logging . getLogger ( __name__ )
65+ self .logger = logger
6366 self .logger .debug ("PluginManager 初始化完成" )
6467
6568 async def load_plugins (self , plugin_dir : str = "src/plugins" ):
@@ -86,8 +89,8 @@ async def load_plugins(self, plugin_dir: str = "src/plugins"):
8689 self .logger .debug (f"检测到潜在插件目录: { plugin_name } " )
8790
8891 if not os .path .exists (plugin_module_file ):
89- self .logger .warning (f"在插件目录 '{ plugin_name } ' 中未找到主文件 'plugin.py',跳过。" )
90- continue
92+ self .logger .warning (f"在插件目录 '{ plugin_name } ' 中未找到主文件 'plugin.py',跳过。" )
93+ continue
9194
9295 # --- 检查插件是否在配置中启用 ---
9396 is_enabled_key = f"enable_{ plugin_name } "
@@ -99,7 +102,7 @@ async def load_plugins(self, plugin_dir: str = "src/plugins"):
99102 continue
100103
101104 # --- 加载插件模块 ---
102- module = None # 初始化为 None
105+ module = None # 初始化为 None
103106 try :
104107 module_import_path = f"plugins.{ plugin_name } .plugin"
105108 self .logger .debug (f"尝试导入模块: { module_import_path } " )
@@ -108,19 +111,23 @@ async def load_plugins(self, plugin_dir: str = "src/plugins"):
108111
109112 # --- 查找并实例化插件类 (使用入口点) ---
110113 plugin_class : Optional [Type [BasePlugin ]] = None
111- entrypoint = None # 初始化为 None
112- if hasattr (module , 'plugin_entrypoint' ):
113- entrypoint = getattr (module , 'plugin_entrypoint' )
114- self .logger .debug (f"在模块 '{ module_import_path } ' 中找到入口点 'plugin_entrypoint' 指向: { entrypoint } " )
114+ entrypoint = None # 初始化为 None
115+ if hasattr (module , "plugin_entrypoint" ):
116+ entrypoint = getattr (module , "plugin_entrypoint" )
117+ self .logger .debug (
118+ f"在模块 '{ module_import_path } ' 中找到入口点 'plugin_entrypoint' 指向: { entrypoint } "
119+ )
115120 # 检查 entrypoint 是否是类,并且具有我们的标记属性
116- if inspect .isclass (entrypoint ) and getattr (entrypoint , ' _is_vup_next_plugin' , False ):
121+ if inspect .isclass (entrypoint ) and getattr (entrypoint , " _is_vup_next_plugin" , False ):
117122 plugin_class = entrypoint
118123 self .logger .debug (f"入口点验证成功 (通过标记属性),插件类为: { plugin_class .__name__ } " )
119124 else :
120- self .logger .warning (f"模块 '{ module_import_path } ' 中的 'plugin_entrypoint' ({ entrypoint } ) 不是有效的插件类 (缺少标记或不是类)。" )
125+ self .logger .warning (
126+ f"模块 '{ module_import_path } ' 中的 'plugin_entrypoint' ({ entrypoint } ) 不是有效的插件类 (缺少标记或不是类)。"
127+ )
121128 else :
122- self .logger .warning (f"在模块 '{ module_import_path } ' 中未找到入口点 'plugin_entrypoint'。" )
123-
129+ self .logger .warning (f"在模块 '{ module_import_path } ' 中未找到入口点 'plugin_entrypoint'。" )
130+
124131 if plugin_class :
125132 plugin_specific_config = self .global_plugin_config .get (plugin_name , {})
126133 self .logger .debug (f"为插件 '{ plugin_class .__name__ } ' 加载特定配置: { plugin_specific_config } " )
@@ -135,7 +142,9 @@ async def load_plugins(self, plugin_dir: str = "src/plugins"):
135142 self .logger .warning (f"未能为模块 '{ module_import_path } ' 找到并验证有效的插件类。" )
136143
137144 except ImportError as e :
138- self .logger .error (f"导入插件模块 '{ module_import_path if module else plugin_name } ' 失败: { e } " , exc_info = True )
145+ self .logger .error (
146+ f"导入插件模块 '{ module_import_path if module else plugin_name } ' 失败: { e } " , exc_info = True
147+ )
139148 except Exception as e :
140149 self .logger .error (f"加载或设置插件 '{ plugin_name } ' 时发生错误: { e } " , exc_info = True )
141150 # else: # 可以选择性地记录非插件目录的项
@@ -157,9 +166,9 @@ async def unload_plugins(self):
157166 if unload_tasks :
158167 results = await asyncio .gather (* unload_tasks , return_exceptions = True )
159168 for i , task in enumerate (unload_tasks ):
160- plugin_name = list (self .loaded_plugins .keys ())[i ]
161- if isinstance (results [i ], Exception ):
162- self .logger .error (f"清理插件 '{ plugin_name } ' 时出错: { results [i ]} " , exc_info = results [i ])
163-
169+ plugin_name = list (self .loaded_plugins .keys ())[i ]
170+ if isinstance (results [i ], Exception ):
171+ self .logger .error (f"清理插件 '{ plugin_name } ' 时出错: { results [i ]} " , exc_info = results [i ])
172+
164173 self .loaded_plugins .clear ()
165- self .logger .info ("所有插件已卸载。" )
174+ self .logger .info ("所有插件已卸载。" )
0 commit comments