move native validators into single file
This commit is contained in:
parent
319cf1a3bb
commit
775342f0cf
11 changed files with 57 additions and 71 deletions
|
|
@ -1,11 +0,0 @@
|
|||
from typing import Any
|
||||
from typing import Optional
|
||||
from typing import Type
|
||||
|
||||
from ytdl_subscribe.validators.base.validator import Validator
|
||||
from ytdl_subscribe.validators.exceptions import ValidationException
|
||||
|
||||
|
||||
class BoolValidator(Validator):
|
||||
expected_value_type: Type = bool
|
||||
expected_value_type_name = "boolean"
|
||||
|
|
@ -5,7 +5,7 @@ from typing import Set
|
|||
from typing import Type
|
||||
from typing import TypeVar
|
||||
|
||||
from ytdl_subscribe.validators.base.validator import Validator
|
||||
from ytdl_subscribe.validators.base.validators import Validator
|
||||
from ytdl_subscribe.validators.exceptions import ValidationException
|
||||
|
||||
T = TypeVar("T", bound=Validator)
|
||||
|
|
@ -35,7 +35,7 @@ class DictValidator(Validator):
|
|||
# If no extra fields are allowed, ensure all fields are either
|
||||
# required or optional fields
|
||||
if not self._allow_extra_fields:
|
||||
for object_key in self.object_keys:
|
||||
for object_key in self.keys:
|
||||
if object_key not in self.allowed_fields:
|
||||
error_msg = (
|
||||
f"'{self.name}' contains the field '{object_key}' which is not allowed. "
|
||||
|
|
@ -43,18 +43,18 @@ class DictValidator(Validator):
|
|||
)
|
||||
raise ValidationException(error_msg)
|
||||
|
||||
def validate_dict_value(
|
||||
self, dict_value_name: str, validator: Type[T], default: Optional[Any] = None
|
||||
def validate_key(
|
||||
self, key: str, validator: Type[T], default: Optional[Any] = None
|
||||
) -> T:
|
||||
value = self.get(object_key=dict_value_name, default=default)
|
||||
value = self.get(key=key, default=default)
|
||||
if value is None:
|
||||
raise self._validation_exception(
|
||||
f"{dict_value_name} is missing when it should be present."
|
||||
f"{key} is missing when it should be present."
|
||||
)
|
||||
|
||||
return validator(
|
||||
name=f"{self.name}.{dict_value_name}",
|
||||
value=self.get(object_key=dict_value_name, default=default),
|
||||
name=f"{self.name}.{key}",
|
||||
value=self.get(key=key, default=default),
|
||||
)
|
||||
|
||||
@property
|
||||
|
|
@ -66,15 +66,11 @@ class DictValidator(Validator):
|
|||
return sorted(self.required_fields.union(self.optional_fields))
|
||||
|
||||
@property
|
||||
def object_keys(self):
|
||||
def keys(self):
|
||||
return sorted(list(self.dict.keys()))
|
||||
|
||||
@property
|
||||
def object_items(self):
|
||||
return self.dict.items()
|
||||
|
||||
def get(self, object_key: str, default: Optional[Any] = None) -> Any:
|
||||
return self.dict.get(object_key, default)
|
||||
def get(self, key: str, default: Optional[Any] = None) -> Any:
|
||||
return self.dict.get(key, default)
|
||||
|
||||
|
||||
class DictWithExtraFieldsValidator(DictValidator):
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import re
|
|||
from keyword import iskeyword
|
||||
from typing import List
|
||||
|
||||
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
||||
from ytdl_subscribe.validators.base.validators import StringValidator
|
||||
|
||||
|
||||
class StringFormatterValidator(StringValidator):
|
||||
|
|
|
|||
|
|
@ -1,15 +0,0 @@
|
|||
from typing import Any
|
||||
from typing import Optional
|
||||
from typing import Type
|
||||
|
||||
from ytdl_subscribe.validators.base.validator import Validator
|
||||
from ytdl_subscribe.validators.exceptions import ValidationException
|
||||
|
||||
|
||||
class StringValidator(Validator):
|
||||
expected_value_type: Type = str
|
||||
expected_value_type_name = "string"
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return self._value
|
||||
|
|
@ -39,3 +39,21 @@ class Validator:
|
|||
def _validation_exception(self, error_message: str):
|
||||
prefix = f"Validation error in {self.name}: "
|
||||
return ValidationException(f"{prefix}{error_message}")
|
||||
|
||||
|
||||
class BoolValidator(Validator):
|
||||
expected_value_type: Type = bool
|
||||
expected_value_type_name = "boolean"
|
||||
|
||||
@property
|
||||
def value(self) -> bool:
|
||||
return self._value
|
||||
|
||||
|
||||
class StringValidator(Validator):
|
||||
expected_value_type: Type = str
|
||||
expected_value_type_name = "string"
|
||||
|
||||
@property
|
||||
def value(self) -> str:
|
||||
return self._value
|
||||
|
|
@ -4,7 +4,7 @@ import yaml
|
|||
|
||||
from ytdl_subscribe.validators.base.dict_validator import DictValidator
|
||||
from ytdl_subscribe.validators.base.dict_validator import DictWithExtraFieldsValidator
|
||||
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
||||
from ytdl_subscribe.validators.base.validators import StringValidator
|
||||
|
||||
|
||||
class ConfigValidator(DictValidator):
|
||||
|
|
@ -12,10 +12,8 @@ class ConfigValidator(DictValidator):
|
|||
|
||||
def __init__(self, name: str, value: Any):
|
||||
super().__init__(name, value)
|
||||
self.working_directory = self.validate_dict_value(
|
||||
"working_directory", StringValidator
|
||||
)
|
||||
self.presets = self.validate_dict_value("presets", DictWithExtraFieldsValidator)
|
||||
self.working_directory = self.validate_key("working_directory", StringValidator)
|
||||
self.presets = self.validate_key("presets", DictWithExtraFieldsValidator)
|
||||
|
||||
@classmethod
|
||||
def from_file_path(cls, config_path) -> "ConfigValidator":
|
||||
|
|
|
|||
|
|
@ -27,22 +27,22 @@ class PresetValidator(DictValidator):
|
|||
self.subscription_source: Optional[SourceValidator] = None
|
||||
self.subscription_source_name: Optional[str] = None
|
||||
|
||||
for object_key, object_value in self.object_items:
|
||||
if object_key in SubscriptionSourceName.all() and self.subscription_source:
|
||||
for key, val in self.dict.items():
|
||||
if key in SubscriptionSourceName.all() and self.subscription_source:
|
||||
raise ValidationException(
|
||||
f"'{self.name}' can only have one of the following sources: {SubscriptionSourceName.pretty_all()}"
|
||||
)
|
||||
|
||||
if object_key == SubscriptionSourceName.SOUNDCLOUD:
|
||||
if key == SubscriptionSourceName.SOUNDCLOUD:
|
||||
self.subscription_source_name = SubscriptionSourceName.SOUNDCLOUD
|
||||
self.subscription_source = self.validate_dict_value(
|
||||
dict_value_name=object_key,
|
||||
self.subscription_source = self.validate_key(
|
||||
key=key,
|
||||
validator=SoundcloudSourceValidator,
|
||||
)
|
||||
elif object_key == SubscriptionSourceName.YOUTUBE:
|
||||
elif key == SubscriptionSourceName.YOUTUBE:
|
||||
self.subscription_source_name = SubscriptionSourceName.YOUTUBE
|
||||
self.subscription_source = self.validate_dict_value(
|
||||
dict_value_name=object_key,
|
||||
self.subscription_source = self.validate_key(
|
||||
key=key,
|
||||
validator=YoutubeSourceValidator,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from ytdl_subscribe.validators.base.bool_validator import BoolValidator
|
||||
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
||||
from ytdl_subscribe.validators.base.validators import BoolValidator
|
||||
from ytdl_subscribe.validators.base.validators import StringValidator
|
||||
from ytdl_subscribe.validators.config.sources.source_validator import (
|
||||
DownloadStrategyValidator,
|
||||
)
|
||||
|
|
@ -11,8 +11,8 @@ class SoundcloudAlbumsAndSinglesDownloadValidator(DownloadStrategyValidator):
|
|||
|
||||
def __init__(self, name, value):
|
||||
super().__init__(name, value)
|
||||
self.username = self.validate_dict_value(
|
||||
dict_value_name="username", validator=StringValidator
|
||||
self.username = self.validate_key(
|
||||
key="username", validator=StringValidator
|
||||
).value
|
||||
|
||||
|
||||
|
|
@ -25,6 +25,6 @@ class SoundcloudSourceValidator(SourceValidator):
|
|||
|
||||
def __init__(self, name: str, value: dict):
|
||||
super().__init__(name=name, value=value)
|
||||
self.skip_premiere_tracks = self.validate_dict_value(
|
||||
self.skip_premiere_tracks = self.validate_key(
|
||||
"skip_premiere_tracks", BoolValidator, default=True
|
||||
).value
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from typing import Type
|
|||
|
||||
from ytdl_subscribe.validators.base.dict_validator import DictValidator
|
||||
from ytdl_subscribe.validators.base.dict_validator import DictWithExtraFieldsValidator
|
||||
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
||||
from ytdl_subscribe.validators.base.validators import StringValidator
|
||||
|
||||
|
||||
class DownloadStrategyValidator(DictValidator):
|
||||
|
|
@ -20,8 +20,8 @@ class SourceValidator(DictWithExtraFieldsValidator):
|
|||
|
||||
def __init__(self, name: str, value: Any):
|
||||
super().__init__(name=name, value=value)
|
||||
self.download_strategy_name = self.validate_dict_value(
|
||||
dict_value_name="download_strategy",
|
||||
self.download_strategy_name = self.validate_key(
|
||||
key="download_strategy",
|
||||
validator=StringValidator,
|
||||
).value
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from typing import Any
|
||||
|
||||
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
||||
from ytdl_subscribe.validators.base.validators import StringValidator
|
||||
from ytdl_subscribe.validators.config.sources.source_validator import (
|
||||
DownloadStrategyValidator,
|
||||
)
|
||||
|
|
@ -12,7 +12,7 @@ class YoutubePlaylistDownloadValidator(DownloadStrategyValidator):
|
|||
|
||||
def __init__(self, name, value):
|
||||
super().__init__(name, value)
|
||||
self.playlist_id = self.validate_dict_value("playlist_id", StringValidator)
|
||||
self.playlist_id = self.validate_key("playlist_id", StringValidator)
|
||||
|
||||
|
||||
class YoutubeSourceValidator(SourceValidator):
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ from ytdl_subscribe.subscriptions.youtube import YoutubeSubscription
|
|||
from ytdl_subscribe.utils.enums import SubscriptionSourceName
|
||||
from ytdl_subscribe.validators.base.dict_validator import DictValidator
|
||||
from ytdl_subscribe.validators.base.dict_validator import DictWithExtraFieldsValidator
|
||||
from ytdl_subscribe.validators.base.string_validator import StringValidator
|
||||
from ytdl_subscribe.validators.base.validators import StringValidator
|
||||
from ytdl_subscribe.validators.config.config_validator import ConfigValidator
|
||||
from ytdl_subscribe.validators.config.preset_validator import PresetValidator
|
||||
|
||||
|
|
@ -28,18 +28,18 @@ class SubscriptionValidator(DictValidator):
|
|||
def __init__(self, config: ConfigValidator, name: str, value: Any):
|
||||
super().__init__(name, value)
|
||||
self.config = config
|
||||
self.overrides = self.validate_dict_value(
|
||||
dict_value_name="overrides",
|
||||
self.overrides = self.validate_key(
|
||||
key="overrides",
|
||||
validator=DictWithExtraFieldsValidator,
|
||||
default={},
|
||||
)
|
||||
|
||||
preset_name = self.validate_dict_value(
|
||||
dict_value_name="preset",
|
||||
preset_name = self.validate_key(
|
||||
key="preset",
|
||||
validator=StringValidator,
|
||||
).value
|
||||
|
||||
available_presets = self.config.presets.object_keys
|
||||
available_presets = self.config.presets.keys
|
||||
if preset_name not in available_presets:
|
||||
raise self._validation_exception(
|
||||
f"'preset '{preset_name}' does not exist in the provided config. "
|
||||
|
|
|
|||
Loading…
Reference in a new issue