potodo/potodo/cache.py
Jules Lasne (jlasne - seluj78) 10bcd43f9d Removed utils folder
2020-10-13 17:08:35 +02:00

34 lines
858 B
Python

import logging
import os
import pickle
from pathlib import Path
from typing import cast
from typing import Dict
from potodo.po_file import PoFileStats
def get_cache_file_content(
path: str = ".potodo/cache.pickle",
) -> Dict[Path, PoFileStats]:
logging.debug("Trying to load cache from %s", path)
try:
with open(path, "rb") as handle:
data = pickle.load(handle)
except FileNotFoundError:
logging.warning("No cache found")
return {}
else:
logging.debug("Found cache")
return cast(Dict[Path, PoFileStats], data)
def set_cache_content(
obj: Dict[Path, PoFileStats], path: str = ".potodo/cache.pickle"
) -> None:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "wb") as handle:
pickle.dump(obj, handle)
logging.debug("Set cache to %s", path)