2023-05-18 16:00:10 +08:00
|
|
|
|
"""本模块定义插件相关信息。
|
2022-01-22 15:23:07 +08:00
|
|
|
|
|
|
|
|
|
FrontMatter:
|
|
|
|
|
sidebar_position: 3
|
2023-09-27 16:00:26 +08:00
|
|
|
|
description: nonebot.plugin.model 模块
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""
|
2023-05-18 16:00:10 +08:00
|
|
|
|
|
|
|
|
|
import contextlib
|
2021-11-08 01:02:35 +08:00
|
|
|
|
from types import ModuleType
|
|
|
|
|
from dataclasses import field, dataclass
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from typing import TYPE_CHECKING, Any, Type, Optional # noqa: UP035
|
2022-06-20 15:49:53 +08:00
|
|
|
|
|
|
|
|
|
from pydantic import BaseModel
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2021-11-11 17:33:30 +08:00
|
|
|
|
from nonebot.matcher import Matcher
|
2023-05-18 16:00:10 +08:00
|
|
|
|
from nonebot.utils import resolve_dot_notation
|
2022-01-15 21:27:43 +08:00
|
|
|
|
|
2021-12-20 00:28:17 +08:00
|
|
|
|
if TYPE_CHECKING:
|
2023-05-18 16:00:10 +08:00
|
|
|
|
from nonebot.adapters import Adapter
|
|
|
|
|
|
2021-12-20 00:28:17 +08:00
|
|
|
|
from .manager import PluginManager
|
|
|
|
|
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
@dataclass(eq=False)
|
2022-06-20 15:49:53 +08:00
|
|
|
|
class PluginMetadata:
|
|
|
|
|
"""插件元信息,由插件编写者提供"""
|
|
|
|
|
|
|
|
|
|
name: str
|
2023-05-18 16:00:10 +08:00
|
|
|
|
"""插件名称"""
|
2022-06-20 15:49:53 +08:00
|
|
|
|
description: str
|
|
|
|
|
"""插件功能介绍"""
|
|
|
|
|
usage: str
|
|
|
|
|
"""插件使用方法"""
|
2023-05-18 16:00:10 +08:00
|
|
|
|
type: Optional[str] = None
|
|
|
|
|
"""插件类型,用于商店分类"""
|
|
|
|
|
homepage: Optional[str] = None
|
|
|
|
|
"""插件主页"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
config: Optional[Type[BaseModel]] = None # noqa: UP006
|
2022-06-20 15:49:53 +08:00
|
|
|
|
"""插件配置项"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
supported_adapters: Optional[set[str]] = None
|
2023-05-18 16:00:10 +08:00
|
|
|
|
"""插件支持的适配器模块路径
|
|
|
|
|
|
|
|
|
|
格式为 `<module>[:<Adapter>]`,`~` 为 `nonebot.adapters.` 的缩写。
|
|
|
|
|
|
|
|
|
|
`None` 表示支持**所有适配器**。
|
|
|
|
|
"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
extra: dict[Any, Any] = field(default_factory=dict)
|
2023-05-18 16:00:10 +08:00
|
|
|
|
"""插件额外信息,可由插件编写者自由扩展定义"""
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def get_supported_adapters(self) -> Optional[set[Type["Adapter"]]]: # noqa: UP006
|
2023-05-18 16:00:10 +08:00
|
|
|
|
"""获取当前已安装的插件支持适配器类列表"""
|
|
|
|
|
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
|
2022-06-20 15:49:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(eq=False)
|
|
|
|
|
class Plugin:
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""存储插件信息"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
2021-11-08 01:02:35 +08:00
|
|
|
|
name: str
|
2022-06-20 15:49:53 +08:00
|
|
|
|
"""插件索引标识,NoneBot 使用 文件/文件夹 名称作为标识符"""
|
2021-11-08 01:02:35 +08:00
|
|
|
|
module: ModuleType
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""插件模块对象"""
|
2021-11-08 01:02:35 +08:00
|
|
|
|
module_name: str
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""点分割模块路径"""
|
2021-12-20 00:28:17 +08:00
|
|
|
|
manager: "PluginManager"
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""导入该插件的插件管理器"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
matcher: set[type[Matcher]] = field(default_factory=set)
|
2023-03-29 12:22:50 +08:00
|
|
|
|
"""插件加载时定义的 `Matcher`"""
|
2021-11-08 01:02:35 +08:00
|
|
|
|
parent_plugin: Optional["Plugin"] = None
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""父插件"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
sub_plugins: set["Plugin"] = field(default_factory=set)
|
2022-01-22 15:23:07 +08:00
|
|
|
|
"""子插件集合"""
|
2022-06-20 15:49:53 +08:00
|
|
|
|
metadata: Optional[PluginMetadata] = None
|