[REFACTOR] SourcePluginExtension

This commit is contained in:
Jesse Bannon 2023-07-25 12:41:36 -07:00
parent 1236af270d
commit 80d48bb491
3 changed files with 31 additions and 49 deletions

View file

@ -1,8 +1,12 @@
import abc import abc
from abc import ABC from abc import ABC
from typing import Dict
from typing import Generic from typing import Generic
from typing import Iterable from typing import Iterable
from typing import List from typing import List
from typing import Optional
from typing import Type
from typing import final
from ytdl_sub.config.plugin import BasePlugin from ytdl_sub.config.plugin import BasePlugin
from ytdl_sub.config.plugin import Plugin from ytdl_sub.config.plugin import Plugin
@ -19,8 +23,18 @@ class SourcePluginExtension(Plugin[TOptionsValidator], ABC):
are the plugin options. are the plugin options.
""" """
@final
def ytdl_options(self) -> Optional[Dict]:
"""
SourcePluginExtensions are intended to run after downloading. ytdl_options at that point
are not needed.
"""
return None
class SourcePlugin(BasePlugin[TOptionsValidator], Generic[TOptionsValidator], ABC): class SourcePlugin(BasePlugin[TOptionsValidator], Generic[TOptionsValidator], ABC):
plugin_extensions: List[Type[SourcePluginExtension]] = []
def __init__( def __init__(
self, self,
options: TOptionsValidator, options: TOptionsValidator,
@ -45,15 +59,14 @@ class SourcePlugin(BasePlugin[TOptionsValidator], Generic[TOptionsValidator], AB
def download(self, entry: Entry) -> Entry: def download(self, entry: Entry) -> Entry:
"""The function to perform the download of all media entries""" """The function to perform the download of all media entries"""
# pylint: disable=unused-argument @final
@classmethod def added_plugins(self) -> List[SourcePluginExtension]:
def added_plugins(
cls,
downloader_options: TOptionsValidator,
enhanced_download_archive: EnhancedDownloadArchive,
overrides: Overrides,
) -> List[SourcePluginExtension]:
"""Add these plugins from the Downloader to the subscription""" """Add these plugins from the Downloader to the subscription"""
return [] return [
plugin_extension(
# pylint: enable=unused-argument options=self.plugin_options,
overrides=self.overrides,
enhanced_download_archive=self._enhanced_download_archive,
)
for plugin_extension in self.plugin_extensions
]

View file

@ -12,7 +12,6 @@ from typing import Tuple
from yt_dlp.utils import RejectedVideoReached from yt_dlp.utils import RejectedVideoReached
from ytdl_sub.config.plugin import Plugin
from ytdl_sub.config.preset_options import Overrides from ytdl_sub.config.preset_options import Overrides
from ytdl_sub.downloaders.source_plugin import SourcePlugin from ytdl_sub.downloaders.source_plugin import SourcePlugin
from ytdl_sub.downloaders.source_plugin import SourcePluginExtension from ytdl_sub.downloaders.source_plugin import SourcePluginExtension
@ -196,30 +195,7 @@ class BaseUrlDownloader(SourcePlugin[TMultiURLSourceOptionsValidator], ABC):
and should translate that to list of Entry objects. and should translate that to list of Entry objects.
""" """
@classmethod plugin_extensions = [UrlDownloaderThumbnailPlugin, UrlDownloaderCollectionVariablePlugin]
def added_plugins(
cls,
downloader_options: TMultiURLSourceOptionsValidator,
enhanced_download_archive: EnhancedDownloadArchive,
overrides: Overrides,
) -> List[Plugin]:
"""
Adds
1. URL thumbnail download plugin
2. Collection variable plugin to add to each entry
"""
return [
UrlDownloaderThumbnailPlugin(
options=downloader_options,
overrides=overrides,
enhanced_download_archive=enhanced_download_archive,
),
UrlDownloaderCollectionVariablePlugin(
downloader_options=downloader_options,
overrides=overrides,
enhanced_download_archive=enhanced_download_archive,
),
]
@classmethod @classmethod
def ytdl_option_defaults(cls) -> Dict: def ytdl_option_defaults(cls) -> Dict:

View file

@ -174,23 +174,14 @@ class SubscriptionDownload(BaseSubscription, ABC):
------- -------
List of plugins defined in the subscription, initialized and ready to use. List of plugins defined in the subscription, initialized and ready to use.
""" """
# Always add plugins provided by the downloader return [
plugins: List[Plugin] = self.downloader_class.added_plugins( plugin_type(
downloader_options=self.downloader_options,
enhanced_download_archive=self._enhanced_download_archive,
overrides=self.overrides,
)
for plugin_type, plugin_options in self.plugins.zipped():
plugin = plugin_type(
options=plugin_options, options=plugin_options,
overrides=self.overrides, overrides=self.overrides,
enhanced_download_archive=self._enhanced_download_archive, enhanced_download_archive=self._enhanced_download_archive,
) )
for plugin_type, plugin_options in self.plugins.zipped()
plugins.append(plugin) ]
return plugins
@classmethod @classmethod
def _cleanup_entry_files(cls, entry: Entry): def _cleanup_entry_files(cls, entry: Entry):
@ -347,6 +338,8 @@ class SubscriptionDownload(BaseSubscription, ABC):
overrides=self.overrides, overrides=self.overrides,
) )
plugins.extend(downloader.added_plugins())
return self._process_subscription( return self._process_subscription(
plugins=plugins, plugins=plugins,
downloader=downloader, downloader=downloader,