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 sys
from typing import List
###################################################################################################
@ -68,4 +69,4 @@ if __name__ == "__main__":
if args.subparser == "dl":
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
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["writethumbnail"] = True

View file

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

View file

@ -1,3 +1,4 @@
from typing import List
from typing import Set
from ytdl_subscribe.validators.base.validators import DictValidator
@ -5,17 +6,21 @@ from ytdl_subscribe.validators.exceptions import ValidationException
class StrictDictValidator(DictValidator):
"""
Validates dictionary-based fields with required and optional keys.
"""
required_keys: Set[str] = set()
optional_keys: Set[str] = set()
allow_extra_fields = False
allow_extra_keys = False
def __init__(self, name, value):
super().__init__(name, value)
if len(self.required_keys) == 0:
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
@ -27,7 +32,7 @@ class StrictDictValidator(DictValidator):
raise ValidationException(error_msg)
# 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:
if object_key not in self.allowed_keys:
error_msg = (
@ -37,5 +42,10 @@ class StrictDictValidator(DictValidator):
raise ValidationException(error_msg)
@property
def allowed_keys(self):
return sorted(self.required_keys.union(self.optional_keys))
def allowed_keys(self) -> List[str]:
"""
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:
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(
@ -59,4 +60,9 @@ class StringFormatterValidator(StringValidator):
@property
def format_string(self) -> str:
"""
Returns
-------
The literal format string, unformatted.
"""
return self._value

View file

@ -95,7 +95,8 @@ T = TypeVar("T", bound=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

View file

@ -27,7 +27,7 @@ class PresetValidator(StrictDictValidator):
self.subscription_source: Optional[SourceValidator] = 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:
raise ValidationException(
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"}
# 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]] = {}

View file

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