core/cachew: use cache_dir in mcachew if it wasn't specified by the user
This commit is contained in:
parent
3e821ca7fd
commit
271cd7feef
2 changed files with 17 additions and 8 deletions
|
@ -219,31 +219,39 @@ _CACHE_DIR_NONE_HACK = Path('/tmp/hpi/cachew_none_hack')
|
||||||
"""See core.cachew.cache_dir for the explanation"""
|
"""See core.cachew.cache_dir for the explanation"""
|
||||||
|
|
||||||
|
|
||||||
|
_cache_path_dflt = cast(str, object())
|
||||||
# TODO I don't really like 'mcachew', just 'cache' would be better... maybe?
|
# TODO I don't really like 'mcachew', just 'cache' would be better... maybe?
|
||||||
# todo ugh. I think it needs doublewrap, otherwise @mcachew without args doesn't work
|
# todo ugh. I think it needs @doublewrap, otherwise @mcachew without args doesn't work
|
||||||
def mcachew(*args, **kwargs): # type: ignore[no-redef]
|
# but it's a bit problematic.. doublewrap works by defecting if the first arg is callable
|
||||||
|
# but here cache_path can also be a callable (for lazy/dynamic path)... so unclear how to detect this
|
||||||
|
def mcachew(cache_path=_cache_path_dflt, **kwargs): # type: ignore[no-redef]
|
||||||
"""
|
"""
|
||||||
Stands for 'Maybe cachew'.
|
Stands for 'Maybe cachew'.
|
||||||
Defensive wrapper around @cachew to make it an optional dependency.
|
Defensive wrapper around @cachew to make it an optional dependency.
|
||||||
"""
|
"""
|
||||||
cpath = kwargs.get('cache_path')
|
if cache_path is _cache_path_dflt:
|
||||||
if isinstance(cpath, (str, Path)):
|
# wasn't specified... so we need to use cache_dir
|
||||||
|
from .cachew import cache_dir
|
||||||
|
cache_path = cache_dir()
|
||||||
|
|
||||||
|
if isinstance(cache_path, (str, Path)):
|
||||||
try:
|
try:
|
||||||
# check that it starts with 'hack' path
|
# check that it starts with 'hack' path
|
||||||
Path(cpath).relative_to(_CACHE_DIR_NONE_HACK)
|
Path(cache_path).relative_to(_CACHE_DIR_NONE_HACK)
|
||||||
except:
|
except:
|
||||||
pass # no action needed, doesn't start with 'hack' string
|
pass # no action needed, doesn't start with 'hack' string
|
||||||
else:
|
else:
|
||||||
# todo show warning? tbh unclear how to detect when user stopped using 'old' way and using suffix instead?
|
# todo show warning? tbh unclear how to detect when user stopped using 'old' way and using suffix instead?
|
||||||
# if it does, means that user wanted to disable cache
|
# if it does, means that user wanted to disable cache
|
||||||
kwargs['cache_path'] = None
|
cache_path = None
|
||||||
try:
|
try:
|
||||||
import cachew
|
import cachew
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
warnings.warn('cachew library not found. You might want to install it to speed things up. See https://github.com/karlicoss/cachew')
|
warnings.warn('cachew library not found. You might want to install it to speed things up. See https://github.com/karlicoss/cachew')
|
||||||
return lambda orig_func: orig_func
|
return lambda orig_func: orig_func
|
||||||
else:
|
else:
|
||||||
return cachew.cachew(*args, **kwargs)
|
kwargs['cache_path'] = cache_path
|
||||||
|
return cachew.cachew(**kwargs)
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache(1)
|
@functools.lru_cache(1)
|
||||||
|
|
|
@ -107,7 +107,8 @@ def test_cachew() -> None:
|
||||||
from my.core.common import mcachew
|
from my.core.common import mcachew
|
||||||
|
|
||||||
called = 0
|
called = 0
|
||||||
@mcachew
|
# FIXME ugh. need doublewrap or something
|
||||||
|
@mcachew()
|
||||||
def cf() -> List[int]:
|
def cf() -> List[int]:
|
||||||
nonlocal called
|
nonlocal called
|
||||||
called += 1
|
called += 1
|
||||||
|
|
Loading…
Add table
Reference in a new issue