From 64f5dca9b0556c5a257cb6b453c760a821826f9b Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 1 Aug 2023 20:49:30 +0200 Subject: [PATCH] delete remove content items: - this was missing the whole time, I just noticed, that removed items aren't removed - this now removes not present items --- src/content/base_class.py | 29 +++++++++++++++++++++++++++-- src/content/episode_content.py | 2 ++ src/content/general.py | 11 +++++++++++ src/main.py | 1 + 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/content/base_class.py b/src/content/base_class.py index 3c47e6e..4fcc7f1 100644 --- a/src/content/base_class.py +++ b/src/content/base_class.py @@ -21,6 +21,7 @@ ScannedFile, ScannedFileType, Summary, + safe_index, ) ContentCharacteristic = tuple[Optional[ContentType], ScannedFileType] @@ -136,13 +137,14 @@ def process_folder( results.append(result) value = (parent_type, ScannedFileType.folder) - callback.finish(directory.name, parent_folders, value) + callback.finish(directory.name, parent_folders, 0, value) return results already_scanned_file_paths: list[Path] = [ content.scanned_file.path for content in rescan ] + scanned_file_registry: list[bool] = [False for _ in rescan] for file_path, file_type, parent_folders in temp: is_rescan = ( @@ -168,7 +170,30 @@ def process_folder( if result is not None and is_rescan is None: rescan.append(result) + if is_rescan is not None: + idx = already_scanned_file_paths.index(file_path) + scanned_file_registry[idx] = True + + deleted: int = 0 + for path, was_found in zip( + already_scanned_file_paths, + scanned_file_registry, + strict=True, + ): + if was_found: + continue + + index: Optional[int] = safe_index( + [content.scanned_file.path for content in rescan], + path, + ) + if index is None: + raise RuntimeError(f"Path to delete wasn't founds: {path}") + + del rescan[index] + deleted += 1 + value = (parent_type, ScannedFileType.folder) - callback.finish(directory.name, parent_folders, value) + callback.finish(directory.name, parent_folders, deleted, value) return rescan diff --git a/src/content/episode_content.py b/src/content/episode_content.py index 7a069c9..24a9592 100644 --- a/src/content/episode_content.py +++ b/src/content/episode_content.py @@ -166,6 +166,7 @@ def scan( callback.finish( self.scanned_file.path.name, self.scanned_file.parents, + 0, characteristic, ) @@ -200,6 +201,7 @@ def scan( callback.finish( self.scanned_file.path.name, self.scanned_file.parents, + 0, characteristic, ) diff --git a/src/content/general.py b/src/content/general.py index bb71e23..2a53249 100644 --- a/src/content/general.py +++ b/src/content/general.py @@ -486,9 +486,20 @@ def finish( self: Self, name: str, # noqa: ARG002 parent_folders: list[str], # noqa: ARG002 + deleted: int, # noqa: ARG002 characteristic: CT, # noqa: ARG002 ) -> None: return None def get_saved(self: Self) -> RT: raise MissingOverrideError + + +SF = TypeVar("SF") + + +def safe_index(ls: list[SF], item: SF) -> Optional[int]: + try: + return ls.index(item) + except ValueError: + return None diff --git a/src/main.py b/src/main.py index 87bd596..f451f0a 100644 --- a/src/main.py +++ b/src/main.py @@ -176,6 +176,7 @@ def finish( self: Self, name: str, parent_folders: list[str], + deleted: int, characteristic: ContentCharacteristic, ) -> None: if self.__progress_bars.get(name) is None: