HPI/my/core/cachew.py
Dima Gerasimov da3c1c9b74 core/cachew: rely on ~/.cache for default cache path
- rely on appdirs for default cache path instead of hardcoded /var/tmp/cachew
  technically backwards incompatible, but no action needed
  you might want to clean /var/tmp/cachew after updating

- use default cache path (e.g. ~/.cache) by default
  see https://github.com/ActiveState/appdirs#some-example-output for more info
  *warning*: things will be cached by default now (used to be uncached before)

- treat cache_dir = None in the config
  *warning*: kind of backwards incompatible.. but again nothing disasterous
2021-02-21 19:51:58 +00:00

43 lines
1.1 KiB
Python

from contextlib import contextmanager
from pathlib import Path
from typing import Optional
# can lead to some unexpected issues if you 'import cachew' which being in my/core directory.. so let's protect against it
# NOTE: if we use overlay, name can be smth like my.origg.my.core.cachew ...
assert 'my.core' in __name__, f'Expected module __name__ ({__name__}) to start with my.core'
def disable_cachew() -> None:
try:
import cachew
except ImportError:
# nothing to disable
return
from cachew import settings
settings.ENABLE = False
from typing import Iterator
@contextmanager
def disabled_cachew() -> Iterator[None]:
try:
import cachew
except ImportError:
# nothing to disable
yield
return
from cachew.extra import disabled_cachew
with disabled_cachew():
yield
def _appdirs_cache_dir() -> Path:
import appdirs # type: ignore
cd = Path(appdirs.user_cache_dir('my'))
cd.mkdir(exist_ok=True, parents=True)
return cd
def cache_dir() -> Optional[Path]:
from . import core_config as CC
return CC.config.get_cache_dir()