From 6ba2e74073c0c4465a410c23a3ca69aae7300d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B8=AD=E9=B8=AD=E3=80=8C=E3=82=AB=E3=83=A2=E3=80=8D?= <89643991+DuckDuckStudio@users.noreply.github.com> Date: Wed, 18 Feb 2026 03:30:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E6=94=AF=E6=8C=81=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=91=BD=E4=BB=A4=E9=87=8D=E8=AF=95=E9=97=B4=E9=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/function/constant/general.py | 12 ++++++++++++ src/function/maintain/config.py | 29 +++++++++++++++++++---------- src/tools/maintain/config.py | 5 +++-- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/function/constant/general.py b/src/function/constant/general.py index 8a845c6..834600b 100644 --- a/src/function/constant/general.py +++ b/src/function/constant/general.py @@ -3,6 +3,7 @@ """ from typing import Final +from function.maintain.config import 读取配置 UNEXPECTED_TYPES: Final[set[str]] = {"xml", "json", "html"} """ @@ -27,3 +28,14 @@ - 一般版本 ### This PR is automatically created by [Sundry](https://github.com/DuckDuckStudio/Sundry/) version x.x.x 🚀. """ + +_config_value = 读取配置("git.retry_interval") +if not isinstance(_config_value, int): + _config_value = 50 + +RETRY_INTERVAL: Final[int] = _config_value +""" +func/command/run 中的 runCommand 函数的重试间隔 + +默认为 50,单位为秒 +""" diff --git a/src/function/maintain/config.py b/src/function/maintain/config.py index eeeb41c..d4e07da 100644 --- a/src/function/maintain/config.py +++ b/src/function/maintain/config.py @@ -10,8 +10,8 @@ class 配置信息: 默认配置: dict[str, Any] = { - "$schema": "https://duckduckstudio.github.io/yazicbs.github.io/Tools/Sundry/config/schema/1.3.json", - "version": "1.3", + "$schema": "https://duckduckstudio.github.io/yazicbs.github.io/Tools/Sundry/config/schema/1.4.json", + "version": "1.4", "debug": False, "paths": { "winget-pkgs": "", @@ -22,6 +22,7 @@ class 配置信息: "winget-tools": "" }, "git": { + "retry_interval": 50, "signature": False }, "github": { @@ -72,12 +73,12 @@ class 配置信息: "repos.winget-tools" ] - 最新版本: str = "1.3" + 最新版本: str = "1.4" 所在位置: str = CONFIG_FILE_PATH """等同于 `from function.constant.paths import CONFIG_FILE_PATH`。""" -def 验证配置(配置项: str, 配置值: str | bool) -> str | None: +def 验证配置(配置项: str, 配置值: str | bool | int) -> str | None: """ [验证配置] 验证指定的配置项和配置值的配对是否有效,返回为什么无效。 @@ -93,7 +94,7 @@ def 验证配置(配置项: str, 配置值: str | bool) -> str | None: if 配置项.startswith("paths.") and isinstance(配置值, str): 配置值 = os.path.normpath(配置值) if (not os.path.exists(配置值)): - return f"配置文件中的目录 {Fore.BLUE}{配置值}{Fore.RESET} 不存在" + return f"目录 {Fore.BLUE}{配置值}{Fore.RESET} 不存在" return None elif 配置项.startswith("repos.") and isinstance(配置值, str): @@ -123,11 +124,14 @@ def 验证配置(配置项: str, 配置值: str | bool) -> str | None: elif (配置项 == "github.token") and (配置值 not in ["glm", "komac", "env"]): return "未知的 Token 读取源" + + elif (配置项 == "git.retry_interval") and (not isinstance(配置值, int)): + return f"应是整数,但实际是 {Fore.BLUE}{type(配置值)}{Fore.RESET}" else: return None -def 读取配置(配置项: str, 静默: bool = False) -> None | str | tuple[str, str] | bool: +def 读取配置(配置项: str, 静默: bool = False) -> None | str | tuple[str, str] | bool | int: """ [验证/转换后的配置值] 读取 Sundry 配置文件的指定配置项,并返回配置值。 @@ -135,7 +139,7 @@ def 读取配置(配置项: str, 静默: bool = False) -> None | str | tuple[str """ try: - 配置值: str | bool | None = 读取配置项(配置项, 静默) + 配置值: str | bool | int | None = 读取配置项(配置项, 静默) if 配置值 is None: return None @@ -170,11 +174,11 @@ def 读取配置(配置项: str, 静默: bool = False) -> None | str | tuple[str print(f"{消息头.错误} 读取配置 {配置项} 失败: {Fore.RED}{e}{Fore.RESET}") return None -def 读取配置项(配置项: str, 静默: bool = False) -> str | bool | None: +def 读取配置项(配置项: str, 静默: bool = False) -> str | bool | int | None: """ [原始字符串] 读取指定配置项的值,并返回配置项值。 - 预期返回非空 str 或 bool,读取失败返回 None。 + 读取失败返回 None。 """ if os.path.exists(配置信息.所在位置): @@ -252,7 +256,7 @@ def 获取配置schema(版本: str | float) -> dict[str, Any] | None: except Exception: return None -def 转换配置值(配置项: str, 配置值: str) -> str | bool: +def 转换配置值(配置项: str, 配置值: str) -> str | bool | int: """ 尝试将输入的配置值转换为符合配置文件要求的格式,如 y → true 遇到无法转换的会 raise OperationFailed(原因),我假设调用这个函数的地方会用红色显示错误消息 @@ -276,6 +280,11 @@ def 转换配置值(配置项: str, 配置值: str) -> str | bool: return False else: raise OperationFailed(f"{Fore.BLUE}{配置值}{Fore.RED} 不能代表是或否,请使用 y / n") + elif 配置项 in ("git.retry_interval"): + try: + return int(配置值) + except ValueError: + raise OperationFailed("指定的配置值不是整数") else: if not 配置值: # 使用默认配置值 diff --git a/src/tools/maintain/config.py b/src/tools/maintain/config.py index 3527d81..9d68627 100644 --- a/src/tools/maintain/config.py +++ b/src/tools/maintain/config.py @@ -17,7 +17,7 @@ from pygments.formatters import TerminalFormatter from catfood.exceptions.operation import OperationFailed -def 获取用户输入(配置项: str) -> str | bool: +def 获取用户输入(配置项: str) -> str | bool | int: 提示消息映射: dict[str, str] = { # paths.* "paths.winget-pkgs": f"{消息头.问题} 您的本地 winget-{Fore.YELLOW}pkgs{Fore.RESET} 仓库在哪里: ", @@ -26,6 +26,7 @@ def 获取用户输入(配置项: str) -> str | bool: "repos.winget-pkgs": f"{消息头.问题} 您的远程 winget-{Fore.YELLOW}pkgs{Fore.RESET} 仓库是什么 (owner/winget-pkgs): ", "repos.winget-tools": f"{消息头.问题} 您的远程 winget-{Fore.YELLOW}tools{Fore.RESET} 仓库是什么 (owner/winget-tools): ", # git.* + "git.retry_interval": f"{消息头.问题} 重试命令的间隔是? [间隔应为{Fore.GREEN}整数{Fore.RESET},{Fore.RED}负数{Fore.RESET}为不重试,{Fore.YELLOW}零{Fore.RESET}为立即重试] (默认为{Fore.BLUE} 50 {Fore.RESET}秒): ", "git.signature": f"{消息头.问题} 是否要为 Git 提交签名? (默认为{Fore.YELLOW}否{Fore.RESET}): ", # github.pr.* "github.pr.maintainer_can_modify": f"{消息头.问题} 是否允许维护者修改您的 PR 内容? (默认为{Fore.YELLOW}否{Fore.RESET}): ", @@ -183,7 +184,7 @@ def 更新配置文件() -> int: schema = 获取配置schema(当前配置版本) if schema: - with open(配置信息.所在位置, "r") as f: + with open(配置信息.所在位置, "r", encoding="utf-8") as f: jsonschema.validate(json.load(f), schema) else: print(f"{消息头.警告} 未能获取到当前配置版本的 schema,跳过验证")