From a9d28df9ad346c9c99ce9d46f3df9959c5d68b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Mon, 11 Apr 2022 17:32:41 +0200 Subject: [PATCH] cache: move prune-cache function to FileCache --- frontools/cache.py | 38 ++++++++++++++++++++------------------ frontools/cli.py | 4 ++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/frontools/cache.py b/frontools/cache.py index 6d5db4c..82e928d 100644 --- a/frontools/cache.py +++ b/frontools/cache.py @@ -13,8 +13,6 @@ CacheFallback = Union[bytes, Callable[[str], Awaitable[bytes]]] class Cache(ABC): """Base class for caches""" - cache_base = xdg_cache_home() / "frontools" - @abstractmethod async def get(self, key: str, fallback: CacheFallback) -> bytes: """Get an item in the cache, call fallback if it's not present""" @@ -23,22 +21,6 @@ class Cache(ABC): def set(self, key: str, data: bytes) -> 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) -> bytes: if callable(fallback): @@ -62,6 +44,8 @@ class NullCache(Cache): class FileCache(Cache): """Cache on the local filesystem""" + cache_base = xdg_cache_home() / "frontools" + def __init__(self, name: str) -> None: self._name = name @@ -87,6 +71,24 @@ class FileCache(Cache): with open(cache_file_path, "wb") as cache_file: cache_file.write(resource) + @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 FileCache.cache_base.iterdir() if it.is_dir() + ] + for cache_name in cache_names: + cache_path: Path = FileCache.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) + def _get_cache_file_path(self, key: str) -> Path: key_slug = _get_key_slug(key) cache_directory = self.cache_base / self._name diff --git a/frontools/cli.py b/frontools/cli.py index 9157064..7bb4fbe 100644 --- a/frontools/cli.py +++ b/frontools/cli.py @@ -8,7 +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.cache import FileCache from frontools.config import Config from frontools.css import css_diff from frontools.screenshot import screenshot_diff @@ -76,7 +76,7 @@ async def main( @argument("cache_names", nargs=-1) def prune_caches(cache_names: list[str]) -> None: """Prune frontools caches""" - Cache.prune(cache_names) + FileCache.prune(cache_names) @main.command(name="css-diff")