8390706df8
Add taste-skill to catalog.yaml, and refresh pouch plus cloned catalog or custom Git sources on a 24h interval so users do not need a separate update command.
88 lines
2.6 KiB
Python
88 lines
2.6 KiB
Python
"""用户级 pouch 配置(~/.config/pouch/config.yaml)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
from pathlib import Path
|
||
from typing import Any
|
||
|
||
from pouch import yaml_io
|
||
from pouch.paths import CONFIG_FILE
|
||
|
||
DEFAULT_AUTO_UPDATE_INTERVAL = "24h"
|
||
_INTERVAL_UNITS = {
|
||
"s": 1,
|
||
"m": 60,
|
||
"h": 3600,
|
||
"d": 86400,
|
||
}
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class AutoUpdateSettings:
|
||
enabled: bool = True
|
||
interval_seconds: int = 24 * 3600
|
||
|
||
|
||
def load_user_config(path: Path | None = None) -> dict[str, Any]:
|
||
path = path or CONFIG_FILE
|
||
if not path.is_file():
|
||
return {}
|
||
data = yaml_io.safe_load(path.read_text(encoding="utf-8")) or {}
|
||
if not isinstance(data, dict):
|
||
raise SystemExit(f"pouch 配置格式错误: {path}")
|
||
return data
|
||
|
||
|
||
def save_user_config(data: dict[str, Any], path: Path | None = None) -> None:
|
||
path = path or CONFIG_FILE
|
||
path.parent.mkdir(parents=True, exist_ok=True)
|
||
path.write_text(
|
||
yaml_io.safe_dump(data, allow_unicode=True, sort_keys=False),
|
||
encoding="utf-8",
|
||
)
|
||
|
||
|
||
def parse_interval(value: Any) -> int:
|
||
if value is None or value is True:
|
||
return AutoUpdateSettings().interval_seconds
|
||
if isinstance(value, bool):
|
||
raise ValueError(f"无效的自动更新间隔: {value!r}")
|
||
if isinstance(value, int):
|
||
if value < 0:
|
||
raise ValueError(f"无效的自动更新间隔: {value!r}")
|
||
return value
|
||
text = str(value).strip().lower()
|
||
if not text:
|
||
return AutoUpdateSettings().interval_seconds
|
||
if text.isdigit():
|
||
return int(text)
|
||
unit = text[-1]
|
||
amount = text[:-1]
|
||
if unit in _INTERVAL_UNITS and amount.isdigit():
|
||
return int(amount) * _INTERVAL_UNITS[unit]
|
||
raise ValueError(f"无效的自动更新间隔: {value!r}(例如 24h、1d、60m、3600)")
|
||
|
||
|
||
def load_auto_update_settings(
|
||
config: dict[str, Any] | None = None,
|
||
path: Path | None = None,
|
||
) -> AutoUpdateSettings:
|
||
if config is None:
|
||
config = load_user_config(path)
|
||
raw = config.get("auto_update", True)
|
||
if raw is False:
|
||
return AutoUpdateSettings(enabled=False)
|
||
if raw is True or raw is None:
|
||
return AutoUpdateSettings()
|
||
if not isinstance(raw, dict):
|
||
return AutoUpdateSettings()
|
||
enabled = raw.get("enabled", True)
|
||
if enabled is False:
|
||
return AutoUpdateSettings(enabled=False)
|
||
try:
|
||
interval = parse_interval(raw.get("interval", DEFAULT_AUTO_UPDATE_INTERVAL))
|
||
except ValueError as exc:
|
||
raise SystemExit(str(exc)) from exc
|
||
return AutoUpdateSettings(enabled=bool(enabled), interval_seconds=interval)
|