mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
✨ Feature: 添加插件元数据字段 type
homepage
supported_adapters
(#2012)
This commit is contained in:
parent
0c4a040394
commit
9616b4c0ca
@ -16,7 +16,7 @@ from .manager import PluginManager
|
|||||||
from . import _managers, get_plugin, _module_name_to_plugin_name
|
from . import _managers, get_plugin, _module_name_to_plugin_name
|
||||||
|
|
||||||
try: # pragma: py-gte-311
|
try: # pragma: py-gte-311
|
||||||
import tomllib # pyright: reportMissingImports=false
|
import tomllib # pyright: ignore[reportMissingImports]
|
||||||
except ModuleNotFoundError: # pragma: py-lt-311
|
except ModuleNotFoundError: # pragma: py-lt-311
|
||||||
import tomli as tomllib
|
import tomli as tomllib
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
"""本模块定义插件对象。
|
"""本模块定义插件相关信息。
|
||||||
|
|
||||||
FrontMatter:
|
FrontMatter:
|
||||||
sidebar_position: 3
|
sidebar_position: 3
|
||||||
description: nonebot.plugin.plugin 模块
|
description: nonebot.plugin.plugin 模块
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
from dataclasses import field, dataclass
|
from dataclasses import field, dataclass
|
||||||
from typing import TYPE_CHECKING, Any, Set, Dict, Type, Optional
|
from typing import TYPE_CHECKING, Any, Set, Dict, Type, Optional
|
||||||
@ -11,11 +13,11 @@ from typing import TYPE_CHECKING, Any, Set, Dict, Type, Optional
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from nonebot.matcher import Matcher
|
from nonebot.matcher import Matcher
|
||||||
|
from nonebot.utils import resolve_dot_notation
|
||||||
# FIXME: backport for nonebug
|
|
||||||
from . import _plugins as plugins # nopycln: import
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from nonebot.adapters import Adapter
|
||||||
|
|
||||||
from .manager import PluginManager
|
from .manager import PluginManager
|
||||||
|
|
||||||
|
|
||||||
@ -24,14 +26,39 @@ class PluginMetadata:
|
|||||||
"""插件元信息,由插件编写者提供"""
|
"""插件元信息,由插件编写者提供"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
"""插件可阅读名称"""
|
"""插件名称"""
|
||||||
description: str
|
description: str
|
||||||
"""插件功能介绍"""
|
"""插件功能介绍"""
|
||||||
usage: str
|
usage: str
|
||||||
"""插件使用方法"""
|
"""插件使用方法"""
|
||||||
|
type: Optional[str] = None
|
||||||
|
"""插件类型,用于商店分类"""
|
||||||
|
homepage: Optional[str] = None
|
||||||
|
"""插件主页"""
|
||||||
config: Optional[Type[BaseModel]] = None
|
config: Optional[Type[BaseModel]] = None
|
||||||
"""插件配置项"""
|
"""插件配置项"""
|
||||||
|
supported_adapters: Optional[Set[str]] = None
|
||||||
|
"""插件支持的适配器模块路径
|
||||||
|
|
||||||
|
格式为 `<module>[:<Adapter>]`,`~` 为 `nonebot.adapters.` 的缩写。
|
||||||
|
|
||||||
|
`None` 表示支持**所有适配器**。
|
||||||
|
"""
|
||||||
extra: Dict[Any, Any] = field(default_factory=dict)
|
extra: Dict[Any, Any] = field(default_factory=dict)
|
||||||
|
"""插件额外信息,可由插件编写者自由扩展定义"""
|
||||||
|
|
||||||
|
def get_supported_adapters(self) -> Optional[Set[Type["Adapter"]]]:
|
||||||
|
"""获取当前已安装的插件支持适配器类列表"""
|
||||||
|
if self.supported_adapters is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
adapters = set()
|
||||||
|
for adapter in self.supported_adapters:
|
||||||
|
with contextlib.suppress(ModuleNotFoundError, AttributeError):
|
||||||
|
adapters.add(
|
||||||
|
resolve_dot_notation(adapter, "Adapter", "nonebot.adapters.")
|
||||||
|
)
|
||||||
|
return adapters
|
||||||
|
|
||||||
|
|
||||||
@dataclass(eq=False)
|
@dataclass(eq=False)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from nonebot.adapters import Adapter
|
||||||
from nonebot.plugin import PluginMetadata
|
from nonebot.plugin import PluginMetadata
|
||||||
|
|
||||||
|
|
||||||
@ -7,10 +8,17 @@ class Config(BaseModel):
|
|||||||
custom: str = ""
|
custom: str = ""
|
||||||
|
|
||||||
|
|
||||||
|
class FakeAdapter(Adapter):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
__plugin_meta__ = PluginMetadata(
|
__plugin_meta__ = PluginMetadata(
|
||||||
name="测试插件",
|
name="测试插件",
|
||||||
description="测试插件元信息",
|
description="测试插件元信息",
|
||||||
usage="无法使用",
|
usage="无法使用",
|
||||||
|
type="application",
|
||||||
|
homepage="https://v2.nonebot.dev",
|
||||||
config=Config,
|
config=Config,
|
||||||
|
supported_adapters={"~onebot.v11", "plugins.metadata:FakeAdapter"},
|
||||||
extra={"author": "NoneBot"},
|
extra={"author": "NoneBot"},
|
||||||
)
|
)
|
||||||
|
@ -128,7 +128,7 @@ async def test_require_not_found():
|
|||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_plugin_metadata():
|
async def test_plugin_metadata():
|
||||||
from plugins.metadata import Config
|
from plugins.metadata import Config, FakeAdapter
|
||||||
|
|
||||||
plugin = nonebot.get_plugin("metadata")
|
plugin = nonebot.get_plugin("metadata")
|
||||||
assert plugin
|
assert plugin
|
||||||
@ -137,6 +137,11 @@ async def test_plugin_metadata():
|
|||||||
"name": "测试插件",
|
"name": "测试插件",
|
||||||
"description": "测试插件元信息",
|
"description": "测试插件元信息",
|
||||||
"usage": "无法使用",
|
"usage": "无法使用",
|
||||||
|
"type": "application",
|
||||||
|
"homepage": "https://v2.nonebot.dev",
|
||||||
"config": Config,
|
"config": Config,
|
||||||
|
"supported_adapters": {"~onebot.v11", "plugins.metadata:FakeAdapter"},
|
||||||
"extra": {"author": "NoneBot"},
|
"extra": {"author": "NoneBot"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert plugin.metadata.get_supported_adapters() == {FakeAdapter}
|
||||||
|
Loading…
Reference in New Issue
Block a user