Skip to content

Commit

Permalink
added some logging and better cache-identification via is-cache file
Browse files Browse the repository at this point in the history
  • Loading branch information
Barakudum committed Mar 4, 2024
1 parent 9ecee5c commit 1b5efd7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/jarklin/cache/_cache_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def generate(self) -> None:
self.dest.mkdir(parents=True)

try:
logging.info(f"{self}.mark_cache()")
self.mark_cache()
logging.info(f"{self}.generate_meta()")
self.generate_meta()
logging.info(f"{self}.generate_previews()")
Expand All @@ -48,6 +50,10 @@ def generate(self) -> None:
shutil.rmtree(self.dest, ignore_errors=True)
raise

@t.final
def mark_cache(self):
self.dest.joinpath("is-cache").touch()

@abstractmethod
def generate_meta(self) -> None: ...

Expand Down
7 changes: 4 additions & 3 deletions src/jarklin/cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ._cache_generator import CacheGenerator
from .video import VideoCacheGenerator
from .gallery import GalleryCacheGenerator
from .util import is_video_file, is_gallery, is_deprecated, is_incomplete, get_creation_time, get_modification_time
from .util import is_video_file, is_gallery, is_deprecated, is_incomplete, get_creation_time, get_modification_time, is_cache
try:
from better_exceptions import format_exception
except ModuleNotFoundError:
Expand Down Expand Up @@ -93,9 +93,10 @@ def invalidate(self) -> None:
for dirname in dirnames:
dest = Path(root, dirname)
source = dest.relative_to(self.jarklin_cache)
if not dest.joinpath("meta.json").is_file():
if not is_cache(fp=dest):
continue
if not source.exists() or is_deprecated(source=source, dest=dest):
if not source.exists() or is_deprecated(source=source, dest=dest) or is_incomplete(dest=dest):
logging.info(f"removing {str(source)!r} from cache")
shutil.rmtree(dest)

def generate(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions src/jarklin/cache/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
│ ├─ 2.webp
├─ meta.json
├─ gallery.type
├─ is-cache
"""
import re
import shutil
Expand Down
25 changes: 20 additions & 5 deletions src/jarklin/cache/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def is_gallery(fp: PathSource, boundary: int = 5) -> bool:
]) > boundary


def is_cache(fp: PathSource) -> bool:
fp = Path(fp)
return fp.joinpath("is-cache").is_file()


def is_gallery_cache(fp: PathSource) -> bool:
fp = Path(fp)
return is_cache(fp) and fp.joinpath("gallery.type").is_file()


def is_video_cache(fp: PathSource) -> bool:
fp = Path(fp)
return is_cache(fp) and fp.joinpath("video.type").is_file()


def is_incomplete(dest: PathSource) -> bool:
dest = Path(dest)
return next(dest.glob("*.type"), None) is None


def is_deprecated(source: PathSource, dest: PathSource) -> bool:
source = Path(source)
dest = Path(dest)
Expand All @@ -59,11 +79,6 @@ def is_deprecated(source: PathSource, dest: PathSource) -> bool:
return source_mtime > p.getmtime(dest)


def is_incomplete(dest: PathSource) -> bool:
dest = Path(dest)
return next(dest.glob("*.type"), None) is None


def get_creation_time(path: PathSource) -> float:
# fixme: ctime != creation-time on unix
path = Path(path)
Expand Down
1 change: 1 addition & 0 deletions src/jarklin/cache/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
│ ├─ 2.webp
├─ meta.json
├─ video.type
├─ is-cache
"""
import logging
import shutil
Expand Down

0 comments on commit 1b5efd7

Please sign in to comment.