From 959c5dfc9d73d89b53f3fe1435c5abbdd514bbe8 Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Wed, 30 Nov 2022 00:20:48 -0800 Subject: [PATCH] working directory getting cleaned --- src/ytdl_sub/plugins/subtitles.py | 14 ++++++++------ .../subscriptions/subscription_download.py | 13 ++++++++++--- tests/unit/prebuilt_presets/conftest.py | 6 ++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/ytdl_sub/plugins/subtitles.py b/src/ytdl_sub/plugins/subtitles.py index 0ea85244..8fefa973 100644 --- a/src/ytdl_sub/plugins/subtitles.py +++ b/src/ytdl_sub/plugins/subtitles.py @@ -208,11 +208,13 @@ class SubtitlesPlugin(Plugin[SubtitleOptions]): entry=entry, ) - # Delete any possible original subtitle files before conversion - for possible_ext in SUBTITLE_EXTENSIONS: - possible_subs_file = ( - Path(self.working_directory) / f"{entry.uid}.{lang}.{possible_ext}" - ) - FileHandler.delete(possible_subs_file) + # Delete any possible original subtitle files before conversion + # Can happen for both file and embedded subs + for lang in langs: + for possible_ext in SUBTITLE_EXTENSIONS: + possible_subs_file = ( + Path(self.working_directory) / f"{entry.uid}.{lang}.{possible_ext}" + ) + FileHandler.delete(possible_subs_file) return file_metadata diff --git a/src/ytdl_sub/subscriptions/subscription_download.py b/src/ytdl_sub/subscriptions/subscription_download.py index 58e65871..ef5e4b5f 100644 --- a/src/ytdl_sub/subscriptions/subscription_download.py +++ b/src/ytdl_sub/subscriptions/subscription_download.py @@ -166,6 +166,12 @@ class SubscriptionDownload(BaseSubscription, ABC): return plugins + @classmethod + def _cleanup_entry_files(cls, entry: Entry): + FileHandler.delete(entry.get_download_file_path()) + FileHandler.delete(entry.get_download_thumbnail_path()) + FileHandler.delete(entry.get_download_info_json_path()) + def _post_process_entry( self, plugins: List[Plugin], dry_run: bool, entry: Entry, entry_metadata: FileMetadata ): @@ -184,6 +190,9 @@ class SubscriptionDownload(BaseSubscription, ABC): if self.maintain_download_archive: self._enhanced_download_archive.save_download_mappings() + # Clean up any files that remain + self._cleanup_entry_files(entry=entry) + def _process_entry( self, plugins: List[Plugin], dry_run: bool, entry: Entry, entry_metadata: FileMetadata ) -> None: @@ -278,9 +287,7 @@ class SubscriptionDownload(BaseSubscription, ABC): plugins=plugins, dry_run=dry_run, entry=entry, entry_metadata=entry_metadata ) - FileHandler.delete(entry.get_download_file_path()) - FileHandler.delete(entry.get_download_thumbnail_path()) - FileHandler.delete(entry.get_download_info_json_path()) + self._cleanup_entry_files(entry=entry) for plugin in plugins: plugin.post_process_subscription() diff --git a/tests/unit/prebuilt_presets/conftest.py b/tests/unit/prebuilt_presets/conftest.py index 7ce3d4e8..7822681c 100644 --- a/tests/unit/prebuilt_presets/conftest.py +++ b/tests/unit/prebuilt_presets/conftest.py @@ -24,6 +24,12 @@ from ytdl_sub.entries.variables.kwargs import UPLOAD_DATE from ytdl_sub.entries.variables.kwargs import WEBPAGE_URL +@pytest.fixture +def working_directory() -> str: + with tempfile.TemporaryDirectory() as temp_dir: + yield temp_dir + + @pytest.fixture def subscription_name(working_directory) -> str: name = "subscription_test"