core/cachew: special handling for None in order to preserve cache_dir() path

+ add 'suffix' argument for more straighforward logic
This commit is contained in:
Dima Gerasimov 2021-02-21 17:43:00 +00:00 committed by karlicoss
parent da3c1c9b74
commit 9afe1811a5
4 changed files with 59 additions and 3 deletions

View file

@ -38,6 +38,28 @@ def _appdirs_cache_dir() -> Path:
return cd
def cache_dir() -> Optional[Path]:
from . import PathIsh
def cache_dir(suffix: Optional[PathIsh] = None) -> Path:
from . import core_config as CC
return CC.config.get_cache_dir()
cdir_ = CC.config.get_cache_dir()
sp: Optional[Path] = None
if suffix is not None:
sp = Path(suffix)
# guess if you do need absolute, better path it directly instead of as suffix?
assert not sp.is_absolute(), sp
# ok, so ideally we could just return cdir_ / sp
# however, this function was at first used without the suffix, e.g. cache_dir() / 'some_dir'
# but now cache_dir setting can also be None which means 'disable cache'
# changing return type to Optional means that it will break for existing users even if the cache isn't used
# it's kinda wrong.. so we use dummy path (_CACHE_DIR_NONE_HACK), and then strip it away in core.common.mcachew
# this logic is tested via test_cachew_dir_none
if cdir_ is None:
from .common import _CACHE_DIR_NONE_HACK
cdir = _CACHE_DIR_NONE_HACK
else:
cdir = cdir_
return cdir if sp is None else cdir / sp