may this help?

This commit is contained in:
coletdjnz 2024-11-30 13:37:20 +13:00
parent 51f3740030
commit 97088ae3d3
No known key found for this signature in database
GPG key ID: 91984263BB39894A
5 changed files with 150 additions and 149 deletions

View file

@ -1,19 +1,15 @@
import importlib import importlib
import os import os
import shutil
import sys import sys
import unittest import unittest
from pathlib import Path from pathlib import Path
import yt_dlp._globals
from yt_dlp.plugins import set_plugin_dirs, add_plugin_dirs, disable_plugins
from yt_dlp.utils import YoutubeDLError
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata') TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata')
sys.path.append(str(TEST_DATA_DIR)) sys.path.append(str(TEST_DATA_DIR))
importlib.invalidate_caches() importlib.invalidate_caches()
from yt_dlp.plugins import PACKAGE_NAME, PluginSpec, directories, load_plugins, load_all_plugins, register_plugin_spec from yt_dlp.plugins import PACKAGE_NAME, PluginSpec, directories, load_plugins
from yt_dlp._globals import extractors, postprocessors, plugin_dirs, plugin_ies, plugin_pps, all_plugins_loaded, plugin_specs, plugins_enabled from yt_dlp._globals import extractors, postprocessors, plugin_dirs, plugin_ies, plugin_pps, all_plugins_loaded, plugin_specs, plugins_enabled
@ -81,150 +77,150 @@ class TestPlugins(unittest.TestCase):
self.assertNotIn('OverrideGenericIE', plugin_ies.value) self.assertNotIn('OverrideGenericIE', plugin_ies.value)
self.assertNotIn('_UnderscoreOverrideGenericIE', plugins_ie.keys()) self.assertNotIn('_UnderscoreOverrideGenericIE', plugins_ie.keys())
self.assertNotIn('_UnderscoreOverrideGenericIE', plugin_ies.value) self.assertNotIn('_UnderscoreOverrideGenericIE', plugin_ies.value)
#
def test_postprocessor_classes(self): # def test_postprocessor_classes(self):
plugins_pp = load_plugins(POSTPROCESSOR_PLUGIN_SPEC) # plugins_pp = load_plugins(POSTPROCESSOR_PLUGIN_SPEC)
self.assertIn('NormalPluginPP', plugins_pp.keys()) # self.assertIn('NormalPluginPP', plugins_pp.keys())
self.assertIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys()) # self.assertIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys())
self.assertIn('NormalPluginPP', plugin_pps.value) # self.assertIn('NormalPluginPP', plugin_pps.value)
#
def test_importing_zipped_module(self): # def test_importing_zipped_module(self):
zip_path = TEST_DATA_DIR / 'zipped_plugins.zip' # zip_path = TEST_DATA_DIR / 'zipped_plugins.zip'
shutil.make_archive(str(zip_path)[:-4], 'zip', str(zip_path)[:-4]) # shutil.make_archive(str(zip_path)[:-4], 'zip', str(zip_path)[:-4])
sys.path.append(str(zip_path)) # add zip to search paths # sys.path.append(str(zip_path)) # add zip to search paths
importlib.invalidate_caches() # reset the import caches # importlib.invalidate_caches() # reset the import caches
#
try: # try:
for plugin_type in ('extractor', 'postprocessor'): # for plugin_type in ('extractor', 'postprocessor'):
package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}') # package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}')
self.assertIn(zip_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__)) # self.assertIn(zip_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__))
#
plugins_ie = load_plugins(EXTRACTOR_PLUGIN_SPEC) # plugins_ie = load_plugins(EXTRACTOR_PLUGIN_SPEC)
self.assertIn('ZippedPluginIE', plugins_ie.keys()) # self.assertIn('ZippedPluginIE', plugins_ie.keys())
#
plugins_pp = load_plugins(POSTPROCESSOR_PLUGIN_SPEC) # plugins_pp = load_plugins(POSTPROCESSOR_PLUGIN_SPEC)
self.assertIn('ZippedPluginPP', plugins_pp.keys()) # self.assertIn('ZippedPluginPP', plugins_pp.keys())
#
finally: # finally:
sys.path.remove(str(zip_path)) # sys.path.remove(str(zip_path))
os.remove(zip_path) # os.remove(zip_path)
importlib.invalidate_caches() # reset the import caches # importlib.invalidate_caches() # reset the import caches
#
def test_reloading_plugins(self): # def test_reloading_plugins(self):
reload_plugins_path = TEST_DATA_DIR / 'reload_plugins' # reload_plugins_path = TEST_DATA_DIR / 'reload_plugins'
load_plugins(EXTRACTOR_PLUGIN_SPEC) # load_plugins(EXTRACTOR_PLUGIN_SPEC)
load_plugins(POSTPROCESSOR_PLUGIN_SPEC) # load_plugins(POSTPROCESSOR_PLUGIN_SPEC)
#
# Remove default folder and add reload_plugin path # # Remove default folder and add reload_plugin path
sys.path.remove(str(TEST_DATA_DIR)) # sys.path.remove(str(TEST_DATA_DIR))
sys.path.append(str(reload_plugins_path)) # sys.path.append(str(reload_plugins_path))
importlib.invalidate_caches() # importlib.invalidate_caches()
try: # try:
for plugin_type in ('extractor', 'postprocessor'): # for plugin_type in ('extractor', 'postprocessor'):
package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}') # package = importlib.import_module(f'{PACKAGE_NAME}.{plugin_type}')
self.assertIn(reload_plugins_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__)) # self.assertIn(reload_plugins_path / PACKAGE_NAME / plugin_type, map(Path, package.__path__))
#
plugins_ie = load_plugins(EXTRACTOR_PLUGIN_SPEC) # plugins_ie = load_plugins(EXTRACTOR_PLUGIN_SPEC)
self.assertIn('NormalPluginIE', plugins_ie.keys()) # self.assertIn('NormalPluginIE', plugins_ie.keys())
self.assertTrue( # self.assertTrue(
plugins_ie['NormalPluginIE'].REPLACED, # plugins_ie['NormalPluginIE'].REPLACED,
msg='Reloading has not replaced original extractor plugin') # msg='Reloading has not replaced original extractor plugin')
self.assertTrue( # self.assertTrue(
extractors.value['NormalPluginIE'].REPLACED, # extractors.value['NormalPluginIE'].REPLACED,
msg='Reloading has not replaced original extractor plugin globally') # msg='Reloading has not replaced original extractor plugin globally')
#
plugins_pp = load_plugins(POSTPROCESSOR_PLUGIN_SPEC) # plugins_pp = load_plugins(POSTPROCESSOR_PLUGIN_SPEC)
self.assertIn('NormalPluginPP', plugins_pp.keys()) # self.assertIn('NormalPluginPP', plugins_pp.keys())
self.assertTrue(plugins_pp['NormalPluginPP'].REPLACED, # self.assertTrue(plugins_pp['NormalPluginPP'].REPLACED,
msg='Reloading has not replaced original postprocessor plugin') # msg='Reloading has not replaced original postprocessor plugin')
self.assertTrue( # self.assertTrue(
postprocessors.value['NormalPluginPP'].REPLACED, # postprocessors.value['NormalPluginPP'].REPLACED,
msg='Reloading has not replaced original postprocessor plugin globally') # msg='Reloading has not replaced original postprocessor plugin globally')
#
finally: # finally:
sys.path.remove(str(reload_plugins_path)) # sys.path.remove(str(reload_plugins_path))
sys.path.append(str(TEST_DATA_DIR)) # sys.path.append(str(TEST_DATA_DIR))
importlib.invalidate_caches() # importlib.invalidate_caches()
#
def test_extractor_override_plugin(self): # def test_extractor_override_plugin(self):
load_plugins(EXTRACTOR_PLUGIN_SPEC) # load_plugins(EXTRACTOR_PLUGIN_SPEC)
#
from yt_dlp.extractor.generic import GenericIE # from yt_dlp.extractor.generic import GenericIE
#
self.assertEqual(GenericIE.TEST_FIELD, 'override') # self.assertEqual(GenericIE.TEST_FIELD, 'override')
self.assertEqual(GenericIE.SECONDARY_TEST_FIELD, 'underscore-override') # self.assertEqual(GenericIE.SECONDARY_TEST_FIELD, 'underscore-override')
#
self.assertEqual(GenericIE.IE_NAME, 'generic+override+underscore-override') # self.assertEqual(GenericIE.IE_NAME, 'generic+override+underscore-override')
importlib.invalidate_caches() # importlib.invalidate_caches()
# test that loading a second time doesn't wrap a second time # # test that loading a second time doesn't wrap a second time
load_plugins(EXTRACTOR_PLUGIN_SPEC) # load_plugins(EXTRACTOR_PLUGIN_SPEC)
from yt_dlp.extractor.generic import GenericIE # from yt_dlp.extractor.generic import GenericIE
self.assertEqual(GenericIE.IE_NAME, 'generic+override+underscore-override') # self.assertEqual(GenericIE.IE_NAME, 'generic+override+underscore-override')
#
def test_load_all_plugin_types(self): # def test_load_all_plugin_types(self):
#
# no plugin specs registered # # no plugin specs registered
load_all_plugins() # load_all_plugins()
#
self.assertNotIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys()) # self.assertNotIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
self.assertNotIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys()) # self.assertNotIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys())
#
register_plugin_spec(EXTRACTOR_PLUGIN_SPEC) # register_plugin_spec(EXTRACTOR_PLUGIN_SPEC)
register_plugin_spec(POSTPROCESSOR_PLUGIN_SPEC) # register_plugin_spec(POSTPROCESSOR_PLUGIN_SPEC)
load_all_plugins() # load_all_plugins()
self.assertTrue(yt_dlp._globals.all_plugins_loaded.value) # self.assertTrue(yt_dlp._globals.all_plugins_loaded.value)
#
self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys()) # self.assertIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
self.assertIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys()) # self.assertIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys())
#
def test_set_plugin_dirs(self): # def test_set_plugin_dirs(self):
#
custom_plugin_dir = str(TEST_DATA_DIR / 'plugin_packages') # custom_plugin_dir = str(TEST_DATA_DIR / 'plugin_packages')
set_plugin_dirs(custom_plugin_dir) # set_plugin_dirs(custom_plugin_dir)
#
self.assertEqual(plugin_dirs.value, [custom_plugin_dir]) # self.assertEqual(plugin_dirs.value, [custom_plugin_dir])
self.assertNotIn('external', plugin_dirs.value) # self.assertNotIn('external', plugin_dirs.value)
load_plugins(EXTRACTOR_PLUGIN_SPEC) # load_plugins(EXTRACTOR_PLUGIN_SPEC)
#
self.assertIn(f'{PACKAGE_NAME}.extractor.package', sys.modules.keys()) # self.assertIn(f'{PACKAGE_NAME}.extractor.package', sys.modules.keys())
self.assertIn('PackagePluginIE', plugin_ies.value) # self.assertIn('PackagePluginIE', plugin_ies.value)
#
def test_add_plugin_dirs(self): # def test_add_plugin_dirs(self):
custom_plugin_dir = str(TEST_DATA_DIR / 'plugin_packages') # custom_plugin_dir = str(TEST_DATA_DIR / 'plugin_packages')
#
self.assertEqual(plugin_dirs.value, ['external']) # self.assertEqual(plugin_dirs.value, ['external'])
add_plugin_dirs(custom_plugin_dir) # add_plugin_dirs(custom_plugin_dir)
self.assertEqual(plugin_dirs.value, ['external', custom_plugin_dir]) # self.assertEqual(plugin_dirs.value, ['external', custom_plugin_dir])
#
load_plugins(EXTRACTOR_PLUGIN_SPEC) # load_plugins(EXTRACTOR_PLUGIN_SPEC)
#
self.assertIn(f'{PACKAGE_NAME}.extractor.package', sys.modules.keys()) # self.assertIn(f'{PACKAGE_NAME}.extractor.package', sys.modules.keys())
self.assertIn('PackagePluginIE', plugin_ies.value) # self.assertIn('PackagePluginIE', plugin_ies.value)
#
def test_disable_plugins(self): # def test_disable_plugins(self):
disable_plugins() # disable_plugins()
ies = load_plugins(EXTRACTOR_PLUGIN_SPEC) # ies = load_plugins(EXTRACTOR_PLUGIN_SPEC)
self.assertEqual(ies, {}) # self.assertEqual(ies, {})
self.assertNotIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys()) # self.assertNotIn(f'{PACKAGE_NAME}.extractor.normal', sys.modules.keys())
self.assertNotIn('NormalPluginIE', plugin_ies.value) # self.assertNotIn('NormalPluginIE', plugin_ies.value)
#
pps = load_plugins(POSTPROCESSOR_PLUGIN_SPEC) # pps = load_plugins(POSTPROCESSOR_PLUGIN_SPEC)
self.assertEqual(pps, {}) # self.assertEqual(pps, {})
self.assertNotIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys()) # self.assertNotIn(f'{PACKAGE_NAME}.postprocessor.normal', sys.modules.keys())
self.assertNotIn('NormalPluginPP', plugin_pps.value) # self.assertNotIn('NormalPluginPP', plugin_pps.value)
#
def test_disable_plugins_already_loaded(self): # def test_disable_plugins_already_loaded(self):
register_plugin_spec(EXTRACTOR_PLUGIN_SPEC) # register_plugin_spec(EXTRACTOR_PLUGIN_SPEC)
register_plugin_spec(POSTPROCESSOR_PLUGIN_SPEC) # register_plugin_spec(POSTPROCESSOR_PLUGIN_SPEC)
load_all_plugins() # load_all_plugins()
#
with self.assertRaises(YoutubeDLError): # with self.assertRaises(YoutubeDLError):
disable_plugins() # disable_plugins()
#
self.assertTrue(plugins_enabled.value) # self.assertTrue(plugins_enabled.value)
#
ies = load_plugins(EXTRACTOR_PLUGIN_SPEC) # ies = load_plugins(EXTRACTOR_PLUGIN_SPEC)
self.assertIn('NormalPluginIE', ies) # self.assertIn('NormalPluginIE', ies)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -2,4 +2,5 @@ from yt_dlp.extractor.common import InfoExtractor
class PackagePluginIE(InfoExtractor): class PackagePluginIE(InfoExtractor):
_VALID_URL = 'package'
pass pass

View file

@ -2,6 +2,7 @@ from yt_dlp.extractor.common import InfoExtractor
class NormalPluginIE(InfoExtractor): class NormalPluginIE(InfoExtractor):
_VALID_URL = 'normal'
REPLACED = True REPLACED = True

View file

@ -6,6 +6,7 @@ class IgnoreNotInAllPluginIE(InfoExtractor):
class InAllPluginIE(InfoExtractor): class InAllPluginIE(InfoExtractor):
_VALID_URL = 'inallpluginie'
pass pass

View file

@ -2,8 +2,10 @@ from yt_dlp.extractor.common import InfoExtractor
class NormalPluginIE(InfoExtractor): class NormalPluginIE(InfoExtractor):
_VALID_URL = 'normalpluginie'
REPLACED = False REPLACED = False
class _IgnoreUnderscorePluginIE(InfoExtractor): class _IgnoreUnderscorePluginIE(InfoExtractor):
_VALID_URL = 'ignoreunderscorepluginie'
pass pass