mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-09-06 20:16:47 +00:00
✨ Feature: 嵌套插件名称作用域优化 (#2665)
This commit is contained in:
@ -15,7 +15,7 @@ from nonebot.utils import path_to_module_name
|
||||
|
||||
from .model import Plugin
|
||||
from .manager import PluginManager
|
||||
from . import _managers, get_plugin, _current_plugin_chain, _module_name_to_plugin_name
|
||||
from . import _managers, get_plugin, _module_name_to_plugin_id
|
||||
|
||||
try: # pragma: py-gte-311
|
||||
import tomllib # pyright: ignore[reportMissingImports]
|
||||
@ -151,36 +151,40 @@ def load_builtin_plugins(*plugins: str) -> set[Plugin]:
|
||||
|
||||
def _find_manager_by_name(name: str) -> Optional[PluginManager]:
|
||||
for manager in reversed(_managers):
|
||||
if name in manager.plugins or name in manager.searched_plugins:
|
||||
if (
|
||||
name in manager.controlled_modules
|
||||
or name in manager.controlled_modules.values()
|
||||
):
|
||||
return manager
|
||||
|
||||
|
||||
def require(name: str) -> ModuleType:
|
||||
"""获取一个插件的导出内容。
|
||||
|
||||
如果为 `load_plugins` 文件夹导入的插件,则为文件(夹)名。
|
||||
"""声明依赖插件。
|
||||
|
||||
参数:
|
||||
name: 插件名,即 {ref}`nonebot.plugin.model.Plugin.name`。
|
||||
name: 插件模块名或插件标识符,仅在已声明插件的情况下可使用标识符。
|
||||
|
||||
异常:
|
||||
RuntimeError: 插件无法加载
|
||||
"""
|
||||
plugin = get_plugin(_module_name_to_plugin_name(name))
|
||||
if "." in name:
|
||||
# name is a module name
|
||||
plugin = get_plugin(_module_name_to_plugin_id(name))
|
||||
else:
|
||||
# name is a plugin id or simple module name (equals to plugin id)
|
||||
plugin = get_plugin(name)
|
||||
|
||||
# if plugin not loaded
|
||||
if not plugin:
|
||||
# plugin already declared
|
||||
if plugin is None:
|
||||
# plugin already declared, module name / plugin id
|
||||
if manager := _find_manager_by_name(name):
|
||||
plugin = manager.load_plugin(name)
|
||||
|
||||
# plugin not declared, try to declare and load it
|
||||
else:
|
||||
# clear current plugin chain, ensure plugin loaded in a new context
|
||||
_t = _current_plugin_chain.set(())
|
||||
try:
|
||||
plugin = load_plugin(name)
|
||||
finally:
|
||||
_current_plugin_chain.reset(_t)
|
||||
if not plugin:
|
||||
plugin = load_plugin(name)
|
||||
|
||||
if plugin is None:
|
||||
raise RuntimeError(f'Cannot load plugin "{name}"!')
|
||||
return plugin.module
|
||||
|
||||
@ -200,9 +204,11 @@ def inherit_supported_adapters(*names: str) -> Optional[set[str]]:
|
||||
final_supported: Optional[set[str]] = None
|
||||
|
||||
for name in names:
|
||||
plugin = get_plugin(_module_name_to_plugin_name(name))
|
||||
plugin = get_plugin(_module_name_to_plugin_id(name))
|
||||
if plugin is None:
|
||||
raise RuntimeError(f'Plugin "{name}" is not loaded!')
|
||||
raise RuntimeError(
|
||||
f'Plugin "{name}" is not loaded! You should require it first.'
|
||||
)
|
||||
meta = plugin.metadata
|
||||
if meta is None:
|
||||
raise ValueError(f'Plugin "{name}" has no metadata!')
|
||||
|
Reference in New Issue
Block a user