2022-01-22 15:23:07 +08:00
|
|
|
|
"""本模块定义插件加载接口。
|
|
|
|
|
|
|
|
|
|
FrontMatter:
|
|
|
|
|
sidebar_position: 1
|
|
|
|
|
description: nonebot.plugin.load 模块
|
|
|
|
|
"""
|
2021-11-11 17:33:30 +08:00
|
|
|
|
import json
|
2021-11-15 23:05:05 +08:00
|
|
|
|
import warnings
|
2021-11-11 17:33:30 +08:00
|
|
|
|
from typing import Set, Iterable, Optional
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
import tomlkit
|
|
|
|
|
|
|
|
|
|
from . import _managers
|
2021-11-08 01:02:35 +08:00
|
|
|
|
from .export import Export
|
|
|
|
|
from .manager import PluginManager
|
|
|
|
|
from .plugin import Plugin, get_plugin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_plugin(module_path: str) -> Optional[Plugin]:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
module_path: 插件名称 `path.to.your.plugin`
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
manager = PluginManager([module_path])
|
|
|
|
|
_managers.append(manager)
|
|
|
|
|
return manager.load_plugin(module_path)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_plugins(*plugin_dir: str) -> Set[Plugin]:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""导入文件夹下多个插件,以 `_` 开头的插件不会被导入!
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
plugin_dir: 文件夹路径
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2021-11-11 17:33:30 +08:00
|
|
|
|
manager = PluginManager(search_path=plugin_dir)
|
|
|
|
|
_managers.append(manager)
|
|
|
|
|
return manager.load_all_plugins()
|
|
|
|
|
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def load_all_plugins(
|
|
|
|
|
module_path: Iterable[str], plugin_dir: Iterable[str]
|
|
|
|
|
) -> Set[Plugin]:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""导入指定列表中的插件以及指定目录下多个插件,以 `_` 开头的插件不会被导入!
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
module_path: 指定插件集合
|
2022-01-22 15:23:07 +08:00
|
|
|
|
plugin_dir: 指定文件夹路径集合
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2021-11-11 17:33:30 +08:00
|
|
|
|
manager = PluginManager(module_path, plugin_dir)
|
|
|
|
|
_managers.append(manager)
|
|
|
|
|
return manager.load_all_plugins()
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""导入指定 json 文件中的 `plugins` 以及 `plugin_dirs` 下多个插件,以 `_` 开头的插件不会被导入!
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
file_path: 指定 json 文件路径
|
|
|
|
|
encoding: 指定 json 文件编码
|
2022-01-22 15:23:07 +08:00
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
```json title=plugins.json
|
|
|
|
|
{
|
|
|
|
|
"plugins": ["some_plugin"],
|
|
|
|
|
"plugin_dirs": ["some_dir"]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
nonebot.load_from_json("plugins.json")
|
|
|
|
|
```
|
2022-01-12 19:10:29 +08:00
|
|
|
|
"""
|
2021-11-08 01:02:35 +08:00
|
|
|
|
with open(file_path, "r", encoding=encoding) as f:
|
|
|
|
|
data = json.load(f)
|
|
|
|
|
plugins = data.get("plugins")
|
|
|
|
|
plugin_dirs = data.get("plugin_dirs")
|
|
|
|
|
assert isinstance(plugins, list), "plugins must be a list of plugin name"
|
2021-11-22 23:21:26 +08:00
|
|
|
|
assert isinstance(plugin_dirs, list), "plugin_dirs must be a list of directories"
|
2021-11-08 01:02:35 +08:00
|
|
|
|
return load_all_plugins(set(plugins), set(plugin_dirs))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""导入指定 toml 文件 `[tool.nonebot]` 中的 `plugins` 以及 `plugin_dirs` 下多个插件,以 `_` 开头的插件不会被导入!
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
file_path: 指定 toml 文件路径
|
|
|
|
|
encoding: 指定 toml 文件编码
|
2022-01-22 15:23:07 +08:00
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
```toml title=pyproject.toml
|
|
|
|
|
[tool.nonebot]
|
|
|
|
|
plugins = ["some_plugin"]
|
|
|
|
|
plugin_dirs = ["some_dir"]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
nonebot.load_from_toml("pyproject.toml")
|
|
|
|
|
```
|
2022-01-12 19:10:29 +08:00
|
|
|
|
"""
|
2021-11-08 01:02:35 +08:00
|
|
|
|
with open(file_path, "r", encoding=encoding) as f:
|
|
|
|
|
data = tomlkit.parse(f.read()) # type: ignore
|
|
|
|
|
|
2021-11-15 23:05:05 +08:00
|
|
|
|
nonebot_data = data.get("tool", {}).get("nonebot")
|
2021-11-08 01:02:35 +08:00
|
|
|
|
if not nonebot_data:
|
2021-11-15 23:05:05 +08:00
|
|
|
|
nonebot_data = data.get("nonebot", {}).get("plugins")
|
|
|
|
|
if nonebot_data:
|
|
|
|
|
warnings.warn(
|
|
|
|
|
"[nonebot.plugins] table are now deprecated. Use [tool.nonebot] instead.",
|
2021-11-22 23:21:26 +08:00
|
|
|
|
DeprecationWarning,
|
|
|
|
|
)
|
2021-11-15 23:05:05 +08:00
|
|
|
|
else:
|
|
|
|
|
raise ValueError("Cannot find '[tool.nonebot]' in given toml file!")
|
2021-11-08 01:02:35 +08:00
|
|
|
|
plugins = nonebot_data.get("plugins", [])
|
|
|
|
|
plugin_dirs = nonebot_data.get("plugin_dirs", [])
|
|
|
|
|
assert isinstance(plugins, list), "plugins must be a list of plugin name"
|
2021-11-22 23:21:26 +08:00
|
|
|
|
assert isinstance(plugin_dirs, list), "plugin_dirs must be a list of directories"
|
2021-11-11 17:33:30 +08:00
|
|
|
|
return load_all_plugins(plugins, plugin_dirs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2022-01-10 22:52:10 +08:00
|
|
|
|
def load_builtin_plugin(name: str) -> Optional[Plugin]:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""导入 NoneBot 内置插件。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
name: 插件名称
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
|
|
|
|
return load_plugin(f"nonebot.plugins.{name}")
|
|
|
|
|
|
|
|
|
|
|
2022-01-10 22:52:10 +08:00
|
|
|
|
def load_builtin_plugins(*plugins) -> Set[Plugin]:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""导入多个 NoneBot 内置插件。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
plugins: 插件名称列表
|
2022-01-10 22:52:10 +08:00
|
|
|
|
"""
|
|
|
|
|
return load_all_plugins([f"nonebot.plugins.{p}" for p in plugins], [])
|
|
|
|
|
|
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
def require(name: str) -> Export:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""获取一个插件的导出内容。
|
|
|
|
|
|
|
|
|
|
如果为 `load_plugins` 文件夹导入的插件,则为文件(夹)名。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-22 15:23:07 +08:00
|
|
|
|
name: 插件名,即 {ref}`nonebot.plugin.plugin.Plugin.name`。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:45:35 +08:00
|
|
|
|
异常:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
RuntimeError: 插件无法加载
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
|
|
|
|
plugin = get_plugin(name) or load_plugin(name)
|
|
|
|
|
if not plugin:
|
2021-11-22 23:21:26 +08:00
|
|
|
|
raise RuntimeError(f'Cannot load plugin "{name}"!')
|
2021-11-08 01:02:35 +08:00
|
|
|
|
return plugin.export
|