[FEATURE] Ability to add subscription __value__ via config
This commit is contained in:
parent
6615e1d1ac
commit
3c105c11cc
3 changed files with 34 additions and 18 deletions
|
|
@ -106,6 +106,7 @@ class ConfigOptions(StrictDictValidator):
|
|||
"ffprobe_path",
|
||||
"file_name_max_bytes",
|
||||
"experimental",
|
||||
"subscription_value",
|
||||
}
|
||||
|
||||
def __init__(self, name: str, value: Any):
|
||||
|
|
@ -138,6 +139,9 @@ class ConfigOptions(StrictDictValidator):
|
|||
self._file_name_max_bytes = self._validate_key(
|
||||
key="file_name_max_bytes", validator=IntValidator, default=MAX_FILE_NAME_BYTES
|
||||
)
|
||||
self._subscription_value = self._validate_key_if_present(
|
||||
key="subscription_value", validator=StringValidator
|
||||
)
|
||||
|
||||
@property
|
||||
def working_directory(self) -> str:
|
||||
|
|
@ -230,6 +234,14 @@ class ConfigOptions(StrictDictValidator):
|
|||
"""
|
||||
return self._ffprobe_path.value
|
||||
|
||||
@property
|
||||
def subscription_value(self) -> Optional[str]:
|
||||
"""
|
||||
Optional. Sets the (TODO: LINK)
|
||||
subscription value key for subscription files that use this config.
|
||||
"""
|
||||
return self._subscription_value.value if self._subscription_value else None
|
||||
|
||||
|
||||
class ConfigValidator(StrictDictValidator):
|
||||
_required_keys = {"configuration", "presets"}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import copy
|
||||
from typing import Dict
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
|
||||
from ytdl_sub.config.config_file import ConfigFile
|
||||
from ytdl_sub.config.preset import Preset
|
||||
|
|
@ -63,6 +64,18 @@ class Subscription(SubscriptionDownload):
|
|||
config=config,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _maybe_get_subscription_value(cls, config: ConfigFile, subscription_dict: Dict) -> Optional[str]:
|
||||
if FILE_SUBSCRIPTION_VALUE_KEY in subscription_dict:
|
||||
if not isinstance(subscription_dict[FILE_SUBSCRIPTION_VALUE_KEY], str):
|
||||
raise ValidationException(
|
||||
f"Using {FILE_SUBSCRIPTION_VALUE_KEY} in a subscription"
|
||||
f"must be a string that corresponds to an override variable"
|
||||
)
|
||||
return subscription_dict[FILE_SUBSCRIPTION_VALUE_KEY]
|
||||
|
||||
return config.config_options.subscription_value # can be None
|
||||
|
||||
@classmethod
|
||||
def from_file_path(cls, config: ConfigFile, subscription_path: str) -> List["Subscription"]:
|
||||
"""
|
||||
|
|
@ -90,7 +103,9 @@ class Subscription(SubscriptionDownload):
|
|||
subscription_dict = load_yaml(file_path=subscription_path)
|
||||
|
||||
has_file_preset = FILE_PRESET_APPLY_KEY in subscription_dict
|
||||
has_file_subscription_value = FILE_SUBSCRIPTION_VALUE_KEY in subscription_dict
|
||||
file_subscription_value: Optional[str] = cls._maybe_get_subscription_value(
|
||||
config=config, subscription_dict=subscription_dict
|
||||
)
|
||||
|
||||
# If a file preset is present...
|
||||
if has_file_preset:
|
||||
|
|
@ -104,13 +119,6 @@ class Subscription(SubscriptionDownload):
|
|||
config = copy.deepcopy(config)
|
||||
config.presets.dict[FILE_PRESET_APPLY_KEY] = file_preset.dict
|
||||
|
||||
if has_file_subscription_value:
|
||||
if not isinstance(subscription_dict[FILE_SUBSCRIPTION_VALUE_KEY], str):
|
||||
raise ValidationException(
|
||||
f"Using {FILE_SUBSCRIPTION_VALUE_KEY} in a subscription"
|
||||
f"must be a string that corresponds to an override variable"
|
||||
)
|
||||
|
||||
for subscription_key, subscription_object in subscription_dict.items():
|
||||
|
||||
# Skip file preset or value
|
||||
|
|
@ -119,18 +127,14 @@ class Subscription(SubscriptionDownload):
|
|||
|
||||
# If the subscription obj is just a string, set it to the override variable
|
||||
# defined in FILE_SUBSCRIPTION_VALUE_KEY
|
||||
if isinstance(subscription_object, str) and has_file_subscription_value:
|
||||
subscription_object = {
|
||||
"overrides": {
|
||||
subscription_dict[FILE_SUBSCRIPTION_VALUE_KEY]: subscription_object
|
||||
}
|
||||
}
|
||||
if isinstance(subscription_object, str) and file_subscription_value:
|
||||
subscription_object = {"overrides": {file_subscription_value: subscription_object}}
|
||||
elif isinstance(subscription_object, dict):
|
||||
pass
|
||||
elif isinstance(subscription_object, str) and not has_file_subscription_value:
|
||||
elif isinstance(subscription_object, str) and not file_subscription_value:
|
||||
raise ValidationException(
|
||||
f"Subscription {subscription_key} is a string, but "
|
||||
f"{FILE_SUBSCRIPTION_VALUE_KEY} is not set to an override variable"
|
||||
f"Subscription {subscription_key} is a string, but the subscription value "
|
||||
f"is not set to an override variable"
|
||||
)
|
||||
else:
|
||||
raise ValidationException(
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ def test_subscription_file_using_value_when_not_defined(config_file: ConfigFile)
|
|||
ValidationException,
|
||||
match=re.escape(
|
||||
f"Subscription sub_name is a string, but "
|
||||
f"{FILE_SUBSCRIPTION_VALUE_KEY} is not set to an override variable"
|
||||
f"the subscription value is not set to an override variable"
|
||||
),
|
||||
):
|
||||
_ = Subscription.from_file_path(config=config_file, subscription_path="mocked")
|
||||
|
|
|
|||
Loading…
Reference in a new issue