From 9ebb340c00be8451e396395d8fa2a3cc9f331c5c Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Tue, 15 Aug 2023 13:42:12 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E7=9A=84=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=9B=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/command/command.py | 78 ++++++++++++++++++++++------------------ util/general_utils.py | 2 +- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/model/command/command.py b/model/command/command.py index 0f40a992b..b387f20b3 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -88,40 +88,39 @@ class Command: def plugin_reload(self, cached_plugins: dict, target: str = None, all: bool = False): plugins = self.get_plugin_modules() fail_rec = "" - if plugins != None: - for p in plugins: - try: - if p not in cached_plugins or p == target or all: - module = __import__("addons.plugins." + p + "." + p, fromlist=[p]) - if p in cached_plugins: - module = importlib.reload(module) - cls = putil.get_classes(p, module) - obj = getattr(module, cls[0])() - try: - info = obj.info() - if 'name' not in info or 'desc' not in info or 'version' not in info or 'author' not in info: - fail_rec += f"载入插件{p}失败,原因: 插件信息不完整\n" - continue - if isinstance(info, dict) == False: - fail_rec += f"载入插件{p}失败,原因: 插件信息格式不正确\n" - continue - except BaseException as e: - fail_rec += f"调用插件{p} info失败, 原因: {str(e)}\n" - continue - cached_plugins[p] = { - "module": module, - "clsobj": obj, - "info": info - } - except BaseException as e: - raise e - fail_rec += f"加载{p}插件出现问题,原因{str(e)}\n" - if fail_rec == "": - return True, None - else: - return False, fail_rec - else: + if plugins is None: return False, "未找到任何插件模块" + + for p in plugins: + try: + if p not in cached_plugins or p == target or all: + module = __import__("addons.plugins." + p + "." + p, fromlist=[p]) + if p in cached_plugins: + module = importlib.reload(module) + cls = putil.get_classes(p, module) + obj = getattr(module, cls[0])() + try: + info = obj.info() + if 'name' not in info or 'desc' not in info or 'version' not in info or 'author' not in info: + fail_rec += f"载入插件{p}失败,原因: 插件信息不完整\n" + continue + if isinstance(info, dict) == False: + fail_rec += f"载入插件{p}失败,原因: 插件信息格式不正确\n" + continue + except BaseException as e: + fail_rec += f"调用插件{p} info失败, 原因: {str(e)}\n" + continue + cached_plugins[p] = { + "module": module, + "clsobj": obj, + "info": info + } + except BaseException as e: + fail_rec += f"加载{p}插件出现问题,原因{str(e)}\n" + if fail_rec == "": + return True, None + else: + return False, fail_rec ''' 插件指令 @@ -130,9 +129,9 @@ class Command: l = message.split(" ") if len(l) < 2: if platform == gu.PLATFORM_GOCQ: - p = gu.create_text_image("【插件指令面板】", "安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin i 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n") + p = gu.create_text_image("【插件指令面板】", "安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin d 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n") return True, [Image.fromFileSystem(p)], "plugin" - return True, "\n=====插件指令面板=====\n安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin i 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n===============", "plugin" + return True, "\n=====插件指令面板=====\n安装插件: \nplugin i 插件Github地址\n卸载插件: \nplugin d 插件名 \n重载插件: \nplugin reload\n查看插件列表:\nplugin l\n更新插件: plugin u 插件名\n===============", "plugin" else: ppath = "" if os.path.exists("addons/plugins"): @@ -188,6 +187,15 @@ class Command: try: repo = Repo(path = plugin_path) repo.remotes.origin.pull() + + # 读取插件的requirements.txt + if os.path.exists(os.path.join(plugin_path, "requirements.txt")): + with open(os.path.join(plugin_path, "requirements.txt"), "r", encoding="utf-8") as f: + for line in f.readlines(): + mm = os.system(f"pip3 install {line.strip()}") + if mm != 0: + return False, "插件依赖安装失败,需要您手动pip安装对应插件的依赖。", "plugin" + ok, err = self.plugin_reload(cached_plugins, target=l[2]) if ok: return True, "\n更新插件成功!!", "plugin" diff --git a/util/general_utils.py b/util/general_utils.py index 38455a135..44be4203f 100644 --- a/util/general_utils.py +++ b/util/general_utils.py @@ -51,7 +51,7 @@ def log( tag: str = "System", fg: str = None, bg: str = None, - max_len: int = 100): + max_len: int = 300): """ 日志记录函数 """