添加插件加载模块及相关模型,支持插件的动态加载与管理

This commit is contained in:
远野千束(神羽) 2024-12-14 03:32:56 +08:00
parent 40f1982708
commit 23f9a6dde4
6 changed files with 167 additions and 1 deletions

0
.pre-commit/check_filename.py Normal file → Executable file
View File

View File

View File

@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
本模块为工具加载模块
"""
import os
import traceback
from importlib import import_module
from pathlib import Path
from typing import Optional
from nonebot import logger
from .models import Plugin, PluginMetadata
from .utils import path_to_module_name
_plugins: dict[str, Plugin] = {}
__all__ = [
"load_plugin",
"load_plugins",
"_plugins",
]
def load_plugin(module_path: str | Path) -> Optional[Plugin]:
"""加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
该函数产生的副作用在于将插件加载到 `_plugins`
Args:
module_path: 插件名称 `path.to.your.plugin`
或插件路径 `pathlib.Path(path/to/your/plugin)`
Returns:
Optional[Plugin]: 插件对象
"""
module_path = (
path_to_module_name(Path(module_path))
if isinstance(module_path, Path)
else module_path
)
try:
module = import_module(module_path) # 导入模块对象
plugin = Plugin(
name=module.__name__,
module=module,
module_name=module_path,
)
plugin.metadata = getattr(module, "__marsho_meta__", None)
_plugins[plugin.name] = plugin
logger.opt(colors=True).success(
f'Succeeded to load liteyuki plugin "{plugin.name}"'
)
return _plugins[module.__name__]
except Exception as e:
logger.opt(colors=True).success(
f'Failed to load liteyuki plugin "<r>{module_path}</r>"'
)
traceback.print_exc()
return None
def load_plugins(*plugin_dirs: str) -> set[Plugin]:
"""导入文件夹下多个插件
参数:
plugin_dir: 文件夹路径
ignore_warning: 是否忽略警告通常是目录不存在或目录为空
返回:
set[Plugin]: 插件集合
"""
plugins = set()
for plugin_dir in plugin_dirs:
for f in os.listdir(plugin_dir):
path = Path(os.path.join(plugin_dir, f))
module_name = None
if os.path.isfile(path) and f.endswith(".py"):
"""单文件加载"""
module_name = f"{path_to_module_name(Path(plugin_dir))}.{f[:-3]}"
elif os.path.isdir(path) and os.path.exists(os.path.join(path, "__init__.py")):
"""包加载"""
module_name = path_to_module_name(path)
if module_name and (plugin := load_plugin(module_name)):
plugins.add(plugin)
return plugins

View File

@ -0,0 +1,56 @@
from types import ModuleType
from typing import Any
from pydantic import BaseModel
class Plugin(BaseModel):
"""
存储插件信息
Attributes:
----------
name: str
包名称 例如marsho_test
module: ModuleType
插件模块对象
module_name: str
点分割模块路径 例如a.b.c
metadata: "PluginMeta" | None
"""
name: str
"""包名称 例如marsho_test"""
module: ModuleType
"""插件模块对象"""
module_name: str
"""点分割模块路径 例如a.b.c"""
metadata: "PluginMetadata" | None = None
""""""
class PluginMetadata(BaseModel):
"""
Marsho 插件 对象元数据
Attributes:
----------
name: str
友好名称: 例如Marsho Test
description: str
插件描述
usage: str
插件使用方法
type: str
插件类型
author: str
插件作者
homepage: str
插件主页
extra: dict[str, Any]
额外信息自定义键值对
"""
name: str
description: str = ""
usage: str = ""
author: str = ""
homepage: str = ""
extra: dict[str, Any] = {}

View File

@ -0,0 +1,16 @@
from pathlib import Path
def path_to_module_name(path: Path) -> str:
"""
转换路径为模块名
Args:
path: 路径a/b/c/d -> a.b.c.d
Returns:
str: 模块名
"""
rel_path = path.resolve().relative_to(Path.cwd().resolve())
if rel_path.stem == "__init__":
return ".".join(rel_path.parts[:-1])
else:
return ".".join(rel_path.parts[:-1] + (rel_path.stem,))

View File

@ -19,7 +19,8 @@ dependencies = [
"ruamel.yaml>=0.18.6",
"pyyaml>=6.0.2",
"psutil>=6.1.0",
"beautifulsoup4>=4.12.3"
"beautifulsoup4>=4.12.3",
"pydantic>=2.10.3"
]
license = { text = "MIT, Mulan PSL v2" }