🐛 fix parent plugin detect error

This commit is contained in:
yanyongyu 2021-12-20 00:28:17 +08:00
parent c2c3d5ef4b
commit 44e5182322
2 changed files with 15 additions and 5 deletions

View File

@ -150,9 +150,11 @@ class PluginLoader(SourceFileLoader):
if self.loaded:
return
plugin = _new_plugin(self.name, module)
plugin = _new_plugin(self.name, module, self.manager)
parent_plugin = _current_plugin.get()
if parent_plugin:
if parent_plugin and _managers.index(parent_plugin.manager) < _managers.index(
self.manager
):
plugin.parent_plugin = parent_plugin
parent_plugin.sub_plugins.add(plugin)

View File

@ -1,10 +1,13 @@
from types import ModuleType
from dataclasses import field, dataclass
from typing import Set, Dict, Type, Optional
from typing import TYPE_CHECKING, Set, Dict, Type, Optional
from .export import Export
from nonebot.matcher import Matcher
if TYPE_CHECKING:
from .manager import PluginManager
plugins: Dict[str, "Plugin"] = {}
"""
:类型: ``Dict[str, Plugin]``
@ -31,6 +34,11 @@ class Plugin(object):
- **类型**: ``str``
- **说明**: 点分割模块路径
"""
manager: "PluginManager"
"""
- **类型**: ``PluginManager``
- **说明**: 导入该插件的插件管理器
"""
export: Export = field(default_factory=Export)
"""
- **类型**: ``Export``
@ -83,10 +91,10 @@ def get_loaded_plugins() -> Set[Plugin]:
return set(plugins.values())
def _new_plugin(fullname: str, module: ModuleType) -> Plugin:
def _new_plugin(fullname: str, module: ModuleType, manager: "PluginManager") -> Plugin:
name = fullname.rsplit(".", 1)[-1] if "." in fullname else fullname
if name in plugins:
raise RuntimeError("Plugin already exists! Check your plugin name.")
plugin = Plugin(name, module, fullname)
plugin = Plugin(name, module, fullname, manager)
plugins[name] = plugin
return plugin