From 6818f9b0e855e530848db79dae19c9a57eaac04c Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Wed, 25 Jan 2023 13:43:09 +0300 Subject: [PATCH] handle watchdog's file created event using the on_modified handler + move processing thumbnails and album colors to the populate class + move processing artist colors behind the populate call in run_periodic_checks --- app/functions.py | 6 ++---- app/lib/populate.py | 4 ++++ app/lib/watchdogg.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/app/functions.py b/app/functions.py index 2140550..375b1c1 100644 --- a/app/functions.py +++ b/app/functions.py @@ -28,10 +28,6 @@ def run_periodic_checks(): except PopulateCancelledError: pass - ProcessTrackThumbnails() - ProcessAlbumColors() - ProcessArtistColors() - if utils.Ping()(): try: CheckArtistImages() @@ -40,4 +36,6 @@ def run_periodic_checks(): "Internet connection lost. Downloading artist images stopped." ) + ProcessArtistColors() + time.sleep(300) diff --git a/app/lib/populate.py b/app/lib/populate.py index 9e657dc..9284961 100644 --- a/app/lib/populate.py +++ b/app/lib/populate.py @@ -7,6 +7,7 @@ from app.db.sqlite.tracks import SQLiteTrackMethods from app.db.sqlite.settings import SettingsSQLMethods as sdb from app.db.sqlite.favorite import SQLiteFavoriteMethods as favdb from app.db.store import Store +from app.lib.colorlib import ProcessAlbumColors from app.lib.taglib import extract_thumb, get_tags from app.logger import log @@ -65,6 +66,9 @@ class Populate: self.tag_untagged(untagged, key) + ProcessTrackThumbnails() + ProcessAlbumColors() + @staticmethod def filter_untagged(tracks: list[Track], files: list[str]): tagged_files = [t.filepath for t in tracks] diff --git a/app/lib/watchdogg.py b/app/lib/watchdogg.py index 27ffa7c..57875e7 100644 --- a/app/lib/watchdogg.py +++ b/app/lib/watchdogg.py @@ -40,6 +40,8 @@ class Watcher: while trials < 10: try: dirs = sdb.get_root_dirs() + dirs = [rf"{d}" for d in dirs] + dir_map = [ {"original": d, "realpath": os.path.realpath(d)} for d in dirs ] @@ -59,7 +61,7 @@ class Watcher: ) return - dir_map = [d for d in dir_map if d['realpath'] != d['original']] + dir_map = [d for d in dir_map if d["realpath"] != d["original"]] if len(dirs) > 0 and dirs[0] == "$home": dirs = [settings.USER_HOME_DIR] @@ -179,6 +181,8 @@ def remove_track(filepath: str) -> None: class Handler(PatternMatchingEventHandler): files_to_process = [] + files_to_process_windows = [] + root_dirs = [] dir_map = [] @@ -208,6 +212,7 @@ class Handler(PatternMatchingEventHandler): Fired when a supported file is created. """ self.files_to_process.append(event.src_path) + self.files_to_process_windows.append(event.src_path) def on_deleted(self, event): """ @@ -240,6 +245,7 @@ class Handler(PatternMatchingEventHandler): def on_closed(self, event): """ Fired when a created file is closed. + NOT FIRED IN WINDOWS """ try: self.files_to_process.remove(event.src_path) @@ -248,3 +254,27 @@ class Handler(PatternMatchingEventHandler): add_track(path) except ValueError: pass + + def on_modified(self, event): + # this event handler is triggered twice on windows + # for copy events. We need to test how this behaves in + # Linux. + + if event.src_path not in self.files_to_process_windows: + return + + file_size = -1 + + while file_size != os.path.getsize(event.src_path): + file_size = os.path.getsize(event.src_path) + time.sleep(0.1) + + try: + os.rename(event.src_path, event.src_path) + path = self.get_abs_path(event.src_path) + remove_track(path) + add_track(path) + self.files_to_process_windows.remove(event.src_path) + except OSError: + print("File is locked, skipping") + pass