diff --git a/docs/api/nonebot.md b/docs/api/nonebot.md
index e3b80445..c11cf2be 100644
--- a/docs/api/nonebot.md
+++ b/docs/api/nonebot.md
@@ -52,6 +52,9 @@ sidebarDepth: 0
* `load_plugins` => `nonebot.plugin.load_plugins`
+* `load_all_plugins` => `nonebot.plugin.load_all_plugins`
+
+
* `load_builtin_plugins` => `nonebot.plugin.load_builtin_plugins`
diff --git a/docs/api/plugin.md b/docs/api/plugin.md
index 1d134c52..8b16427a 100644
--- a/docs/api/plugin.md
+++ b/docs/api/plugin.md
@@ -1267,7 +1267,7 @@ def something_else():
* **说明**
- 使用 `importlib` 加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
+ 使用 `PluginManager` 加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
@@ -1308,6 +1308,32 @@ def something_else():
+## `load_all_plugins(module_path, plugin_dir)`
+
+
+* **说明**
+
+ 导入指定列表中的插件以及指定目录下多个插件,以 `_` 开头的插件不会被导入!
+
+
+
+* **参数**
+
+
+ * `module_path: Set[str]`: 指定插件集合
+
+
+ * `plugin_dir: Set[str]`: 指定插件路径集合
+
+
+
+* **返回**
+
+
+ * `Set[Plugin]`
+
+
+
## `load_builtin_plugins(name='echo')`
diff --git a/nonebot/__init__.py b/nonebot/__init__.py
index db0d8277..efe6ed81 100644
--- a/nonebot/__init__.py
+++ b/nonebot/__init__.py
@@ -18,6 +18,7 @@
- ``Matchergroup`` => ``nonebot.plugin.MatcherGroup``
- ``load_plugin`` => ``nonebot.plugin.load_plugin``
- ``load_plugins`` => ``nonebot.plugin.load_plugins``
+- ``load_all_plugins`` => ``nonebot.plugin.load_all_plugins``
- ``load_builtin_plugins`` => ``nonebot.plugin.load_builtin_plugins``
- ``get_plugin`` => ``nonebot.plugin.get_plugin``
- ``get_loaded_plugins`` => ``nonebot.plugin.get_loaded_plugins``
@@ -221,5 +222,5 @@ def run(host: Optional[str] = None,
from nonebot.plugin import on_message, on_notice, on_request, on_metaevent, CommandGroup, MatcherGroup
from nonebot.plugin import on_startswith, on_endswith, on_keyword, on_command, on_shell_command, on_regex
-from nonebot.plugin import load_plugin, load_plugins, load_builtin_plugins
+from nonebot.plugin import load_plugin, load_plugins, load_all_plugins, load_builtin_plugins
from nonebot.plugin import export, require, get_plugin, get_loaded_plugins
diff --git a/nonebot/plugin/__init__.py b/nonebot/plugin/__init__.py
index 540eac89..439067af 100644
--- a/nonebot/plugin/__init__.py
+++ b/nonebot/plugin/__init__.py
@@ -948,6 +948,32 @@ class MatcherGroup:
return matcher
+def _load_plugin(manager: PluginManager, plugin_name: str) -> Optional[Plugin]:
+ if plugin_name.startswith("_"):
+ return None
+
+ _tmp_matchers.set(set())
+ _export.set(Export())
+
+ if plugin_name in plugins:
+ return None
+
+ try:
+ module = manager.load_plugin(plugin_name)
+
+ for m in _tmp_matchers.get():
+ m.module = plugin_name
+ plugin = Plugin(plugin_name, module, _tmp_matchers.get(), _export.get())
+ plugins[plugin_name] = plugin
+ logger.opt(
+ colors=True).info(f'Succeeded to import "{plugin_name}"')
+ return plugin
+ except Exception as e:
+ logger.opt(colors=True, exception=e).error(
+ f'Failed to import "{plugin_name}"')
+ return None
+
+
def load_plugin(module_path: str) -> Optional[Plugin]:
"""
:说明:
@@ -963,34 +989,9 @@ def load_plugin(module_path: str) -> Optional[Plugin]:
- ``Optional[Plugin]``
"""
- def _load_plugin(module_path: str) -> Optional[Plugin]:
- try:
- _tmp_matchers.set(set())
- _export.set(Export())
- if module_path in plugins:
- return plugins[module_path]
- elif module_path in sys.modules:
- logger.warning(
- f"Module {module_path} has been loaded by other plugins! Ignored"
- )
- return None
- module = importlib.import_module(module_path)
- for m in _tmp_matchers.get():
- m.module = module_path
- plugin = Plugin(module_path, module, _tmp_matchers.get(),
- _export.get())
- plugins[module_path] = plugin
- logger.opt(
- colors=True).info(f'Succeeded to import "{module_path}"')
- return plugin
- except Exception as e:
- logger.opt(colors=True, exception=e).error(
- f'Failed to import "{module_path}"'
- )
- return None
-
context: Context = copy_context()
- return context.run(_load_plugin, module_path)
+ manager = PluginManager(PLUGIN_NAMESPACE, plugins=[module_path])
+ return context.run(_load_plugin, manager, module_path)
def load_plugins(*plugin_dir: str) -> Set[Plugin]:
@@ -1003,6 +1004,32 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
- ``*plugin_dir: str``: 插件路径
+ :返回:
+
+ - ``Set[Plugin]``
+ """
+ loaded_plugins = set()
+ manager = PluginManager(PLUGIN_NAMESPACE, search_path=plugin_dir)
+ for plugin_name in manager.list_plugins():
+ context: Context = copy_context()
+ result = context.run(_load_plugin, manager, plugin_name)
+ if result:
+ loaded_plugins.add(result)
+ return loaded_plugins
+
+
+def load_all_plugins(module_path: Set[str],
+ plugin_dir: Set[str]) -> Set[Plugin]:
+ """
+ :说明:
+
+ 导入指定列表中的插件以及指定目录下多个插件,以 ``_`` 开头的插件不会被导入!
+
+ :参数:
+
+ - ``module_path: Set[str]``: 指定插件集合
+ - ``plugin_dir: Set[str]``: 指定插件路径集合
+
:返回:
- ``Set[Plugin]``
@@ -1036,7 +1063,7 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
return None
loaded_plugins = set()
- manager = PluginManager(PLUGIN_NAMESPACE, search_path=plugin_dir)
+ manager = PluginManager(PLUGIN_NAMESPACE, module_path, plugin_dir)
for plugin_name in manager.list_plugins():
context: Context = copy_context()
result = context.run(_load_plugin, plugin_name)
diff --git a/nonebot/plugin/__init__.pyi b/nonebot/plugin/__init__.pyi
index 409043d5..9549d45e 100644
--- a/nonebot/plugin/__init__.pyi
+++ b/nonebot/plugin/__init__.pyi
@@ -176,6 +176,11 @@ def load_plugins(*plugin_dir: str) -> Set[Plugin]:
...
+def load_all_plugins(module_path: Set[str],
+ plugin_dir: Set[str]) -> Set[Plugin]:
+ ...
+
+
def load_builtin_plugins(name: str = ...):
...
diff --git a/tests/bot.py b/tests/bot.py
index b263bf49..e4ec39e4 100644
--- a/tests/bot.py
+++ b/tests/bot.py
@@ -25,11 +25,10 @@ driver.register_adapter("mirai", MiraiBot)
# load builtin plugin
nonebot.load_builtin_plugins()
-nonebot.load_plugin("nonebot_plugin_apscheduler")
-nonebot.load_plugin("nonebot_plugin_test")
-# load local plugins
-nonebot.load_plugins("test_plugins")
+# load all plugins
+nonebot.load_all_plugins({"nonebot_plugin_apscheduler", "nonebot_plugin_test"},
+ {"test_plugins"})
# modify some config / config depends on loaded configs
config = driver.config