pylinting

This commit is contained in:
jbannon 2022-04-03 18:41:37 +00:00
parent 16cba13570
commit c838447423
9 changed files with 32 additions and 15 deletions

View file

@ -1,4 +1,5 @@
import argparse import argparse
import sys
from typing import List from typing import List
################################################################################################### ###################################################################################################
@ -68,4 +69,4 @@ if __name__ == "__main__":
if args.subparser == "dl": if args.subparser == "dl":
print("Interactive download is not supported yet. Stay tuned!") print("Interactive download is not supported yet. Stay tuned!")
exit(0) sys.exit(0)

View file

@ -43,7 +43,8 @@ class Subscription(object):
# Separate each subscription's working directory # Separate each subscription's working directory
self.WORKING_DIRECTORY += f"{'/' if self.WORKING_DIRECTORY else ''}{self.name}" self.WORKING_DIRECTORY += f"{'/' if self.WORKING_DIRECTORY else ''}{self.name}"
# Always set outtmpl to the id and extension. Will be renamed using the subscription's output_path value # Always set outtmpl to the id and extension. Will be renamed using the subscription's
# output_path value
self.ytdl_opts["outtmpl"] = self.WORKING_DIRECTORY + "/%(id)s.%(ext)s" self.ytdl_opts["outtmpl"] = self.WORKING_DIRECTORY + "/%(id)s.%(ext)s"
self.ytdl_opts["writethumbnail"] = True self.ytdl_opts["writethumbnail"] = True

View file

@ -1,7 +1,7 @@
from typing import List from typing import List
class SubscriptionSourceName(object): class SubscriptionSourceName:
YOUTUBE = "youtube" YOUTUBE = "youtube"
SOUNDCLOUD = "soundcloud" SOUNDCLOUD = "soundcloud"

View file

@ -1,3 +1,4 @@
from typing import List
from typing import Set from typing import Set
from ytdl_subscribe.validators.base.validators import DictValidator from ytdl_subscribe.validators.base.validators import DictValidator
@ -5,17 +6,21 @@ from ytdl_subscribe.validators.exceptions import ValidationException
class StrictDictValidator(DictValidator): class StrictDictValidator(DictValidator):
"""
Validates dictionary-based fields with required and optional keys.
"""
required_keys: Set[str] = set() required_keys: Set[str] = set()
optional_keys: Set[str] = set() optional_keys: Set[str] = set()
allow_extra_keys = False
allow_extra_fields = False
def __init__(self, name, value): def __init__(self, name, value):
super().__init__(name, value) super().__init__(name, value)
if len(self.required_keys) == 0: if len(self.required_keys) == 0:
raise ValueError( raise ValueError(
"No required fields when using a StrictDictValidator. Should be using DictValidator instead." "No required fields when using a StrictDictValidator. "
"Should be using DictValidator instead."
) )
# Ensure all required keys are present # Ensure all required keys are present
@ -27,7 +32,7 @@ class StrictDictValidator(DictValidator):
raise ValidationException(error_msg) raise ValidationException(error_msg)
# Ensure all keys are either required or optional keys if no extra field are allowed # Ensure all keys are either required or optional keys if no extra field are allowed
if not self.allow_extra_fields: if not self.allow_extra_keys:
for object_key in self.keys: for object_key in self.keys:
if object_key not in self.allowed_keys: if object_key not in self.allowed_keys:
error_msg = ( error_msg = (
@ -37,5 +42,10 @@ class StrictDictValidator(DictValidator):
raise ValidationException(error_msg) raise ValidationException(error_msg)
@property @property
def allowed_keys(self): def allowed_keys(self) -> List[str]:
return sorted(self.required_keys.union(self.optional_keys)) """
Returns
-------
Sorted list of required and optional keys
"""
return sorted(list(self.required_keys.union(self.optional_keys)))

View file

@ -32,7 +32,8 @@ class StringFormatterValidator(StringValidator):
if open_bracket_count != close_bracket_count: if open_bracket_count != close_bracket_count:
raise self._validation_exception( raise self._validation_exception(
error_message="Brackets are reserved for {variable_names} and should contain a single open and close bracket." "Brackets are reserved for {variable_names} and should contain "
"a single open and close bracket."
) )
format_variables: List[str] = list( format_variables: List[str] = list(
@ -59,4 +60,9 @@ class StringFormatterValidator(StringValidator):
@property @property
def format_string(self) -> str: def format_string(self) -> str:
"""
Returns
-------
The literal format string, unformatted.
"""
return self._value return self._value

View file

@ -95,7 +95,8 @@ T = TypeVar("T", bound=Validator)
class DictValidator(Validator): class DictValidator(Validator):
""" """
Validates dictionary-based fields. Errors to them as 'object's since this could be validating a yaml. Validates dictionary-based fields. Errors to them as 'object's since this could be validating
a yaml.
""" """
expected_value_type = dict expected_value_type = dict

View file

@ -27,7 +27,7 @@ class PresetValidator(StrictDictValidator):
self.subscription_source: Optional[SourceValidator] = None self.subscription_source: Optional[SourceValidator] = None
self.subscription_source_name: Optional[str] = None self.subscription_source_name: Optional[str] = None
for key, val in self.dict.items(): for key in self.keys:
if key in SubscriptionSourceName.all() and self.subscription_source: if key in SubscriptionSourceName.all() and self.subscription_source:
raise ValidationException( raise ValidationException(
f"'{self.name}' can only have one of the following sources: {SubscriptionSourceName.pretty_all()}" f"'{self.name}' can only have one of the following sources: {SubscriptionSourceName.pretty_all()}"

View file

@ -16,7 +16,7 @@ class SourceValidator(StrictDictValidator):
required_keys = {"download_strategy"} required_keys = {"download_strategy"}
# Extra fields will be strict-validated using other StictDictValidators # Extra fields will be strict-validated using other StictDictValidators
allow_extra_fields = True allow_extra_keys = True
download_strategy_validator_mapping: Dict[str, Type[DownloadStrategyValidator]] = {} download_strategy_validator_mapping: Dict[str, Type[DownloadStrategyValidator]] = {}

View file

@ -1,4 +1,2 @@
class ValidationException(ValueError): class ValidationException(ValueError):
"""Any user-caused configuration error should result in this error""" """Any user-caused configuration error should result in this error"""
pass