# -*- coding: utf-8 -*- from __future__ import annotations import re from pathlib import Path from typing import Any def current_filename(record: dict[str, Any]) -> str: if record.get("type") == "week": return f"week-{int(record.get('week') or 0):02d}.json" return "overall.json" def cleanup_legacy_files(directory: Path, record: dict[str, Any], keep: str) -> None: if record.get("type") == "week": week = int(record.get("week") or 0) pattern = re.compile(rf"^week[-_]?0?{week}(?:[-_].*)?\.json$", re.I) else: pattern = re.compile(r"^overall(?:[-_].*)?\.json$", re.I) for path in directory.glob("*.json"): if path.name in {"index.json", "state.json", keep}: continue if pattern.match(path.name): try: path.unlink() except OSError: pass