diff --git a/docs/config.rst b/docs/config.rst index 94ea50d..0725a0e 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -318,6 +318,12 @@ Source Variables :inherited-members: :undoc-members: +Override Variables +------------------ + +.. autoclass:: ytdl_sub.config.preset_options.OverridesVariables() + :members: + ------------------------------------------------------------------------------- Config Types diff --git a/src/ytdl_sub/config/preset_options.py b/src/ytdl_sub/config/preset_options.py index 21839ee..ca5b827 100644 --- a/src/ytdl_sub/config/preset_options.py +++ b/src/ytdl_sub/config/preset_options.py @@ -110,9 +110,45 @@ class YTDLOptions(LiteralDictValidator): """ +class OverridesVariables(DictFormatterValidator): + """ + Override variables that are automatically added to every subscription. + """ + + def _add_override_variable(self, key_name: str, format_string: str, sanitize: bool = False): + if sanitize: + key_name = f"{key_name}_sanitized" + format_string = sanitize_filename(format_string) + + self._value[key_name] = StringFormatterValidator( + name="__should_never_fail__", + value=format_string, + ) + + def __init__(self, name, value): + super().__init__(name, value) + + # Add sanitized and non-sanitized override variables + for sanitize in [True, False]: + self._add_override_variable( + key_name="subscription_name", + format_string=self.subscription_name, + sanitize=sanitize, + ) + + @property + def subscription_name(self) -> str: + """ + Returns + ------- + Name of the subscription + """ + return self._root_name + + # Disable for proper docstring formatting # pylint: disable=line-too-long -class Overrides(DictFormatterValidator): +class Overrides(OverridesVariables): """ Optional. This section allows you to define variables that can be used in any string formatter. For example, if you want your file and thumbnail files to match without copy-pasting a large @@ -142,16 +178,6 @@ class Overrides(DictFormatterValidator): # pylint: enable=line-too-long - def _add_override_variable(self, key_name: str, format_string: str, sanitize: bool = False): - if sanitize: - key_name = f"{key_name}_sanitized" - format_string = sanitize_filename(format_string) - - self._value[key_name] = StringFormatterValidator( - name="__should_never_fail__", - value=format_string, - ) - def __init__(self, name, value): super().__init__(name, value) diff --git a/src/ytdl_sub/validators/validators.py b/src/ytdl_sub/validators/validators.py index 784442e..97e5941 100644 --- a/src/ytdl_sub/validators/validators.py +++ b/src/ytdl_sub/validators/validators.py @@ -179,6 +179,16 @@ class DictValidator(Validator): super().__init__(name, value) self.__validator_dict: Dict[str, Validator] = {} + @final + @property + def _root_name(self) -> str: + """ + Returns + ------- + "first" from the first.element.of.the.name + """ + return self._name.split(".")[0] + @final @property def _dict(self) -> dict: diff --git a/tests/e2e/youtube/test_video.py b/tests/e2e/youtube/test_video.py index 89229ca..e7a1f2f 100644 --- a/tests/e2e/youtube/test_video.py +++ b/tests/e2e/youtube/test_video.py @@ -3,7 +3,6 @@ from conftest import preset_dict_to_dl_args from e2e.conftest import mock_run_from_cli from expected_download import assert_expected_downloads from expected_transaction_log import assert_transaction_log_matches -from mergedeep import mergedeep from ytdl_sub.subscriptions.subscription import Subscription @@ -49,6 +48,13 @@ def single_tv_show_video_nulled_values_preset_dict(output_directory): "format": "worst[ext=mp4]", "max_downloads": 2, }, + # test override variables added by ytdl-sub + "nfo_tags": { + "tags": { + "subscription_name": "{subscription_name}", + "subscription_name_sanitized": "{subscription_name_sanitized}", + } + }, "overrides": { "url": "https://www.youtube.com/@ProjectZombie603", "tv_show_name": "Project Zombie", diff --git a/tests/resources/expected_downloads_summaries/youtube/test_video.json b/tests/resources/expected_downloads_summaries/youtube/test_video.json index 5b78ad1..c94d3dd 100644 --- a/tests/resources/expected_downloads_summaries/youtube/test_video.json +++ b/tests/resources/expected_downloads_summaries/youtube/test_video.json @@ -1,6 +1,6 @@ { "JMC/JMC - Oblivion Mod "Falcor" p.1-thumb.jpg": "fb95b510681676e81c321171fc23143e", - "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "e4eb0e33920f9b4dce1fcba0561614c9", + "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "59ac500bb4778b37bd8c8fb472269e95", "JMC/JMC - Oblivion Mod "Falcor" p.1.mp4": "3744c49f2e447bd7712a5aad5ed36be2", "JMC/JMC - Oblivion Mod "Falcor" p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e" } \ No newline at end of file diff --git a/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json b/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json index 3d6d743..83521f0 100644 --- a/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json +++ b/tests/resources/expected_downloads_summaries/youtube/test_video_cli.json @@ -1,6 +1,6 @@ { "JMC/JMC - Oblivion Mod "Falcor" p.1-thumb.jpg": "fb95b510681676e81c321171fc23143e", - "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "62101bc3899f69860004ddcfc061a28b", + "JMC/JMC - Oblivion Mod "Falcor" p.1.info.json": "2ad1e593fc1c50a53a107db062e82c4a", "JMC/JMC - Oblivion Mod "Falcor" p.1.mp4": "3744c49f2e447bd7712a5aad5ed36be2", "JMC/JMC - Oblivion Mod "Falcor" p.1.nfo": "24cc4e17d2bebc89b2759ce5471d403e" } \ No newline at end of file diff --git a/tests/resources/transaction_log_summaries/youtube/test_video_nulled_values.txt b/tests/resources/transaction_log_summaries/youtube/test_video_nulled_values.txt index ff98d35..8b995cd 100644 --- a/tests/resources/transaction_log_summaries/youtube/test_video_nulled_values.txt +++ b/tests/resources/transaction_log_summaries/youtube/test_video_nulled_values.txt @@ -43,6 +43,8 @@ Files created: This is the first time I've really used the editor, and the beginning of our first mod. Hope you enjoy. season: 2010 + subscription_name: tv_video_nulled_values + subscription_name_sanitized: tv_video_nulled_values title: 2010-08-13 - Oblivion Mod "Falcor" p.1 year: 2010 s2010.e000002 - Oblivion Mod "Falcor" p.2.mp4 @@ -78,5 +80,7 @@ Files created: Please keep in mind that the mod is not complete, so you'll see some blank areas during the video. I haven't made LOD (Level of Distance) yet either, so areas far away will not appear. season: 2010 + subscription_name: tv_video_nulled_values + subscription_name_sanitized: tv_video_nulled_values title: 2010-12-02 - Oblivion Mod "Falcor" p.2 year: 2010 \ No newline at end of file