Files
spoolman2/spoolman/filecache.py
Donkie 620febc3ab Moved hishel cache directory into spoolmans data directory
This is to prevent permission issues
2024-05-21 11:25:08 +02:00

26 lines
664 B
Python

"""A file-based cache system for reading/writing files."""
from pathlib import Path
from spoolman.env import get_cache_dir
def get_file(name: str) -> Path:
"""Get the path to a file in the cache dir."""
return get_cache_dir() / name
def update_file(name: str, data: bytes) -> None:
"""Update a file if it differs from the given data."""
path = get_file(name)
if path.exists() and path.read_bytes() == data:
return
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(data)
def get_file_contents(name: str) -> bytes:
"""Get the contents of a file."""
path = get_file(name)
return path.read_bytes()