diff --git a/my/bluemaestro/__init__.py b/my/bluemaestro/__init__.py index b76c1bd..882c675 100755 --- a/my/bluemaestro/__init__.py +++ b/my/bluemaestro/__init__.py @@ -13,6 +13,7 @@ from typing import Iterable, NamedTuple, Sequence, Set from ..common import mcachew, LazyLogger, get_files +from ..core.cachew import cache_dir from my.config import bluemaestro as config @@ -28,7 +29,7 @@ class Measurement(NamedTuple): temp: float -@mcachew(cache_path=config.cache_path) +@mcachew(cache_path=cache_dir() / 'bluemaestro.cache') def measurements(dbs=inputs()) -> Iterable[Measurement]: emitted: Set[datetime] = set() for f in dbs: diff --git a/my/core/cachew.py b/my/core/cachew.py index 94f0376..d1f05e0 100644 --- a/my/core/cachew.py +++ b/my/core/cachew.py @@ -1,5 +1,6 @@ # TODO this probably belongs to cachew? or cachew.experimental from contextlib import contextmanager +from pathlib import Path def disable_cachew(): @@ -25,3 +26,21 @@ def disabled_cachew(): yield finally: cachew.cachew = old + + +def cache_dir() -> Path: + ''' + Base directory for cachew. + To override, add to your config file: + class config: + cache_dir = '/your/custom/cache/path' + ''' + import my.config as C + common_config = getattr(C, 'common', object()) + # TODO if attr is set _and_ it's none, disable cache? + cdir = getattr(common_config, 'cache_dir', None) + if cdir is None: + # TODO fallback to default cachew dir instead? + return Path('/var/tmp/cachew') + else: + return Path(cdir) diff --git a/my/emfit/__init__.py b/my/emfit/__init__.py index 93ea337..96858db 100755 --- a/my/emfit/__init__.py +++ b/my/emfit/__init__.py @@ -15,6 +15,7 @@ import pytz from more_itertools import bucket from ..common import get_files, LazyLogger, cproperty, mcachew +from ..core.cachew import cache_dir from my.config import emfit as config @@ -292,7 +293,7 @@ def dir_hash(path: Path): # TODO take __file__ into account somehow? -@mcachew(cache_path=config.cache_path, hashf=dir_hash, logger=logger) +@mcachew(cache_path=cache_dir() / 'emfit.cache', hashf=dir_hash, logger=logger) def iter_datas(path: Path=config.export_path) -> Iterator[Emfit]: for f in get_files(path, glob='*.json'): sid = f.stem diff --git a/my/location/takeout.py b/my/location/takeout.py index a7cfb9f..6dbce72 100644 --- a/my/location/takeout.py +++ b/my/location/takeout.py @@ -30,6 +30,7 @@ from ..kython import kompress logger = LazyLogger(__name__) +# todo switch to use cachew.cache_dir def cache_path(*args, **kwargs): from my.config import location as config return config.cache_path