Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/function/constant/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from typing import Final
from function.maintain.config import 读取配置

UNEXPECTED_TYPES: Final[set[str]] = {"xml", "json", "html"}
"""
Expand All @@ -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,单位为秒
"""
29 changes: 19 additions & 10 deletions src/function/maintain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand All @@ -22,6 +22,7 @@ class 配置信息:
"winget-tools": ""
},
"git": {
"retry_interval": 50,
"signature": False
},
"github": {
Expand Down Expand Up @@ -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:
"""
[验证配置]
验证指定的配置项和配置值的配对是否有效,返回为什么无效。
Expand All @@ -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):
Expand Down Expand Up @@ -123,19 +124,22 @@ 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 配置文件的指定配置项,并返回配置值。
如果读取失败则返回 None。
"""

try:
配置值: str | bool | None = 读取配置项(配置项, 静默)
配置值: str | bool | int | None = 读取配置项(配置项, 静默)

if 配置值 is None:
return None
Expand Down Expand Up @@ -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(配置信息.所在位置):
Expand Down Expand Up @@ -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(原因),我假设调用这个函数的地方会用红色显示错误消息
Expand All @@ -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 配置值:
# 使用默认配置值
Expand Down
5 changes: 3 additions & 2 deletions src/tools/maintain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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} 仓库在哪里: ",
Expand All @@ -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}): ",
Expand Down Expand Up @@ -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,跳过验证")
Expand Down