cache: add prune cache command
This commit is contained in:
parent
9ef40a3a12
commit
b22fe14751
|
@ -4,6 +4,8 @@ from pathlib import Path
|
|||
from pickle import dumps, loads
|
||||
from typing import Awaitable, Callable, Generic, TypeVar, Union, cast
|
||||
|
||||
from click import echo
|
||||
from shutil import rmtree
|
||||
from xdg import xdg_cache_home
|
||||
|
||||
ResourceType = TypeVar("ResourceType")
|
||||
|
@ -14,6 +16,8 @@ CacheFallback = Union[ResourceType, Callable[[str], Awaitable[ResourceType]]]
|
|||
class Cache(Generic[ResourceType], ABC):
|
||||
"""Base class for caches"""
|
||||
|
||||
cache_base = xdg_cache_home() / "frontools"
|
||||
|
||||
@abstractmethod
|
||||
async def get(
|
||||
self, key: str, fallback: CacheFallback[ResourceType]
|
||||
|
@ -24,6 +28,22 @@ class Cache(Generic[ResourceType], ABC):
|
|||
def set(self, key: str, resource: ResourceType) -> None:
|
||||
"""Set a resource in the cache"""
|
||||
|
||||
@staticmethod
|
||||
def prune(cache_names: list[str]) -> None:
|
||||
"""Remove caches from filesystem.
|
||||
|
||||
If empty list is provided, all caches will be cleaned
|
||||
"""
|
||||
if not len(cache_names):
|
||||
cache_names = [it.name for it in Cache.cache_base.iterdir() if it.is_dir()]
|
||||
for cache_name in cache_names:
|
||||
cache_path: Path = Cache.cache_base / cache_name
|
||||
if not cache_path.is_dir():
|
||||
echo(f'{cache_path} isn\'t a chache directory', err=True)
|
||||
continue
|
||||
echo(f'Removing {cache_path}')
|
||||
rmtree(cache_path)
|
||||
|
||||
@staticmethod
|
||||
async def _get_fallback_value(
|
||||
key: str, fallback: CacheFallback[ResourceType]
|
||||
|
@ -78,7 +98,7 @@ class FileCache(Cache[ResourceType]):
|
|||
|
||||
def _get_cache_file_path(self, key: str) -> Path:
|
||||
key_slug = _get_key_slug(key)
|
||||
cache_directory = xdg_cache_home() / "frontools" / self._name
|
||||
cache_directory = self.cache_base / self._name
|
||||
file_path = cache_directory.joinpath(*key_slug.split("&"))
|
||||
file_directory = file_path.parent
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ from click import Context as ClickContext
|
|||
from click import Path as PathArgument
|
||||
from click import argument, group, option, pass_context, pass_obj
|
||||
|
||||
from frontools.cache import Cache
|
||||
from frontools.config import Config
|
||||
from frontools.css import css_diff
|
||||
from frontools.screenshot import screenshot_diff
|
||||
|
@ -48,6 +49,13 @@ async def main(
|
|||
ctx.obj = await Config.load(config_file, source, not no_cache)
|
||||
|
||||
|
||||
@main.command(name="prune-caches")
|
||||
@argument("cache_names", nargs=-1)
|
||||
def prune_caches(cache_names: list[str]) -> None:
|
||||
"""Prune frontools caches"""
|
||||
Cache.prune(cache_names)
|
||||
|
||||
|
||||
@main.command(name="css-diff")
|
||||
@argument("right_source", type=str)
|
||||
@pass_obj
|
||||
|
|
Loading…
Reference in New Issue