From 16df5706ff671f8e2aad53876cd9be02dbcc79fb Mon Sep 17 00:00:00 2001 From: snowy Date: Sun, 18 Aug 2024 04:40:02 +0800 Subject: [PATCH] =?UTF-8?q?:children=5Fcrossing:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E5=B7=A5=E4=BD=9C=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/plugin/load.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/liteyuki/plugin/load.py b/liteyuki/plugin/load.py index c78d71ee..a8cd6218 100644 --- a/liteyuki/plugin/load.py +++ b/liteyuki/plugin/load.py @@ -10,13 +10,12 @@ 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 liteyuki.log import logger from liteyuki.plugin.model import Plugin, PluginMetadata -from importlib import import_module -from nonebot import load_builtin_plugin from liteyuki.utils import path_to_module_name _plugins: dict[str, Plugin] = {} @@ -60,18 +59,31 @@ def load_plugin(module_path: str | Path) -> Optional[Plugin]: return None -def load_plugins(*plugin_dir: str) -> set[Plugin]: +def load_plugins(*plugin_dir: str, ignore_warning: bool = True) -> set[Plugin]: """导入文件夹下多个插件 参数: plugin_dir: 文件夹路径 + ignore_warning: 是否忽略警告,通常是目录不存在或目录为空 """ plugins = set() for dir_path in plugin_dir: # 遍历每一个文件夹下的py文件和包含__init__.py的文件夹,不递归 if not os.path.exists(dir_path): - logger.warning(f"Plugins dir '{dir_path}' does not exist.") + if not ignore_warning: + logger.warning(f"Plugins dir '{dir_path}' does not exist.") continue + + if not os.listdir(dir_path): + if not ignore_warning: + logger.warning(f"Plugins dir '{dir_path}' is empty.") + continue + + if not os.path.isdir(dir_path): + if not ignore_warning: + logger.warning(f"Plugins dir '{dir_path}' is not a directory.") + continue + for f in os.listdir(dir_path): path = Path(os.path.join(dir_path, f))