validate with variables, need to add downloader logic in preset

This commit is contained in:
Jesse Bannon 2022-09-12 22:41:53 -07:00
parent 1446720e8d
commit db1af8bd5f
6 changed files with 76 additions and 35 deletions

View file

@ -13,6 +13,7 @@ disable = [
"R0903", # too-few-public-methods
"R0801", # similar lines
"R0913", # Too many arguments
"R0901", # too-many-ancestors
"W0511", # TODO
]

View file

@ -1,4 +1,6 @@
from abc import ABC
from typing import Dict
from typing import List
from typing import Optional
from yt_dlp.utils import sanitize_filename
@ -13,6 +15,43 @@ from ytdl_sub.validators.validators import BoolValidator
from ytdl_sub.validators.validators import LiteralDictValidator
# pylint: disable=no-self-use
# pylint: disable=unused-argument
class AddsVariablesMixin(ABC):
"""
Mixin for parts of the Preset that adds source variables
"""
def added_source_variables(self) -> List[str]:
"""
If the plugin adds source variables, list them here.
Returns
-------
List of added source variables this plugin creates
"""
return []
def validate_with_variables(
self, source_variables: List[str], override_variables: List[str]
) -> None:
"""
Optional validation after init with the session's source and override variables.
Parameters
----------
source_variables
Available source variables when running the plugin
override_variables
Available override variables when running the plugin
"""
return None
# pylint: enable=no-self-use
# pylint: enable=unused-argument
class YTDLOptions(LiteralDictValidator):
"""
Optional. This section allows you to add any ytdl argument to ytdl-sub's downloader.

View file

@ -21,6 +21,7 @@ import yt_dlp as ytdl
from yt_dlp.utils import ExistingVideoReached
from yt_dlp.utils import RejectedVideoReached
from ytdl_sub.config.preset_options import AddsVariablesMixin
from ytdl_sub.config.preset_options import Overrides
from ytdl_sub.downloaders.ytdl_options_builder import YTDLOptionsBuilder
from ytdl_sub.entries.base_entry import BaseEntry
@ -36,7 +37,7 @@ from ytdl_sub.ytdl_additions.enhanced_download_archive import EnhancedDownloadAr
download_logger = Logger.get(name="downloader")
class DownloaderValidator(StrictDictValidator, ABC):
class DownloaderValidator(StrictDictValidator, AddsVariablesMixin, ABC):
"""
Placeholder class to define downloader options
"""

View file

@ -115,6 +115,28 @@ class CollectionDownloadOptions(DownloaderValidator):
"""
return self._urls
def added_source_variables(self) -> List[str]:
"""
Returns
-------
List of variables added. The first collection url always contains all the variables.
"""
return list(self._urls.list[0].variables.keys())
def validate_with_variables(
self, source_variables: List[str], override_variables: List[str]
) -> None:
"""
Ensures new variables added are not existing variables
"""
# TODO: Make sure they resolve
for added_source_var in self.added_source_variables():
if added_source_var in source_variables:
raise self._validation_exception(
f"'{added_source_var}' cannot be used as a variable name because it "
f"is an existing source variable"
)
class CollectionDownloader(Downloader[CollectionDownloadOptions, Entry]):
downloader_options_type = CollectionDownloadOptions
@ -141,9 +163,7 @@ class CollectionDownloader(Downloader[CollectionDownloadOptions, Entry]):
leaf_children.append(parent.child_entries[idx])
for leaf_child in leaf_children:
leaf_child.add_variables(
parent.get_children_entry_variables_to_add(parent.child_entries)
)
leaf_child.add_variables(parent.get_children_entry_variables_to_add())
leaf_child.add_variables(collection_url.variables)
return leaf_children

View file

@ -35,6 +35,9 @@ class EntryParent(BaseEntry):
# pylint: enable=no-self-use
def read_nested_children_from_entry_dicts(self, entry_dicts: List[Dict]):
"""
Populates a tree of EntryParents that belong to this instance
"""
child_entries: List["EntryParent"] = []
for entry_dict in entry_dicts:
@ -155,4 +158,9 @@ class EntryParent(BaseEntry):
return entry_parent
def to_entry(self) -> Entry:
return Entry(entry_dict=self._kwargs, working_directory=self.working_directory())
"""
Returns
-------
EntryParent converted to Entry
"""
return Entry(entry_dict=self._kwargs, working_directory=self._working_directory)

View file

@ -8,6 +8,7 @@ from typing import Type
from typing import TypeVar
from typing import final
from ytdl_sub.config.preset_options import AddsVariablesMixin
from ytdl_sub.config.preset_options import Overrides
from ytdl_sub.entries.entry import Entry
from ytdl_sub.utils.file_handler import FileMetadata
@ -39,40 +40,11 @@ class PluginPriority:
return self.modify_entry >= PluginPriority.MODIFY_ENTRY_AFTER_SPLIT
class PluginOptions(StrictDictValidator):
class PluginOptions(StrictDictValidator, AddsVariablesMixin, ABC):
"""
Class that defines the parameters to a plugin
"""
# pylint: disable=no-self-use
def added_source_variables(self) -> List[str]:
"""
If the plugin adds source variables, list them here.
Returns
-------
List of added source variables this plugin creates
"""
return []
# pylint: disable=unused-argument
def validate_with_variables(
self, source_variables: List[str], override_variables: List[str]
) -> None:
"""
Optional validation after init with the session's source and override variables.
Parameters
----------
source_variables
Available source variables when running the plugin
override_variables
Available override variables when running the plugin
"""
return None
# pylint: enable=no-self-use,unused-argument
PluginOptionsT = TypeVar("PluginOptionsT", bound=PluginOptions)