Co-authored-by: Antoine <33425718+anth2o@users.noreply.github.com> Co-authored-by: Bastien <bastien.baret@gmail.com> Co-authored-by: Clément Sirieix <clement.sirieix@mistral.ai> Co-authored-by: Kim-Adeline Miguel <51720070+kimadeline@users.noreply.github.com> Co-authored-by: Mathias Gesbert <mathias.gesbert@mistral.ai> Co-authored-by: Maxime Dolores <maxime.dolores@ext.mistral.ai> Co-authored-by: Michel Thomazo <51709227+michelTho@users.noreply.github.com> Co-authored-by: Nelson PROIA <144663685+Nelson-PROIA@users.noreply.github.com> Co-authored-by: Pierre Rossinès <pierre.rossines@mistral.ai> Co-authored-by: Quentin <quentin.torroba@mistral.ai> Co-authored-by: Robin Gullo <robin.gullo@mistral.ai> Co-authored-by: Mistral Vibe <vibe@mistral.ai>
32 lines
966 B
Python
32 lines
966 B
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
import tomllib
|
|
from typing import Any
|
|
|
|
import tomli_w
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def read_cache(cache_path: Path) -> dict[str, Any]:
|
|
"""Read the cache.toml file, returning an empty dict on any error."""
|
|
try:
|
|
with cache_path.open("rb") as f:
|
|
return tomllib.load(f)
|
|
except (OSError, tomllib.TOMLDecodeError):
|
|
return {}
|
|
|
|
|
|
def write_cache(cache_path: Path, section: str, data: dict[str, Any]) -> None:
|
|
"""Write the full cache dict to cache.toml, merging with existing data."""
|
|
existing = read_cache(cache_path)
|
|
existing.setdefault(section, {})
|
|
existing[section].update(data)
|
|
try:
|
|
cache_path.parent.mkdir(parents=True, exist_ok=True)
|
|
with cache_path.open("wb") as f:
|
|
tomli_w.dump(existing, f)
|
|
except OSError:
|
|
logger.debug("Failed to write cache file %s", cache_path, exc_info=True)
|