From 3561d2a08f483b17e6a03b9051f6e1febcde9fc2 Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Sat, 19 Oct 2024 16:52:53 +1300 Subject: [PATCH] load plugins in youtubedl if they haven't already --- test/test_YoutubeDL.py | 8 ++++++++ yt_dlp/YoutubeDL.py | 8 ++++++-- yt_dlp/_globals.py | 3 +++ yt_dlp/plugins.py | 3 ++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index a99e62408..ece57d2ce 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -6,6 +6,8 @@ import sys import unittest from unittest.mock import patch +from yt_dlp._globals import ALL_PLUGINS_LOADED + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -1396,6 +1398,12 @@ class TestYoutubeDL(unittest.TestCase): self.assertFalse(result.get('cookies'), msg='Cookies set in cookies field for wrong domain') self.assertFalse(ydl.cookiejar.get_cookie_header(fmt['url']), msg='Cookies set in cookiejar for wrong domain') + def test_load_plugins_compat(self): + # Should try to reload plugins if they haven't already been loaded + ALL_PLUGINS_LOADED.set(False) + FakeYDL().close() + assert ALL_PLUGINS_LOADED.get() + if __name__ == '__main__': unittest.main() diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 5375ad6a0..2928b4387 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -38,7 +38,7 @@ from ._globals import ( LAZY_EXTRACTORS, plugin_ies, plugin_overrides, - plugin_pps, + plugin_pps, ALL_PLUGINS_LOADED, ) from .minicurses import format_text from .networking import HEADRequest, Request, RequestDirector @@ -51,7 +51,7 @@ from .networking.exceptions import ( network_exceptions, ) from .networking.impersonate import ImpersonateRequestHandler -from .plugins import directories as plugin_directories +from .plugins import directories as plugin_directories, load_all_plugin_types from .postprocessor import ( EmbedThumbnailPP, FFmpegFixupDuplicateMoovPP, @@ -645,6 +645,10 @@ class YoutubeDL: self.cache = Cache(self) self.__header_cookies = [] + # compat for API: load plugins if they have not already + if not ALL_PLUGINS_LOADED.get(): + load_all_plugin_types() + stdout = sys.stderr if self.params.get('logtostderr') else sys.stdout self._out_files = Namespace( out=stdout, diff --git a/yt_dlp/_globals.py b/yt_dlp/_globals.py index da0095425..b7b462e5c 100644 --- a/yt_dlp/_globals.py +++ b/yt_dlp/_globals.py @@ -9,6 +9,9 @@ IN_CLI = ContextVar('IN_CLI', default=False) # `False`=force, `None`=disabled, `True`=enabled LAZY_EXTRACTORS = ContextVar('LAZY_EXTRACTORS', default=False) +# Whether plugins have been loaded once +ALL_PLUGINS_LOADED = ContextVar('PLUGINS_LOADED', default=False) + # `...`=search default plugin dirs plugin_dirs = ContextVar('plugin_dirs', default=(..., )) plugin_ies = ContextVar('plugin_ies', default={}) diff --git a/yt_dlp/plugins.py b/yt_dlp/plugins.py index a32929ccc..0fb396227 100644 --- a/yt_dlp/plugins.py +++ b/yt_dlp/plugins.py @@ -22,7 +22,7 @@ from ._globals import ( plugin_dirs, plugin_ies, plugin_pps, - postprocessors, plugin_overrides, + postprocessors, plugin_overrides, ALL_PLUGINS_LOADED, ) from .compat import functools # isort: split @@ -297,6 +297,7 @@ def load_plugins(plugin_type: PluginType): def load_all_plugin_types(): for plugin_type in PluginType: load_plugins(plugin_type) + ALL_PLUGINS_LOADED.set(True) sys.meta_path.insert(0, PluginFinder(f'{PACKAGE_NAME}.extractor', f'{PACKAGE_NAME}.postprocessor'))