2022-01-22 15:23:07 +08:00
|
|
|
|
"""本模块实现插件加载流程。
|
|
|
|
|
|
|
|
|
|
参考: [import hooks](https://docs.python.org/3/reference/import.html#import-hooks), [PEP302](https://www.python.org/dev/peps/pep-0302/)
|
|
|
|
|
|
|
|
|
|
FrontMatter:
|
|
|
|
|
sidebar_position: 5
|
|
|
|
|
description: nonebot.plugin.manager 模块
|
|
|
|
|
"""
|
2023-06-24 14:47:35 +08:00
|
|
|
|
|
2021-02-19 14:58:26 +08:00
|
|
|
|
import sys
|
|
|
|
|
import pkgutil
|
|
|
|
|
import importlib
|
2021-06-19 15:15:00 +08:00
|
|
|
|
from pathlib import Path
|
2021-11-11 17:33:30 +08:00
|
|
|
|
from itertools import chain
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from typing import Optional
|
2021-02-19 14:58:26 +08:00
|
|
|
|
from types import ModuleType
|
|
|
|
|
from importlib.abc import MetaPathFinder
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from collections.abc import Iterable, Sequence
|
2021-03-31 20:38:00 +08:00
|
|
|
|
from importlib.machinery import PathFinder, SourceFileLoader
|
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
from nonebot.log import logger
|
2022-08-31 10:07:14 +08:00
|
|
|
|
from nonebot.utils import escape_tag, path_to_module_name
|
2022-01-15 21:27:43 +08:00
|
|
|
|
|
2023-09-27 16:00:26 +08:00
|
|
|
|
from .model import Plugin, PluginMetadata
|
2022-05-26 16:35:47 +08:00
|
|
|
|
from . import (
|
|
|
|
|
_managers,
|
|
|
|
|
_new_plugin,
|
|
|
|
|
_revert_plugin,
|
2024-04-20 14:47:12 +08:00
|
|
|
|
_current_plugin,
|
|
|
|
|
_module_name_to_plugin_id,
|
2022-05-26 16:35:47 +08:00
|
|
|
|
)
|
2021-02-19 14:58:26 +08:00
|
|
|
|
|
2021-11-09 00:57:59 +08:00
|
|
|
|
|
2021-02-19 14:58:26 +08:00
|
|
|
|
class PluginManager:
|
2022-05-26 16:35:47 +08:00
|
|
|
|
"""插件管理器。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
plugins: 独立插件模块名集合。
|
2024-04-20 14:47:12 +08:00
|
|
|
|
search_path: 插件搜索路径(文件夹),相对于当前工作目录。
|
2022-05-26 16:35:47 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2021-11-09 00:57:59 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
plugins: Optional[Iterable[str]] = None,
|
|
|
|
|
search_path: Optional[Iterable[str]] = None,
|
|
|
|
|
):
|
2021-02-19 14:58:26 +08:00
|
|
|
|
# simple plugin not in search path
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self.plugins: set[str] = set(plugins or [])
|
|
|
|
|
self.search_path: set[str] = set(search_path or [])
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
# cache plugins
|
2024-04-20 14:47:12 +08:00
|
|
|
|
self._third_party_plugin_ids: dict[str, str] = {}
|
|
|
|
|
self._searched_plugin_ids: dict[str, str] = {}
|
|
|
|
|
self._prepare_plugins()
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
2024-04-20 14:47:12 +08:00
|
|
|
|
return f"PluginManager(available_plugins={self.controlled_modules})"
|
2022-09-09 11:52:57 +08:00
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
|
@property
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def third_party_plugins(self) -> set[str]:
|
2024-04-20 14:47:12 +08:00
|
|
|
|
"""返回所有独立插件标识符。"""
|
|
|
|
|
return set(self._third_party_plugin_ids.keys())
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
|
|
@property
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def searched_plugins(self) -> set[str]:
|
2024-04-20 14:47:12 +08:00
|
|
|
|
"""返回已搜索到的插件标识符。"""
|
|
|
|
|
return set(self._searched_plugin_ids.keys())
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
|
|
@property
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def available_plugins(self) -> set[str]:
|
2024-04-20 14:47:12 +08:00
|
|
|
|
"""返回当前插件管理器中可用的插件标识符。"""
|
2022-05-26 16:35:47 +08:00
|
|
|
|
return self.third_party_plugins | self.searched_plugins
|
2021-02-19 14:58:26 +08:00
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
@property
|
|
|
|
|
def controlled_modules(self) -> dict[str, str]:
|
|
|
|
|
"""返回当前插件管理器中控制的插件标识符与模块路径映射字典。"""
|
|
|
|
|
return dict(
|
|
|
|
|
chain(
|
|
|
|
|
self._third_party_plugin_ids.items(), self._searched_plugin_ids.items()
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _previous_controlled_modules(self) -> dict[str, str]:
|
2024-04-16 00:33:48 +08:00
|
|
|
|
_pre_managers: list[PluginManager]
|
2021-11-11 17:33:30 +08:00
|
|
|
|
if self in _managers:
|
2021-11-22 23:21:26 +08:00
|
|
|
|
_pre_managers = _managers[: _managers.index(self)]
|
2021-11-11 17:33:30 +08:00
|
|
|
|
else:
|
|
|
|
|
_pre_managers = _managers[:]
|
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
|
return {
|
2024-04-20 14:47:12 +08:00
|
|
|
|
plugin_id: module_name
|
|
|
|
|
for manager in _pre_managers
|
|
|
|
|
for plugin_id, module_name in manager.controlled_modules.items()
|
2022-05-26 16:35:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
def _prepare_plugins(self) -> set[str]:
|
2022-05-26 16:35:47 +08:00
|
|
|
|
"""搜索插件并缓存插件名称。"""
|
2021-11-11 17:33:30 +08:00
|
|
|
|
# get all previous ready to load plugins
|
2024-04-20 14:47:12 +08:00
|
|
|
|
previous_plugin_ids = self._previous_controlled_modules()
|
|
|
|
|
|
|
|
|
|
# if self not in global managers, merge self's controlled modules
|
|
|
|
|
def get_controlled_modules():
|
|
|
|
|
return (
|
|
|
|
|
previous_plugin_ids
|
|
|
|
|
if self in _managers
|
|
|
|
|
else {**previous_plugin_ids, **self.controlled_modules}
|
|
|
|
|
)
|
2021-12-21 11:18:34 +08:00
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
|
# check third party plugins
|
2021-12-21 11:18:34 +08:00
|
|
|
|
for plugin in self.plugins:
|
2024-04-20 14:47:12 +08:00
|
|
|
|
plugin_id = _module_name_to_plugin_id(plugin, get_controlled_modules())
|
|
|
|
|
if (
|
|
|
|
|
plugin_id in self._third_party_plugin_ids
|
|
|
|
|
or plugin_id in previous_plugin_ids
|
|
|
|
|
):
|
2021-12-21 11:18:34 +08:00
|
|
|
|
raise RuntimeError(
|
2024-04-20 14:47:12 +08:00
|
|
|
|
f"Plugin already exists: {plugin_id}! Check your plugin name"
|
2021-12-21 11:18:34 +08:00
|
|
|
|
)
|
2024-04-20 14:47:12 +08:00
|
|
|
|
self._third_party_plugin_ids[plugin_id] = plugin
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
|
|
# check plugins in search path
|
2021-11-11 17:33:30 +08:00
|
|
|
|
for module_info in pkgutil.iter_modules(self.search_path):
|
2022-05-26 16:35:47 +08:00
|
|
|
|
# ignore if startswith "_"
|
2021-11-11 17:33:30 +08:00
|
|
|
|
if module_info.name.startswith("_"):
|
2021-06-19 15:15:00 +08:00
|
|
|
|
continue
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
2022-08-31 10:07:14 +08:00
|
|
|
|
if not (
|
|
|
|
|
module_spec := module_info.module_finder.find_spec(
|
|
|
|
|
module_info.name, None
|
|
|
|
|
)
|
|
|
|
|
):
|
2021-11-11 17:33:30 +08:00
|
|
|
|
continue
|
2024-04-20 14:47:12 +08:00
|
|
|
|
|
|
|
|
|
if not module_spec.origin:
|
2021-11-11 17:33:30 +08:00
|
|
|
|
continue
|
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
# get module name from path, pkgutil does not return the actual module name
|
|
|
|
|
module_path = Path(module_spec.origin).resolve()
|
|
|
|
|
module_name = path_to_module_name(module_path)
|
|
|
|
|
plugin_id = _module_name_to_plugin_id(module_name, get_controlled_modules())
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
plugin_id in previous_plugin_ids
|
|
|
|
|
or plugin_id in self._third_party_plugin_ids
|
|
|
|
|
or plugin_id in self._searched_plugin_ids
|
|
|
|
|
):
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"Plugin already exists: {plugin_id}! Check your plugin name"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self._searched_plugin_ids[plugin_id] = module_name
|
2021-11-11 17:33:30 +08:00
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
|
return self.available_plugins
|
2021-11-11 17:33:30 +08:00
|
|
|
|
|
2022-01-26 15:37:35 +08:00
|
|
|
|
def load_plugin(self, name: str) -> Optional[Plugin]:
|
2022-05-26 16:35:47 +08:00
|
|
|
|
"""加载指定插件。
|
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
可以使用完整插件模块名或者插件标识符加载。
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
|
|
参数:
|
2024-04-20 14:47:12 +08:00
|
|
|
|
name: 插件名称或插件标识符。
|
2022-05-26 16:35:47 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
try:
|
2024-04-20 14:47:12 +08:00
|
|
|
|
# load using plugin id
|
|
|
|
|
if name in self._third_party_plugin_ids:
|
|
|
|
|
module = importlib.import_module(self._third_party_plugin_ids[name])
|
|
|
|
|
elif name in self._searched_plugin_ids:
|
|
|
|
|
module = importlib.import_module(self._searched_plugin_ids[name])
|
|
|
|
|
# load using module name
|
|
|
|
|
elif (
|
|
|
|
|
name in self._third_party_plugin_ids.values()
|
|
|
|
|
or name in self._searched_plugin_ids.values()
|
|
|
|
|
):
|
2021-11-11 17:33:30 +08:00
|
|
|
|
module = importlib.import_module(name)
|
2022-01-26 15:06:53 +08:00
|
|
|
|
else:
|
|
|
|
|
raise RuntimeError(f"Plugin not found: {name}! Check your plugin name")
|
2021-11-11 17:33:30 +08:00
|
|
|
|
|
2023-02-20 13:19:05 +08:00
|
|
|
|
if (
|
|
|
|
|
plugin := getattr(module, "__plugin__", None)
|
|
|
|
|
) is None or not isinstance(plugin, Plugin):
|
2022-01-26 15:06:53 +08:00
|
|
|
|
raise RuntimeError(
|
|
|
|
|
f"Module {module.__name__} is not loaded as a plugin! "
|
2024-04-20 14:47:12 +08:00
|
|
|
|
f"Make sure not to import it before loading."
|
2022-01-26 15:06:53 +08:00
|
|
|
|
)
|
2023-02-20 13:19:05 +08:00
|
|
|
|
logger.opt(colors=True).success(
|
2024-04-20 14:47:12 +08:00
|
|
|
|
f'Succeeded to load plugin "<y>{escape_tag(plugin.id_)}</y>"'
|
2023-02-20 13:19:05 +08:00
|
|
|
|
+ (
|
|
|
|
|
f' from "<m>{escape_tag(plugin.module_name)}</m>"'
|
2024-04-20 14:47:12 +08:00
|
|
|
|
if plugin.module_name != plugin.id_
|
2023-02-20 13:19:05 +08:00
|
|
|
|
else ""
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-01-26 15:06:53 +08:00
|
|
|
|
return plugin
|
2021-11-11 17:33:30 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.opt(colors=True, exception=e).error(
|
|
|
|
|
f'<r><bg #f8bbd0>Failed to import "{escape_tag(name)}"</bg #f8bbd0></r>'
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def load_all_plugins(self) -> set[Plugin]:
|
2022-05-26 16:35:47 +08:00
|
|
|
|
"""加载所有可用插件。"""
|
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
return set(
|
2022-05-26 16:35:47 +08:00
|
|
|
|
filter(None, (self.load_plugin(name) for name in self.available_plugins))
|
2021-11-22 23:21:26 +08:00
|
|
|
|
)
|
2021-06-19 15:15:00 +08:00
|
|
|
|
|
2021-02-19 14:58:26 +08:00
|
|
|
|
|
|
|
|
|
class PluginFinder(MetaPathFinder):
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def find_spec(
|
|
|
|
|
self,
|
|
|
|
|
fullname: str,
|
2022-12-20 18:13:45 +08:00
|
|
|
|
path: Optional[Sequence[str]],
|
2021-11-22 23:21:26 +08:00
|
|
|
|
target: Optional[ModuleType] = None,
|
|
|
|
|
):
|
2021-11-11 17:33:30 +08:00
|
|
|
|
if _managers:
|
|
|
|
|
module_spec = PathFinder.find_spec(fullname, path, target)
|
|
|
|
|
if not module_spec:
|
|
|
|
|
return
|
|
|
|
|
module_origin = module_spec.origin
|
|
|
|
|
if not module_origin:
|
|
|
|
|
return
|
|
|
|
|
|
2022-01-26 20:55:23 +08:00
|
|
|
|
for manager in reversed(_managers):
|
2024-04-20 14:47:12 +08:00
|
|
|
|
if fullname in manager.controlled_modules.values():
|
2021-11-22 23:21:26 +08:00
|
|
|
|
module_spec.loader = PluginLoader(manager, fullname, module_origin)
|
2021-11-11 17:33:30 +08:00
|
|
|
|
return module_spec
|
|
|
|
|
return
|
2021-02-19 14:58:26 +08:00
|
|
|
|
|
|
|
|
|
|
2021-03-13 18:21:56 +08:00
|
|
|
|
class PluginLoader(SourceFileLoader):
|
2024-04-20 14:47:12 +08:00
|
|
|
|
def __init__(self, manager: PluginManager, fullname: str, path: str) -> None:
|
2021-03-19 14:59:59 +08:00
|
|
|
|
self.manager = manager
|
2021-03-13 18:21:56 +08:00
|
|
|
|
self.loaded = False
|
|
|
|
|
super().__init__(fullname, path)
|
|
|
|
|
|
|
|
|
|
def create_module(self, spec) -> Optional[ModuleType]:
|
|
|
|
|
if self.name in sys.modules:
|
|
|
|
|
self.loaded = True
|
|
|
|
|
return sys.modules[self.name]
|
2021-03-19 14:59:59 +08:00
|
|
|
|
# return None to use default module creation
|
2021-03-13 18:21:56 +08:00
|
|
|
|
return super().create_module(spec)
|
|
|
|
|
|
|
|
|
|
def exec_module(self, module: ModuleType) -> None:
|
|
|
|
|
if self.loaded:
|
|
|
|
|
return
|
2021-03-31 20:38:00 +08:00
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
|
# create plugin before executing
|
2021-12-20 00:28:17 +08:00
|
|
|
|
plugin = _new_plugin(self.name, module, self.manager)
|
2022-05-26 16:35:47 +08:00
|
|
|
|
setattr(module, "__plugin__", plugin)
|
|
|
|
|
|
|
|
|
|
# enter plugin context
|
2024-04-20 14:47:12 +08:00
|
|
|
|
_plugin_token = _current_plugin.set(plugin)
|
2021-05-10 18:39:59 +08:00
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
|
try:
|
|
|
|
|
super().exec_module(module)
|
|
|
|
|
except Exception:
|
|
|
|
|
_revert_plugin(plugin)
|
|
|
|
|
raise
|
|
|
|
|
finally:
|
|
|
|
|
# leave plugin context
|
2024-04-20 14:47:12 +08:00
|
|
|
|
_current_plugin.reset(_plugin_token)
|
2022-01-09 23:15:33 +08:00
|
|
|
|
|
2022-06-20 15:49:53 +08:00
|
|
|
|
# get plugin metadata
|
|
|
|
|
metadata: Optional[PluginMetadata] = getattr(module, "__plugin_meta__", None)
|
|
|
|
|
plugin.metadata = metadata
|
|
|
|
|
|
2021-03-19 14:59:59 +08:00
|
|
|
|
return
|
2021-03-13 18:21:56 +08:00
|
|
|
|
|
|
|
|
|
|
2021-02-19 14:58:26 +08:00
|
|
|
|
sys.meta_path.insert(0, PluginFinder())
|