Added API for fetching external database, removed parameters structure,

This commit is contained in:
Donkie
2024-05-12 12:14:25 +02:00
parent 595fd82b56
commit cbaa7b81df
6 changed files with 127 additions and 48 deletions

30
spoolman/filecache.py Normal file
View File

@@ -0,0 +1,30 @@
"""A file-based cache system for reading/writing files."""
from pathlib import Path
from spoolman.env import get_data_dir
def _get_cache_dir() -> Path:
"""Get the cache directory."""
return get_data_dir() / "cache"
def get_file(name: str) -> Path:
"""Get the path to a file."""
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()