pylint, docstrings for entry
This commit is contained in:
parent
64eb1e83ab
commit
9847cadcbc
4 changed files with 34 additions and 7 deletions
4
.pylintrc
Normal file
4
.pylintrc
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
[MASTER]
|
||||||
|
disable=
|
||||||
|
C0114, # missing-module-docstring
|
||||||
|
R0903, # too-few-public-methods
|
||||||
|
|
@ -7,6 +7,7 @@ mergedeep==1.3.4
|
||||||
music-tag==0.4.3
|
music-tag==0.4.3
|
||||||
pathvalidate==2.4.1
|
pathvalidate==2.4.1
|
||||||
Pillow==8.3.2
|
Pillow==8.3.2
|
||||||
|
pylint==2.13.3
|
||||||
pytest==7.1.1
|
pytest==7.1.1
|
||||||
pyYAML==5.4.1
|
pyYAML==5.4.1
|
||||||
sanitize-filename==1.2.0
|
sanitize-filename==1.2.0
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
from .enums import *
|
|
||||||
|
|
@ -8,7 +8,10 @@ from typing import Optional
|
||||||
from sanitize_filename import sanitize
|
from sanitize_filename import sanitize
|
||||||
|
|
||||||
|
|
||||||
class EntryFormatter(object):
|
class EntryFormatter:
|
||||||
|
"""
|
||||||
|
Ensures user-created formatter strings are valid
|
||||||
|
"""
|
||||||
FIELDS_VALIDATOR = re.compile(r"{([a-z_]+?)}")
|
FIELDS_VALIDATOR = re.compile(r"{([a-z_]+?)}")
|
||||||
|
|
||||||
def __init__(self, format_string: str):
|
def __init__(self, format_string: str):
|
||||||
|
|
@ -26,20 +29,22 @@ class EntryFormatter(object):
|
||||||
|
|
||||||
if open_bracket_count != close_bracket_count:
|
if open_bracket_count != close_bracket_count:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"{self._error_prefix} Brackets are reserved for {{variable_names}} and should contain a single open and close bracket."
|
f"{self._error_prefix} Brackets are reserved for {{variable_names}} "
|
||||||
|
f"and should contain a single open and close bracket."
|
||||||
)
|
)
|
||||||
|
|
||||||
parsed_fields = re.findall(EntryFormatter.FIELDS_VALIDATOR, self.format_string)
|
parsed_fields = re.findall(EntryFormatter.FIELDS_VALIDATOR, self.format_string)
|
||||||
|
|
||||||
if len(parsed_fields) != open_bracket_count:
|
if len(parsed_fields) != open_bracket_count:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"{self._error_prefix} {{variable_names}} should only contain lowercase letters and underscores with a single open and close bracket."
|
f"{self._error_prefix} {{variable_names}} should only contain "
|
||||||
|
f"lowercase letters and underscores with a single open and close bracket."
|
||||||
)
|
)
|
||||||
|
|
||||||
return sorted(parsed_fields)
|
return sorted(parsed_fields)
|
||||||
|
|
||||||
|
|
||||||
class Entry(object):
|
class Entry:
|
||||||
"""
|
"""
|
||||||
Entry object to represent a single media object returned from yt-dlp.
|
Entry object to represent a single media object returned from yt-dlp.
|
||||||
"""
|
"""
|
||||||
|
|
@ -47,10 +52,12 @@ class Entry(object):
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self._kwargs = kwargs
|
self._kwargs = kwargs
|
||||||
|
|
||||||
def kwargs_contains(self, key):
|
def kwargs_contains(self, key: str) -> bool:
|
||||||
|
"""Check if internal kwargs contains the specified key"""
|
||||||
return key in self._kwargs
|
return key in self._kwargs
|
||||||
|
|
||||||
def kwargs(self, key) -> Any:
|
def kwargs(self, key) -> Any:
|
||||||
|
"""Get an internal kwarg value supplied from ytdl"""
|
||||||
if not self.kwargs_contains(key):
|
if not self.kwargs_contains(key):
|
||||||
raise KeyError(
|
raise KeyError(
|
||||||
f"Expected '{key}' in {self.__class__.__name__} but does not exist."
|
f"Expected '{key}' in {self.__class__.__name__} but does not exist."
|
||||||
|
|
@ -59,44 +66,55 @@ class Entry(object):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def uid(self) -> str:
|
def uid(self) -> str:
|
||||||
|
"""Get the entry's unique id"""
|
||||||
return self.kwargs("id")
|
return self.kwargs("id")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def title(self) -> str:
|
def title(self) -> str:
|
||||||
|
"""Get the entry's title"""
|
||||||
return self.kwargs("title")
|
return self.kwargs("title")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sanitized_title(self) -> str:
|
def sanitized_title(self) -> str:
|
||||||
|
"""Get the entry's sanitized title"""
|
||||||
return sanitize(self.title)
|
return sanitize(self.title)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ext(self) -> str:
|
def ext(self) -> str:
|
||||||
|
"""Get the entry's file extension"""
|
||||||
return self.kwargs("ext")
|
return self.kwargs("ext")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def upload_date(self) -> str:
|
def upload_date(self) -> str:
|
||||||
|
"""Get the entry's upload date"""
|
||||||
return self.kwargs("upload_date")
|
return self.kwargs("upload_date")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def upload_year(self) -> int:
|
def upload_year(self) -> int:
|
||||||
|
"""Get the entry's upload year"""
|
||||||
return int(self.upload_date[:4])
|
return int(self.upload_date[:4])
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def thumbnail(self) -> str:
|
def thumbnail(self) -> str:
|
||||||
|
"""Get the entry's thumbnail url"""
|
||||||
return self.kwargs("thumbnail")
|
return self.kwargs("thumbnail")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def thumbnail_ext(self) -> str:
|
def thumbnail_ext(self) -> str:
|
||||||
|
"""Get the entry's thumbnail extension"""
|
||||||
return self.thumbnail.split(".")[-1]
|
return self.thumbnail.split(".")[-1]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def download_file_name(self) -> str:
|
def download_file_name(self) -> str:
|
||||||
|
"""Get the entry's file name when downloaded locally"""
|
||||||
return f"{self.uid}.{self.ext}"
|
return f"{self.uid}.{self.ext}"
|
||||||
|
|
||||||
def file_path(self, relative_directory: str):
|
def file_path(self, relative_directory: str):
|
||||||
|
"""Get the entry's file path with respect to the relative directory"""
|
||||||
return str(Path(relative_directory) / self.download_file_name)
|
return str(Path(relative_directory) / self.download_file_name)
|
||||||
|
|
||||||
def to_dict(self) -> Dict:
|
def to_dict(self) -> Dict:
|
||||||
|
"""Expose the entry's values as a dictionary"""
|
||||||
return {
|
return {
|
||||||
"uid": self.uid,
|
"uid": self.uid,
|
||||||
"title": self.title,
|
"title": self.title,
|
||||||
|
|
@ -111,6 +129,10 @@ class Entry(object):
|
||||||
def apply_formatter(
|
def apply_formatter(
|
||||||
self, format_string: str, overrides: Optional[Dict] = None
|
self, format_string: str, overrides: Optional[Dict] = None
|
||||||
) -> str:
|
) -> str:
|
||||||
|
"""
|
||||||
|
Perform a string format on the given format string, using the entry's dict for format
|
||||||
|
values. The override dict will overwrite any values within the entry's dict.
|
||||||
|
"""
|
||||||
entry_dict = self.to_dict()
|
entry_dict = self.to_dict()
|
||||||
if overrides:
|
if overrides:
|
||||||
entry_dict = dict(entry_dict, **overrides)
|
entry_dict = dict(entry_dict, **overrides)
|
||||||
|
|
@ -121,7 +143,8 @@ class Entry(object):
|
||||||
if field_name not in entry_dict:
|
if field_name not in entry_dict:
|
||||||
available_fields = ", ".join(sorted(entry_dict.keys()))
|
available_fields = ", ".join(sorted(entry_dict.keys()))
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Format variable '{field_name}' does not exist for {self.__class__.__name__}. Available fields: {available_fields}"
|
f"Format variable '{field_name}' does not exist "
|
||||||
|
f"for {self.__class__.__name__}. Available fields: {available_fields}"
|
||||||
)
|
)
|
||||||
|
|
||||||
return format_string.format(**entry_dict)
|
return format_string.format(**entry_dict)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue