Skip to content

Commit

Permalink
improved cache-removal and incomplete-check
Browse files Browse the repository at this point in the history
  • Loading branch information
Barakudum committed Mar 5, 2024
1 parent b82cf0c commit b7a5474
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
59 changes: 58 additions & 1 deletion src/jarklin/cache/_cache_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# -*- coding=utf-8 -*-
r"""
{gallery,video.mp4}/
├─ preview.webp
├─ animated.webp
├─ previews/
│ ├─ 1.webp
│ ├─ 2.webp
├─ meta.json
├─ {gallery,video}.type
├─ is-cache
"""
import logging
import shutil
Expand All @@ -24,11 +33,59 @@ def __init__(self, source: PathSource, dest: PathSource, config: ConfigInterface
def __repr__(self):
return f"<{type(self).__name__}: {self.source.relative_to(Path.cwd())!s}>"

@staticmethod
def remove(fp: PathSource):
fp = Path(fp)

files = [
fp/"meta.json",
fp/"preview.webp",
fp/"animated.webp",
next(fp.glob("*.type"), None),
fp/"is-cache",
]
for f in files:
if f and f.is_file():
f.unlink()

previews = fp/"previews"
if previews.is_dir():
for f in previews.glob("*.webp"):
f.unlink()
if next(previews.iterdir(), None) is None:
previews.rmdir()

if next(fp.iterdir(), None) is None:
fp.rmdir()

@staticmethod
def is_incomplete(fp: PathSource) -> bool:
fp = Path(fp)

if not fp.joinpath("meta.json").is_file():
return True
if not fp.joinpath("preview.webp").is_file():
return True
if not fp.joinpath("animated.webp").is_file():
return True
if next(fp.glob("*.type"), None) is None:
return True
if not fp.joinpath("is-cache").is_file():
return True

previews = fp/"previews"
if not previews.is_dir():
return True
if next(previews.iterdir(), None) is None:
return True

return False

@t.final
def generate(self) -> None:
logging.info(f"{self}.generate()")
if self.dest.is_dir():
shutil.rmtree(self.dest, ignore_errors=True)
CacheGenerator.remove(fp=self.dest)
self.dest.mkdir(parents=True)

try:
Expand Down
19 changes: 15 additions & 4 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, is_cache
from .util import is_video_file, is_gallery, is_deprecated, get_creation_time, get_modification_time, is_cache
try:
from better_exceptions import format_exception
except ModuleNotFoundError:
Expand Down Expand Up @@ -90,14 +90,22 @@ def iteration(self) -> None:
def invalidate(self) -> None:
logging.info("cache.invalidate()")
for root, dirnames, files in os.walk(self.jarklin_cache):
if not dirnames and not files:
os.rmdir(root)
continue

for dirname in dirnames:
dest = Path(root, dirname)
source = dest.relative_to(self.jarklin_cache)
if not is_cache(fp=dest):
continue
if not source.exists() or is_deprecated(source=source, dest=dest) or is_incomplete(dest=dest):
if (
not source.exists()
or is_deprecated(source=source, dest=dest)
or CacheGenerator.is_incomplete(fp=dest)
):
logging.info(f"removing {str(source)!r} from cache")
shutil.rmtree(dest)
CacheGenerator.remove(fp=dest)

def generate(self) -> None:
logging.info("cache.generate()")
Expand All @@ -118,7 +126,10 @@ def generate_info_file():
dest = generator.dest
logging.debug(f"Cache: adding {generator}")
try:
if is_deprecated(source=source, dest=dest) or is_incomplete(dest=dest):
if (
is_deprecated(source=source, dest=dest)
or CacheGenerator.is_incomplete(fp=dest)
):
logging.info(f"Cache: generating {generator}")
try:
generator.generate()
Expand Down
5 changes: 0 additions & 5 deletions src/jarklin/cache/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ def is_video_cache(fp: PathSource) -> bool:
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 Down

0 comments on commit b7a5474

Please sign in to comment.