A feature-rich command-line audio/video downloader
Add option `--plugin-dirs` (#11277)
Imran Hussain0f593dca9fa9
diff --git a/README.md b/README.mdindex 1cafe51d51..fc38a529a7 100644--- a/README.md+++ b/README.md@@ -348,6 +348,13 @@ If you fork the project on GitHub, you can run your fork's [build workflow](.gitcontaining directory ("-" for stdin). Can beused multiple times and inside otherconfiguration files+ --plugin-dirs PATH Path to an additional directory to search+ for plugins. This option can be used+ multiple times to add multiple directories.+ Note that this currently only works for+ extractor plugins; postprocessor plugins can+ only be loaded from the default plugin+ directories--flat-playlist Do not extract the videos of a playlist,only list them--no-flat-playlist Fully extract the videos of a playlistdiff --git a/test/test_plugins.py b/test/test_plugins.pyindex c82158e9fc..77545d136c 100644--- a/test/test_plugins.py+++ b/test/test_plugins.py@@ -10,6 +10,7 @@ TEST_DATA_DIR = Path(os.path.dirname(os.path.abspath(__file__)), 'testdata')sys.path.append(str(TEST_DATA_DIR))importlib.invalidate_caches()+from yt_dlp.utils import Configfrom yt_dlp.plugins import PACKAGE_NAME, directories, load_plugins@@ -68,6 +69,24 @@ class TestPlugins(unittest.TestCase):os.remove(zip_path)importlib.invalidate_caches() # reset the import caches+ def test_plugin_dirs(self):+ # Internal plugin dirs hack for CLI --plugin-dirs+ # To be replaced with proper system later+ custom_plugin_dir = TEST_DATA_DIR / 'plugin_packages'+ Config._plugin_dirs = [str(custom_plugin_dir)]+ importlib.invalidate_caches() # reset the import caches++ try:+ package = importlib.import_module(f'{PACKAGE_NAME}.extractor')+ self.assertIn(custom_plugin_dir / 'testpackage' / PACKAGE_NAME / 'extractor', map(Path, package.__path__))++ plugins_ie = load_plugins('extractor', 'IE')+ self.assertIn('PackagePluginIE', plugins_ie.keys())++ finally:+ Config._plugin_dirs = []+ importlib.invalidate_caches() # reset the import caches+if __name__ == '__main__':unittest.main()diff --git a/test/testdata/plugin_packages/testpackage/yt_dlp_plugins/extractor/package.py b/test/testdata/plugin_packages/testpackage/yt_dlp_plugins/extractor/package.pynew file mode 100644index 0000000000..b860300d8d--- /dev/null+++ b/test/testdata/plugin_packages/testpackage/yt_dlp_plugins/extractor/package.py@@ -0,0 +1,5 @@+from yt_dlp.extractor.common import InfoExtractor+++class PackagePluginIE(InfoExtractor):+ passdiff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.pyindex f598b6c2fe..d976f5bbcb 100644--- a/yt_dlp/__init__.py+++ b/yt_dlp/__init__.py@@ -34,6 +34,7 @@ from .postprocessor import ()from .update import Updaterfrom .utils import (+ Config,NO_DEFAULT,POSTPROCESS_WHEN,DateRange,@@ -967,6 +968,10 @@ def _real_main(argv=None):parser, opts, all_urls, ydl_opts = parse_options(argv)+ # HACK: Set the plugin dirs early on+ # TODO(coletdjnz): remove when plugin globals system is implemented+ Config._plugin_dirs = opts.plugin_dirs+# Dump user agentif opts.dump_user_agent:ua = traverse_obj(opts.headers, 'User-Agent', casesense=False, default=std_headers['User-Agent'])diff --git a/yt_dlp/options.py b/yt_dlp/options.pyindex 9980b7fc3f..c3a647da77 100644--- a/yt_dlp/options.py+++ b/yt_dlp/options.py@@ -408,6 +408,14 @@ def create_parser():help=('Location of the main configuration file; either the path to the config or its containing directory ''("-" for stdin). Can be used multiple times and inside other configuration files'))+ general.add_option(+ '--plugin-dirs',+ dest='plugin_dirs', metavar='PATH', action='append',+ help=(+ 'Path to an additional directory to search for plugins. '+ 'This option can be used multiple times to add multiple directories. '+ 'Note that this currently only works for extractor plugins; '+ 'postprocessor plugins can only be loaded from the default plugin directories'))general.add_option('--flat-playlist',action='store_const', dest='extract_flat', const='in_playlist', default=False,diff --git a/yt_dlp/plugins.py b/yt_dlp/plugins.pyindex d777d14e71..204558d603 100644--- a/yt_dlp/plugins.py+++ b/yt_dlp/plugins.py@@ -15,6 +15,7 @@ from zipfile import ZipFilefrom .compat import functools # isort: splitfrom .utils import (+ Config,get_executable_path,get_system_config_dirs,get_user_config_dirs,@@ -84,6 +85,12 @@ class PluginFinder(importlib.abc.MetaPathFinder):with contextlib.suppress(ValueError): # Added when running __main__.py directlycandidate_locations.remove(Path(__file__).parent)+ # TODO(coletdjnz): remove when plugin globals system is implemented+ if Config._plugin_dirs:+ candidate_locations.extend(_get_package_paths(+ *Config._plugin_dirs,+ containing_folder=''))+parts = Path(*fullname.split('.'))for path in orderedSet(candidate_locations, lazy=True):candidate = path / partsdiff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.pyindex 27ebfefbcb..ea748898f2 100644--- a/yt_dlp/utils/_utils.py+++ b/yt_dlp/utils/_utils.py@@ -4897,6 +4897,10 @@ class Config:filename = None__initialized = False+ # Internal only, do not use! Hack to enable --plugin-dirs+ # TODO(coletdjnz): remove when plugin globals system is implemented+ _plugin_dirs = None+def __init__(self, parser, label=None):self.parser, self.label = parser, labelself._loaded_paths, self.configs = set(), []