/ spoolman / filecache.py
filecache.py
 1  """A file-based cache system for reading/writing files."""
 2  
 3  from pathlib import Path
 4  
 5  from spoolman.env import get_cache_dir
 6  
 7  
 8  def get_file(name: str) -> Path:
 9      """Get the path to a file in the cache dir."""
10      return get_cache_dir() / name
11  
12  
13  def update_file(name: str, data: bytes) -> None:
14      """Update a file if it differs from the given data."""
15      path = get_file(name)
16      if path.exists() and path.read_bytes() == data:
17          return
18      path.parent.mkdir(parents=True, exist_ok=True)
19      path.write_bytes(data)
20  
21  
22  def get_file_contents(name: str) -> bytes:
23      """Get the contents of a file."""
24      path = get_file(name)
25      return path.read_bytes()