From 34a9541d1907b427c6c19c5953d941283f76b91c Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Sat, 17 Sep 2022 10:33:12 -0700 Subject: [PATCH] output using last entry --- src/ytdl_sub/plugins/nfo_tags.py | 10 +++++-- .../plugins/output_directory_nfo_tags.py | 28 +++++++++++++++++-- src/ytdl_sub/plugins/plugin.py | 2 -- tests/e2e/plugins/test_nfo_tags.py | 27 ------------------ .../plugins/date_range/no_downloads.json | 3 +- tests/unit/config/test_preset.py | 2 +- 6 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/ytdl_sub/plugins/nfo_tags.py b/src/ytdl_sub/plugins/nfo_tags.py index d37355db..7e13509d 100644 --- a/src/ytdl_sub/plugins/nfo_tags.py +++ b/src/ytdl_sub/plugins/nfo_tags.py @@ -74,7 +74,7 @@ class SharedNfoTagsPlugin(Plugin[SharedNfoTagsOptions], ABC): Shared code between NFO tags and Ouptut Directory NFO Tags """ - def _get_xml_element_dict(self, entry: Optional[Entry]) -> Dict[str, List[XmlElement]]: + def _get_xml_element_dict(self, entry: Entry) -> Dict[str, List[XmlElement]]: nfo_tags: Dict[str, List[XmlElement]] = defaultdict(list) for key, string_tags in self.plugin_options.tags.string_tags.items(): @@ -102,7 +102,7 @@ class SharedNfoTagsPlugin(Plugin[SharedNfoTagsOptions], ABC): return nfo_tags - def _create_nfo(self, entry: Optional[Entry] = None) -> None: + def _create_nfo(self, entry: Entry, save_to_entry: bool = True) -> None: # Write the nfo tags to XML with the nfo_root nfo_root = self.overrides.apply_formatter( formatter=self.plugin_options.nfo_root, entry=entry @@ -148,7 +148,11 @@ class SharedNfoTagsPlugin(Plugin[SharedNfoTagsOptions], ABC): }, title="NFO tags", ) - self.save_file(file_name=nfo_file_name, file_metadata=nfo_metadata, entry=entry) + + if save_to_entry: + self.save_file(file_name=nfo_file_name, file_metadata=nfo_metadata, entry=entry) + else: + self.save_file(file_name=nfo_file_name, file_metadata=nfo_metadata) class NfoTagsOptions(SharedNfoTagsOptions): diff --git a/src/ytdl_sub/plugins/output_directory_nfo_tags.py b/src/ytdl_sub/plugins/output_directory_nfo_tags.py index 0ff67c33..4e1dda3b 100644 --- a/src/ytdl_sub/plugins/output_directory_nfo_tags.py +++ b/src/ytdl_sub/plugins/output_directory_nfo_tags.py @@ -1,13 +1,19 @@ +from typing import Optional + +from ytdl_sub.config.preset_options import Overrides +from ytdl_sub.entries.entry import Entry from ytdl_sub.plugins.nfo_tags import NfoTagsValidator from ytdl_sub.plugins.nfo_tags import SharedNfoTagsOptions from ytdl_sub.plugins.nfo_tags import SharedNfoTagsPlugin from ytdl_sub.validators.string_formatter_validators import StringFormatterValidator +from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadArchive class OutputDirectoryNfoTagsOptions(SharedNfoTagsOptions): """ Adds a single NFO file in the output directory. An NFO file is simply an XML file with a - ``.nfo`` extension. You can add any strings or override variables into this NFO. + ``.nfo`` extension. It uses the last entry's source variables which can change per download + invocation. Be cautious of which variables you use. Usage: @@ -77,8 +83,24 @@ class OutputDirectoryNfoTagsOptions(SharedNfoTagsOptions): class OutputDirectoryNfoTagsPlugin(SharedNfoTagsPlugin): plugin_options_type = OutputDirectoryNfoTagsOptions + def __init__( + self, + plugin_options: OutputDirectoryNfoTagsOptions, + overrides: Overrides, + enhanced_download_archive: EnhancedDownloadArchive, + ): + super().__init__(plugin_options, overrides, enhanced_download_archive) + self._last_entry: Optional[Entry] = None + + def post_process_entry(self, entry: Entry) -> None: + """ + Tracks the last entry processed + """ + self._last_entry = entry + def post_process_subscription(self): """ - Creates an NFO file in the root of the output directory + Creates an NFO file in the root of the output directory using the last entry """ - self._create_nfo() + if self._last_entry: + self._create_nfo(entry=self._last_entry, save_to_entry=False) diff --git a/src/ytdl_sub/plugins/plugin.py b/src/ytdl_sub/plugins/plugin.py index 7ee6d8a0..b1ecf705 100644 --- a/src/ytdl_sub/plugins/plugin.py +++ b/src/ytdl_sub/plugins/plugin.py @@ -6,7 +6,6 @@ from typing import Optional from typing import Tuple from typing import Type from typing import TypeVar -from typing import final from ytdl_sub.config.preset_options import AddsVariablesMixin from ytdl_sub.config.preset_options import Overrides @@ -60,7 +59,6 @@ class Plugin(DownloadArchiver, Generic[PluginOptionsT], ABC): # If the plugin creates multile entries from a single entry is_split_plugin: bool = False - @final def __init__( self, plugin_options: PluginOptionsT, diff --git a/tests/e2e/plugins/test_nfo_tags.py b/tests/e2e/plugins/test_nfo_tags.py index a3260f8c..4c95122c 100644 --- a/tests/e2e/plugins/test_nfo_tags.py +++ b/tests/e2e/plugins/test_nfo_tags.py @@ -83,30 +83,3 @@ class TestNfoTagsPlugins: transaction_log=transaction_log, transaction_log_summary_file_name=f"plugins/nfo_tags/{transaction_log_file_name}", ) - - def test_source_variable_in_output_directory_nfo_tags_errors( - self, subscription_dict, music_video_config - ): - subscription_dict["output_directory_nfo_tags"]["tags"]["kodi_safe_title_with_attrs"][ - "attributes" - ]["tag"] = "{title}" - with pytest.raises(ValidationException): - Subscription.from_dict( - config=music_video_config, - preset_name="kodi_safe_xml", - preset_dict=subscription_dict, - ) - - def test_source_variable_in_output_directory_nfo_tags_list_errors( - self, subscription_dict, music_video_config - ): - subscription_dict["output_directory_nfo_tags"]["tags"]["kodi_safe_title_with_attrs"] = [ - "okay value", - "not okay {title}", - ] - with pytest.raises(ValidationException): - Subscription.from_dict( - config=music_video_config, - preset_name="kodi_safe_xml", - preset_dict=subscription_dict, - ) diff --git a/tests/e2e/resources/expected_downloads_summaries/plugins/date_range/no_downloads.json b/tests/e2e/resources/expected_downloads_summaries/plugins/date_range/no_downloads.json index 8172fc7b..ccda5535 100644 --- a/tests/e2e/resources/expected_downloads_summaries/plugins/date_range/no_downloads.json +++ b/tests/e2e/resources/expected_downloads_summaries/plugins/date_range/no_downloads.json @@ -1,6 +1,5 @@ { ".ytdl-sub-recent-download-archive.json": "99914b932bd37a50b983c5e7c90ae93b", "fanart.jpg": "129c6639b47299bc48062f0365e670ee", - "poster.jpg": "5de28eea5a921a041452ab3ce1041f73", - "tvshow.nfo": "c1c888ff6691f36328d1fb9d8c43adff" + "poster.jpg": "5de28eea5a921a041452ab3ce1041f73" } \ No newline at end of file diff --git a/tests/unit/config/test_preset.py b/tests/unit/config/test_preset.py index 643f1284..bb1d164e 100644 --- a/tests/unit/config/test_preset.py +++ b/tests/unit/config/test_preset.py @@ -230,7 +230,7 @@ class TestPreset: ): with pytest.raises( StringFormattingVariableNotFoundException, - match="Override variable 'dne_var' does not exist", + match="Format variable 'dne_var' does not exist", ): _ = Preset( config=config_file,