From 7b476060394aa834b65e3cbef885e9cd6785c4f8 Mon Sep 17 00:00:00 2001 From: Snowykami Date: Sat, 14 Dec 2024 03:33:59 +0800 Subject: [PATCH] :art: Apply black formatting --- nonebot_plugin_marshoai/tool/load.py | 16 +++++++++------- nonebot_plugin_marshoai/tool/models.py | 7 +++++-- nonebot_plugin_marshoai/tool/utils.py | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/nonebot_plugin_marshoai/tool/load.py b/nonebot_plugin_marshoai/tool/load.py index 1549841d..bec03024 100755 --- a/nonebot_plugin_marshoai/tool/load.py +++ b/nonebot_plugin_marshoai/tool/load.py @@ -40,15 +40,15 @@ def load_plugin(module_path: str | Path) -> Optional[Plugin]: else module_path ) try: - module = import_module(module_path) # 导入模块对象 + 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( @@ -79,15 +79,17 @@ def load_plugins(*plugin_dirs: str) -> set[Plugin]: 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")): + 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 \ No newline at end of file + return plugins diff --git a/nonebot_plugin_marshoai/tool/models.py b/nonebot_plugin_marshoai/tool/models.py index eae2da16..aec2b4d3 100644 --- a/nonebot_plugin_marshoai/tool/models.py +++ b/nonebot_plugin_marshoai/tool/models.py @@ -6,7 +6,7 @@ from pydantic import BaseModel class Plugin(BaseModel): """ 存储插件信息 - + Attributes: ---------- name: str @@ -18,6 +18,7 @@ class Plugin(BaseModel): metadata: "PluginMeta" | None 元 """ + name: str """包名称 例如marsho_test""" module: ModuleType @@ -27,6 +28,7 @@ class Plugin(BaseModel): metadata: "PluginMetadata" | None = None """元""" + class PluginMetadata(BaseModel): """ Marsho 插件 对象元数据 @@ -48,9 +50,10 @@ class PluginMetadata(BaseModel): extra: dict[str, Any] 额外信息,自定义键值对 """ + name: str description: str = "" usage: str = "" author: str = "" homepage: str = "" - extra: dict[str, Any] = {} \ No newline at end of file + extra: dict[str, Any] = {} diff --git a/nonebot_plugin_marshoai/tool/utils.py b/nonebot_plugin_marshoai/tool/utils.py index 790eb0a4..ca63a25c 100644 --- a/nonebot_plugin_marshoai/tool/utils.py +++ b/nonebot_plugin_marshoai/tool/utils.py @@ -13,4 +13,4 @@ def path_to_module_name(path: Path) -> str: if rel_path.stem == "__init__": return ".".join(rel_path.parts[:-1]) else: - return ".".join(rel_path.parts[:-1] + (rel_path.stem,)) \ No newline at end of file + return ".".join(rel_path.parts[:-1] + (rel_path.stem,))