# -*- coding: utf-8 -*- """桌面应用版本信息与云端升级策略判断。""" from __future__ import annotations import re from typing import Any APP_VERSION = "1.1.2" _VERSION_PATTERN = re.compile( r"^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)" r"(?:[-+][0-9A-Za-z.-]+)?$" ) def normalize_version(value: Any) -> str: """校验并统一版本号格式,允许用户输入可选的 v 前缀。""" text = str(value or "").strip() match = _VERSION_PATTERN.fullmatch(text) if not match: return "" return text[1:] if text.lower().startswith("v") else text def release_status(release: Any, *, local_version: str = APP_VERSION) -> dict[str, Any]: """把云端发布策略转换为桌面端可直接消费的状态。""" source = release if isinstance(release, dict) else {} local = normalize_version(local_version) or APP_VERSION latest = normalize_version(source.get("latest_version")) or local update_available = latest != local download_url = str(source.get("download_url") or "").strip() force_upgrade = bool(source.get("force_upgrade")) and update_available return { "local_version": local, "latest_version": latest, "download_url": download_url, "release_notes": str(source.get("release_notes") or "").strip(), "update_available": update_available, "force_upgrade": force_upgrade, "updated_at": str(source.get("updated_at") or ""), }