Skip to content

Commit

Permalink
Caching and bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgiest05 committed May 14, 2024
1 parent 07b58ab commit 8734623
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ PySide6_Addons>=6.5.1.1,<=6.6.3.1
PySide6_Essentials>=6.5.1.1,<=6.6.3.1
typing_extensions>=3.10.0.0,<=4.11.0
ujson>=5.8.0,<=5.9.0
watchdog>=4.0.0
watchdog>=4.0.0
cachetools>=5.3.3
87 changes: 45 additions & 42 deletions tagstudio/src/core/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
import xml.etree.ElementTree as ET
from enum import Enum
import ujson
from cachetools import cached, LRUCache
from watchdog.observers import Observer
from watchdog.events import DirModifiedEvent, FileSystemEventHandler
from watchdog.events import FileSystemEvent, DirModifiedEvent, FileSystemEventHandler

from src.core.json_typing import JsonCollation, JsonEntry, JsonLibary, JsonTag
from src.core import ts_core
Expand Down Expand Up @@ -314,6 +315,48 @@ def compressed_dict(self) -> JsonCollation:
return obj


class IsDirModifiedHandler(FileSystemEventHandler):
def __init__(self, lib: "Library", callback: Callable[[], None]) -> None:
self.library: Library = lib
self.callback: Callable[[], None] = callback
super().__init__()

@cached(cache=LRUCache(maxsize=32), key=lambda _, path, time: (path, time))
def event_helper(self, filepath: str, time: int) -> None:
_ = time
# Here update internal vars with single new file
if "$RECYCLE.BIN" in filepath or "tagstudio_thumbs" in filepath:
return

assert not os.path.isdir(filepath)

if (
os.path.splitext(filepath)[1][1:].lower()
not in self.library.ignored_extensions
):
self.library.dir_file_count += 1
file = os.path.relpath(filepath, self.library.library_dir)

if os.name == "nt":
id = self.library.filename_to_entry_id_map.get(file.lower())
else:
id = self.library.filename_to_entry_id_map.get(file)

if id is None:
self.library.files_not_in_library.append(file)
new_ids = self.library.add_new_files_as_entries()
assert len(new_ids) == 1
id = new_ids[0]

self.callback()

def on_any_event(self, event: FileSystemEvent) -> None:
filepath = event.dest_path if event.dest_path else event.src_path
now = round(time.time())
if not event.is_directory:
self.event_helper(filepath, now)


class Library:
"""Class for the Library object, and all CRUD operations made upon it."""

Expand Down Expand Up @@ -956,48 +999,8 @@ def refresh_dir(self):

def refresh_on_changes(self, callback: Callable[[], None]) -> None:
"""Activates the automatic refreshing of the Library on filesystem modifications"""

class IsDirModifiedHandler(FileSystemEventHandler):
def __init__(self, lib: Library) -> None:
self.library: Library = lib
self.file_cache: dict[str, int] = {} # TODO: Cache invalidation
super().__init__()

def on_any_event(self, event):
filepath = event.dest_path if event.dest_path else event.src_path
now = time.time()
if not event.is_directory and round(now) != self.file_cache.get(
filepath, 0
):
# Update cache
self.file_cache[filepath] = round(now)

# Here update internal vars with single file
if "$RECYCLE.BIN" in filepath or "tagstudio_thumbs" in filepath:
return

if (
os.path.splitext(filepath)[1][1:].lower()
not in self.library.ignored_extensions
):
self.library.dir_file_count += 1
file = os.path.relpath(filepath, self.library.library_dir)

if os.name == "nt":
id = self.library.filename_to_entry_id_map.get(file.lower())
else:
id = self.library.filename_to_entry_id_map.get(file)

if id is None:
self.library.files_not_in_library.append(file)
new_ids = self.library.add_new_files_as_entries()
assert len(new_ids) == 1
id = new_ids[0]

callback() # TODO: Maybe wait for x time?

self.observer.schedule(
IsDirModifiedHandler(self), path=self.library_dir, recursive=True
IsDirModifiedHandler(self, callback), path=self.library_dir, recursive=True
)
self.observer.start()

Expand Down

0 comments on commit 8734623

Please sign in to comment.