core: add base cachew directory

This commit is contained in:
Dima Gerasimov 2020-07-31 12:14:50 +01:00 committed by karlicoss
parent 10a8ebaae4
commit c54d85037c
4 changed files with 24 additions and 2 deletions

View file

@ -13,6 +13,7 @@ from typing import Iterable, NamedTuple, Sequence, Set
from ..common import mcachew, LazyLogger, get_files from ..common import mcachew, LazyLogger, get_files
from ..core.cachew import cache_dir
from my.config import bluemaestro as config from my.config import bluemaestro as config
@ -28,7 +29,7 @@ class Measurement(NamedTuple):
temp: float temp: float
@mcachew(cache_path=config.cache_path) @mcachew(cache_path=cache_dir() / 'bluemaestro.cache')
def measurements(dbs=inputs()) -> Iterable[Measurement]: def measurements(dbs=inputs()) -> Iterable[Measurement]:
emitted: Set[datetime] = set() emitted: Set[datetime] = set()
for f in dbs: for f in dbs:

View file

@ -1,5 +1,6 @@
# TODO this probably belongs to cachew? or cachew.experimental # TODO this probably belongs to cachew? or cachew.experimental
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path
def disable_cachew(): def disable_cachew():
@ -25,3 +26,21 @@ def disabled_cachew():
yield yield
finally: finally:
cachew.cachew = old 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)

View file

@ -15,6 +15,7 @@ import pytz
from more_itertools import bucket from more_itertools import bucket
from ..common import get_files, LazyLogger, cproperty, mcachew from ..common import get_files, LazyLogger, cproperty, mcachew
from ..core.cachew import cache_dir
from my.config import emfit as config from my.config import emfit as config
@ -292,7 +293,7 @@ def dir_hash(path: Path):
# TODO take __file__ into account somehow? # 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]: def iter_datas(path: Path=config.export_path) -> Iterator[Emfit]:
for f in get_files(path, glob='*.json'): for f in get_files(path, glob='*.json'):
sid = f.stem sid = f.stem

View file

@ -30,6 +30,7 @@ from ..kython import kompress
logger = LazyLogger(__name__) logger = LazyLogger(__name__)
# todo switch to use cachew.cache_dir
def cache_path(*args, **kwargs): def cache_path(*args, **kwargs):
from my.config import location as config from my.config import location as config
return config.cache_path return config.cache_path