reformat in output dir

This commit is contained in:
Jesse Bannon 2023-03-12 14:16:50 -07:00
parent 2f100a319d
commit 7be9d5f88d
3 changed files with 47 additions and 51 deletions

View file

@ -262,6 +262,31 @@ class SubscriptionDownload(BaseSubscription, ABC):
self._cleanup_entry_files(entry) self._cleanup_entry_files(entry)
def _process_subscription(
self,
plugins: List[Plugin],
entries: Iterable[Entry] | Iterable[Tuple[Entry, FileMetadata]],
dry_run: bool,
) -> FileHandlerTransactionLog:
for entry in entries:
entry_metadata = FileMetadata()
if isinstance(entry, tuple):
entry, entry_metadata = entry
if split_plugin := _get_split_plugin(plugins):
self._process_split_entry(
split_plugin=split_plugin, plugins=plugins, dry_run=dry_run, entry=entry
)
else:
self._process_entry(
plugins=plugins, dry_run=dry_run, entry=entry, entry_metadata=entry_metadata
)
for plugin in plugins:
plugin.post_process_subscription()
return self._enhanced_download_archive.get_file_handler_transaction_log()
def download(self, dry_run: bool = False) -> FileHandlerTransactionLog: def download(self, dry_run: bool = False) -> FileHandlerTransactionLog:
""" """
Performs the subscription download Performs the subscription download
@ -292,24 +317,11 @@ class SubscriptionDownload(BaseSubscription, ABC):
overrides=self.overrides, overrides=self.overrides,
) )
for entry in downloader.download(): return self._process_subscription(
entry_metadata = FileMetadata() plugins=plugins,
if isinstance(entry, tuple): entries=downloader.download(),
entry, entry_metadata = entry dry_run=dry_run,
)
if split_plugin := _get_split_plugin(plugins):
self._process_split_entry(
split_plugin=split_plugin, plugins=plugins, dry_run=dry_run, entry=entry
)
else:
self._process_entry(
plugins=plugins, dry_run=dry_run, entry=entry, entry_metadata=entry_metadata
)
for plugin in plugins:
plugin.post_process_subscription()
return self._enhanced_download_archive.get_file_handler_transaction_log()
def _get_entries_for_reformat( def _get_entries_for_reformat(
self, original_enhanced_download_archive: EnhancedDownloadArchive self, original_enhanced_download_archive: EnhancedDownloadArchive
@ -328,10 +340,10 @@ class SubscriptionDownload(BaseSubscription, ABC):
entry_dict=json.load(maybe_info_json), entry_dict=json.load(maybe_info_json),
working_directory=self.working_directory, working_directory=self.working_directory,
) )
except Exception: except Exception as exc:
raise ValidationException( raise ValidationException(
"info.json file cannot be loaded - subscription cannot be reformatted" "info.json file cannot be loaded - subscription cannot be reformatted"
) ) from exc
if not maybe_entry: if not maybe_entry:
raise ValidationException( raise ValidationException(
@ -356,14 +368,12 @@ class SubscriptionDownload(BaseSubscription, ABC):
yield entry yield entry
def reformat( def reformat(self, dry_run: bool = False) -> FileHandlerTransactionLog:
self, reformat_output_directory: Path, dry_run: bool = False
) -> FileHandlerTransactionLog:
original_enhanced_download_archive = self._enhanced_download_archive original_enhanced_download_archive = self._enhanced_download_archive
self._enhanced_download_archive = EnhancedDownloadArchive( self._enhanced_download_archive = EnhancedDownloadArchive(
subscription_name=self.name, subscription_name=self.name,
working_directory=self.working_directory, working_directory=self.working_directory,
output_directory=reformat_output_directory, output_directory=self.output_directory,
dry_run=dry_run, dry_run=dry_run,
) )
@ -371,23 +381,10 @@ class SubscriptionDownload(BaseSubscription, ABC):
plugins = self._initialize_plugins() plugins = self._initialize_plugins()
with self._subscription_download_context_managers(): with self._subscription_download_context_managers():
for entry in self._get_entries_for_reformat( return self._process_subscription(
original_enhanced_download_archive=original_enhanced_download_archive plugins=plugins,
): entries=self._get_entries_for_reformat(
entry_metadata = FileMetadata() original_enhanced_download_archive=original_enhanced_download_archive
if isinstance(entry, tuple): ),
entry, entry_metadata = entry dry_run=dry_run,
)
if split_plugin := _get_split_plugin(plugins):
self._process_split_entry(
split_plugin=split_plugin, plugins=plugins, dry_run=dry_run, entry=entry
)
else:
self._process_entry(
plugins=plugins, dry_run=dry_run, entry=entry, entry_metadata=entry_metadata
)
for plugin in plugins:
plugin.post_process_subscription()
return self._enhanced_download_archive.get_file_handler_transaction_log()

View file

@ -70,11 +70,13 @@ def output_directory() -> Path:
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
yield temp_dir yield temp_dir
@pytest.fixture() @pytest.fixture()
def reformat_directory() -> Path: def reformat_directory() -> Path:
with tempfile.TemporaryDirectory() as temp_dir: with tempfile.TemporaryDirectory() as temp_dir:
yield temp_dir yield temp_dir
@contextlib.contextmanager @contextlib.contextmanager
def assert_logs(logger: logging.Logger, expected_message: str, log_level: str = "debug"): def assert_logs(logger: logging.Logger, expected_message: str, log_level: str = "debug"):
""" """

View file

@ -1,10 +1,9 @@
from pathlib import Path from pathlib import Path
import pytest import pytest
from yt_dlp.utils import sanitize_filename
from expected_download import assert_expected_downloads from expected_download import assert_expected_downloads
from expected_transaction_log import assert_transaction_log_matches from expected_transaction_log import assert_transaction_log_matches
from yt_dlp.utils import sanitize_filename
from ytdl_sub.subscriptions.subscription import Subscription from ytdl_sub.subscriptions.subscription import Subscription
@ -80,16 +79,14 @@ class TestChannel:
if not reformat: if not reformat:
return return
reformat_directory = Path(reformat_directory) / sanitize_filename("Project / Zombie") full_channel_subscription.reformat(dry_run=dry_run)
full_channel_subscription.reformat(reformat_output_directory=reformat_directory, dry_run=dry_run)
assert_transaction_log_matches( assert_transaction_log_matches(
output_directory=reformat_directory, output_directory=output_directory,
transaction_log=transaction_log, transaction_log=transaction_log,
transaction_log_summary_file_name="youtube/test_channel_full.txt", transaction_log_summary_file_name="youtube/test_channel_full.txt",
) )
assert_expected_downloads( assert_expected_downloads(
output_directory=reformat_directory, output_directory=output_directory,
dry_run=dry_run, dry_run=dry_run,
expected_download_summary_file_name="youtube/test_channel_full.json", expected_download_summary_file_name="youtube/test_channel_full.json",
) )