Skip to content

Commit

Permalink
[BUGFIX] Fix periods not working in subscription names (#857)
Browse files Browse the repository at this point in the history
Fixes the bug (#844) where subscription names would drop periods from them, i.e. Mr. Beast would have the name  Beast.

POTENTIAL BREAKING CHANGE
If you have a subscription with a period in its name prior to this fix, this change will make it so your download archive is not found since it uses the subscription name in the download archive file path. To fix, simply change the download archive JSON's name to have the actual subscription name, and not the dropped-period one.
  • Loading branch information
jmbannon authored Dec 27, 2023
1 parent 1375a0f commit 65bb5c0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
60 changes: 37 additions & 23 deletions src/ytdl_sub/subscriptions/subscription_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,26 @@ def subscription_dicts(self, global_presets_to_apply: List[str]) -> Dict[str, Di
Subscriptions in the form of ``{ subscription_name: preset_dict }``
"""

@property
def subscription_name(self) -> str:
"""
Returns
-------
The name of the subscription
"""
return self._leaf_name


class SubscriptionPresetDictValidator(SubscriptionOutput, DictValidator):
def __init__(self, name, value, presets: List[str], indent_overrides: List[str]):
class NamedSubscriptionValidator(SubscriptionOutput, ABC):
def __init__(
self, name, value, subscription_name: str, presets: List[str], indent_overrides: List[str]
):
super().__init__(name=name, value=value, presets=presets, indent_overrides=indent_overrides)
self.subscription_name = subscription_name


class SubscriptionPresetDictValidator(NamedSubscriptionValidator, DictValidator):
def __init__(
self, name, value, subscription_name: str, presets: List[str], indent_overrides: List[str]
):
super().__init__(
name=name,
value=value,
subscription_name=subscription_name,
presets=presets,
indent_overrides=indent_overrides,
)

_ = self._validate_key_if_present(key="preset", validator=StringListValidator, default=[])
_ = self._validate_key_if_present(key="overrides", validator=Overrides, default={})
Expand All @@ -80,16 +87,23 @@ def subscription_dicts(self, global_presets_to_apply: List[str]) -> Dict[str, Di
return {self.subscription_name: output_dict}


class SubscriptionLeafValidator(SubscriptionOutput, ABC):
class SubscriptionLeafValidator(NamedSubscriptionValidator, ABC):
def __init__(
self,
name,
value,
subscription_name: str,
config: ConfigFile,
presets: List[str],
indent_overrides: List[str],
):
super().__init__(name=name, value=value, presets=presets, indent_overrides=indent_overrides)
super().__init__(
name=name,
value=value,
subscription_name=subscription_name,
presets=presets,
indent_overrides=indent_overrides,
)

if self.subscription_name in config.presets.keys:
raise self._validation_exception(
Expand Down Expand Up @@ -117,13 +131,15 @@ def __init__(
self,
name,
value,
subscription_name: str,
config: ConfigFile,
presets: List[str],
indent_overrides: List[str],
):
super().__init__(
name=name,
value=value,
subscription_name=subscription_name,
config=config,
presets=presets,
indent_overrides=indent_overrides,
Expand All @@ -136,13 +152,15 @@ def __init__(
self,
name,
value,
subscription_name: str,
config: ConfigFile,
presets: List[str],
indent_overrides: List[str],
):
super().__init__(
name=name,
value=value,
subscription_name=subscription_name,
config=config,
presets=presets,
indent_overrides=indent_overrides,
Expand All @@ -163,30 +181,22 @@ def __init__(
self,
name,
value,
subscription_name: str,
config: ConfigFile,
presets: List[str],
indent_overrides: List[str],
):
super().__init__(
name=name,
value=value,
subscription_name=subscription_name,
config=config,
presets=presets,
indent_overrides=indent_overrides,
)

self._overrides_to_add = dict(self.dict_with_format_strings, **self._overrides_to_add)

@property
def subscription_name(self) -> str:
"""
Returns
-------
Name of the subscription
"""
# drop the ~ in "~Subscription Name":
return super().subscription_name[1:]


class SubscriptionValidator(SubscriptionOutput):
"""
Expand Down Expand Up @@ -243,6 +253,7 @@ def __init__(
SubscriptionValueValidator(
name=obj_name,
value=obj,
subscription_name=key,
config=config,
presets=presets,
indent_overrides=indent_overrides,
Expand All @@ -257,6 +268,7 @@ def __init__(
SubscriptionListValuesValidator(
name=obj_name,
value=obj,
subscription_name=key,
config=config,
presets=presets,
indent_overrides=indent_overrides,
Expand All @@ -272,6 +284,7 @@ def __init__(
SubscriptionWithOverridesValidator(
name=obj_name,
value=obj,
subscription_name=key[1:],
config=config,
presets=presets,
indent_overrides=indent_overrides,
Expand All @@ -294,6 +307,7 @@ def __init__(
SubscriptionPresetDictValidator(
name=obj_name,
value=obj,
subscription_name=key,
presets=presets,
indent_overrides=indent_overrides,
)
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/config/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from unittest.mock import patch

import pytest
from mergedeep import mergedeep

from ytdl_sub.config.config_file import ConfigFile
from ytdl_sub.plugins.nfo_tags import NfoTagsOptions
Expand Down Expand Up @@ -57,6 +56,16 @@ def preset_with_subscription_value(preset_with_file_preset: Dict):
)


@pytest.fixture
def subscription_with_period_in_name(preset_with_file_preset: Dict):
return dict(
preset_with_file_preset,
**{
"Mr. Beast": "is_overwritten",
},
)


@pytest.fixture
def preset_with_subscription_value_nested_presets(preset_with_subscription_value: Dict):
return dict(
Expand Down Expand Up @@ -231,6 +240,18 @@ def test_subscription_overrides_tilda(
assert sub_2_1.get("current_override") == "test_2_1" # tilda sub takes precedence


def test_subscription_with_period_in_name(
config_file: ConfigFile,
subscription_with_period_in_name: Dict,
):
with mock_load_yaml(preset_dict=subscription_with_period_in_name):
subs = Subscription.from_file_path(config=config_file, subscription_path="mocked")
assert len(subs) == 2

assert subs[1].name == "Mr. Beast"
assert subs[1].overrides.dict_with_format_strings["subscription_name"] == "Mr. Beast"


def test_subscription_file_value_applies_from_config_and_nested_and_indent_variables(
config_file: ConfigFile,
preset_with_subscription_value_nested_presets_and_indent_variables: Dict,
Expand Down

0 comments on commit 65bb5c0

Please sign in to comment.