From c8af5d8445bf12e0b7aad87083ac1aa36caa3cd2 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Mon, 31 Jul 2023 17:46:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=96=AD=E8=A8=80=E5=87=BD=E6=95=B0require=5Fver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/plugin/models.py | 26 ++++++++++++++++++++++++++ pkg/utils/updater.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/pkg/plugin/models.py b/pkg/plugin/models.py index c0f2ca8..6a6ba9d 100644 --- a/pkg/plugin/models.py +++ b/pkg/plugin/models.py @@ -164,6 +164,32 @@ def func(*args, **kwargs): __current_registering_plugin__ = "" +def require_ver(ge: str, le: str="v999.9.9") -> bool: + """插件版本要求装饰器 + + Args: + ge (str): 最低版本要求 + le (str, optional): 最高版本要求 + + Returns: + bool: 是否满足要求, False时为无法获取版本号,True时为满足要求,报错为不满足要求 + """ + qchatgpt_version = "" + + from pkg.utils.updater import get_current_tag, compare_version_str + + try: + qchatgpt_version = get_current_tag() # 从updater模块获取版本号 + except: + return False + + if compare_version_str(qchatgpt_version, ge) < 0 or \ + (compare_version_str(qchatgpt_version, le) > 0): + raise Exception("QChatGPT 版本不满足要求,某些功能(可能是由插件提供的)无法正常使用。(要求版本:{}-{},但当前版本:{})".format(ge, le, qchatgpt_version)) + + return True + + class Plugin: """插件基类""" diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index 9a661e7..956a36b 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -78,6 +78,34 @@ def get_current_tag() -> str: return current_tag +def compare_version_str(v0: str, v1: str) -> int: + """比较两个版本号""" + + # 删除版本号前的v + if v0.startswith("v"): + v0 = v0[1:] + if v1.startswith("v"): + v1 = v1[1:] + + v0:list = v0.split(".") + v1:list = v1.split(".") + + # 如果两个版本号节数不同,把短的后面用0补齐 + if len(v0) < len(v1): + v0.extend(["0"]*(len(v1)-len(v0))) + elif len(v0) > len(v1): + v1.extend(["0"]*(len(v0)-len(v1))) + + # 从高位向低位比较 + for i in range(len(v0)): + if int(v0[i]) > int(v1[i]): + return 1 + elif int(v0[i]) < int(v1[i]): + return -1 + + return 0 + + def update_all(cli: bool = False) -> bool: """检查更新并下载源码""" current_tag = get_current_tag()