working tests, still have more

This commit is contained in:
jbannon 2022-07-01 14:00:00 +00:00
parent dd1e6d4dd6
commit 30ad9501e6
5 changed files with 58 additions and 9 deletions

View file

@ -18,6 +18,7 @@ from ytdl_sub.downloaders.downloader import DownloaderValidator
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.plugins.plugin import Plugin from ytdl_sub.plugins.plugin import Plugin
from ytdl_sub.plugins.plugin import PluginOptions from ytdl_sub.plugins.plugin import PluginOptions
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog
from ytdl_sub.utils.thumbnail import convert_download_thumbnail from ytdl_sub.utils.thumbnail import convert_download_thumbnail
from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive
@ -226,7 +227,7 @@ class Subscription:
return plugins return plugins
def download(self, dry_run: bool = False): def download(self, dry_run: bool = False) -> FileHandlerTransactionLog:
""" """
Performs the subscription download Performs the subscription download
@ -270,6 +271,8 @@ class Subscription:
overrides=self.overrides, output_directory=self.output_directory overrides=self.overrides, output_directory=self.output_directory
) )
return self._enhanced_download_archive.get_file_handler_transaction_log()
@classmethod @classmethod
def from_preset(cls, preset: Preset, config: ConfigFile) -> "Subscription": def from_preset(cls, preset: Preset, config: ConfigFile) -> "Subscription":
""" """

View file

@ -32,11 +32,6 @@ class FileHandlerTransactionLog:
if not file_metadata: if not file_metadata:
file_metadata = FileMetadata() file_metadata = FileMetadata()
if file_name in self.files_created:
raise ValueError(
"Adding a file to the file handler transaction log that already exists"
)
self.files_created[file_name] = file_metadata self.files_created[file_name] = file_metadata
return self return self
@ -56,6 +51,15 @@ class FileHandler:
self.output_directory = output_directory self.output_directory = output_directory
self._file_handler_transaction_log = FileHandlerTransactionLog() self._file_handler_transaction_log = FileHandlerTransactionLog()
@property
def file_handler_transaction_log(self) -> FileHandlerTransactionLog:
"""
Returns
-------
Transaction logs of this file handler
"""
return self._file_handler_transaction_log
@classmethod @classmethod
def copy(cls, src_file_path: Union[str, Path], dst_file_path: Union[str, Path]): def copy(cls, src_file_path: Union[str, Path], dst_file_path: Union[str, Path]):
copyfile(src=src_file_path, dst=dst_file_path) copyfile(src=src_file_path, dst=dst_file_path)

View file

@ -14,6 +14,7 @@ from yt_dlp import DateRange
from ytdl_sub.entries.entry import Entry from ytdl_sub.entries.entry import Entry
from ytdl_sub.utils.file_handler import FileHandler from ytdl_sub.utils.file_handler import FileHandler
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog
from ytdl_sub.utils.logger import Logger from ytdl_sub.utils.logger import Logger
@ -399,7 +400,7 @@ class EnhancedDownloadArchive:
return f".ytdl-sub-{self.subscription_name}-download-archive.json" return f".ytdl-sub-{self.subscription_name}-download-archive.json"
@property @property
def _mapping_output_file_path(self): def _mapping_output_file_path(self) -> str:
""" """
Returns Returns
------- -------
@ -407,6 +408,15 @@ class EnhancedDownloadArchive:
""" """
return str(Path(self.output_directory) / self._mapping_file_name) return str(Path(self.output_directory) / self._mapping_file_name)
@property
def _mapping_working_file_path(self) -> str:
"""
Returns
-------
The download mapping's file path in the working directory.
"""
return str(Path(self.working_directory) / self._mapping_file_name)
@property @property
def _archive_working_file_path(self) -> str: def _archive_working_file_path(self) -> str:
""" """
@ -524,7 +534,10 @@ class EnhancedDownloadArchive:
download_archive.remove_entry(entry_id) download_archive.remove_entry(entry_id)
# Save the updated mapping file to the output directory # Save the updated mapping file to the output directory
self._download_mapping.to_file(output_json_file=self._mapping_output_file_path) # TODO: Make this cleaner. It writes the file to the working dir, the copies it to the
# output dir. Should be just a single write
self._download_mapping.to_file(output_json_file=self._mapping_working_file_path)
self.save_file(file_name=self._mapping_file_name, output_file_name=self._mapping_file_name)
return self return self
@ -547,3 +560,6 @@ class EnhancedDownloadArchive:
self._file_handler.copy_file_to_output_directory( self._file_handler.copy_file_to_output_directory(
file_name=file_name, output_file_name=output_file_name file_name=file_name, output_file_name=output_file_name
) )
def get_file_handler_transaction_log(self) -> FileHandlerTransactionLog:
return self._file_handler.file_handler_transaction_log

View file

@ -6,6 +6,8 @@ from typing import List
from typing import Optional from typing import Optional
from typing import Union from typing import Union
from ytdl_sub.utils.file_handler import FileHandlerTransactionLog
class ExpectedDownload: class ExpectedDownload:
""" """
@ -58,3 +60,13 @@ class ExpectedDownload:
f"MD5 hash for {str(relative_path)} does not match: " f"MD5 hash for {str(relative_path)} does not match: "
f"{md5_hash} != {expected_md5_hash}" f"{md5_hash} != {expected_md5_hash}"
) )
def assert_dry_run_files_logged(self, transaction_log: FileHandlerTransactionLog):
assert (
len(transaction_log.files_created) == self.file_count
), "Mismatch in number of created files"
for relative_path in self.expected_md5_file_hashes.keys():
assert (
str(relative_path) in transaction_log.files_created
), f"Expected {str(relative_path)} to be a file but it is not"

View file

@ -148,8 +148,22 @@ class TestPlaylistAsKodiMusicVideo:
playlist_subscription.download() playlist_subscription.download()
expected_playlist_download.assert_files_exist(relative_directory=output_directory) expected_playlist_download.assert_files_exist(relative_directory=output_directory)
def test_playlist_dry_run(
self, playlist_subscription, expected_playlist_download, output_directory
):
file_transaction_log = playlist_subscription.download(dry_run=True)
expected_playlist_download.assert_dry_run_files_logged(transaction_log=file_transaction_log)
def test_single_video_download( def test_single_video_download(
self, single_video_subscription, expected_single_video_download, output_directory self, single_video_subscription, expected_single_video_download, output_directory
): ):
single_video_subscription.download(dry_run=True) single_video_subscription.download()
expected_single_video_download.assert_files_exist(relative_directory=output_directory) expected_single_video_download.assert_files_exist(relative_directory=output_directory)
def test_single_video_dry_run(
self, single_video_subscription, expected_single_video_download, output_directory
):
file_transaction_log = single_video_subscription.download(dry_run=True)
expected_single_video_download.assert_dry_run_files_logged(
transaction_log=file_transaction_log
)