From 413ae7693bc0497bf539c381b4495ef8ede78c9e Mon Sep 17 00:00:00 2001 From: coletdjnz Date: Sat, 19 Oct 2024 16:25:36 +1300 Subject: [PATCH] Make globals internal-only --- test/test_plugins.py | 2 +- yt_dlp/YoutubeDL.py | 2 +- yt_dlp/__init__.py | 2 +- yt_dlp/{globals.py => _globals.py} | 2 +- yt_dlp/extractor/__init__.py | 2 +- yt_dlp/extractor/extractors.py | 4 ++-- yt_dlp/plugins.py | 9 +++++---- yt_dlp/postprocessor/__init__.py | 2 +- yt_dlp/utils/_utils.py | 2 +- 9 files changed, 14 insertions(+), 13 deletions(-) rename yt_dlp/{globals.py => _globals.py} (91%) diff --git a/test/test_plugins.py b/test/test_plugins.py index b0fc5182b..6d0b7ac83 100644 --- a/test/test_plugins.py +++ b/test/test_plugins.py @@ -11,7 +11,7 @@ sys.path.append(str(TEST_DATA_DIR)) importlib.invalidate_caches() from yt_dlp.plugins import PACKAGE_NAME, PluginType, directories, load_plugins -from yt_dlp.globals import extractors, postprocessors +from yt_dlp._globals import extractors, postprocessors class TestPlugins(unittest.TestCase): diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 17b4d241b..5375ad6a0 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -33,7 +33,7 @@ from .downloader.rtmp import rtmpdump_version from .extractor import gen_extractor_classes, get_info_extractor, import_extractors from .extractor.common import UnsupportedURLIE from .extractor.openload import PhantomJSwrapper -from .globals import ( +from ._globals import ( IN_CLI, LAZY_EXTRACTORS, plugin_ies, diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py index bf09993e4..360599b9c 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -20,7 +20,7 @@ from .downloader.external import get_external_downloader from .extractor import list_extractor_classes from .extractor.adobepass import MSO_INFO from .networking.impersonate import ImpersonateTarget -from .globals import IN_CLI, plugin_dirs +from ._globals import IN_CLI, plugin_dirs from .options import parseOpts from .plugins import load_all_plugin_types from .postprocessor import ( diff --git a/yt_dlp/globals.py b/yt_dlp/_globals.py similarity index 91% rename from yt_dlp/globals.py rename to yt_dlp/_globals.py index 52e98fb71..c91f07eca 100644 --- a/yt_dlp/globals.py +++ b/yt_dlp/_globals.py @@ -1,7 +1,7 @@ from collections import defaultdict from contextvars import ContextVar -# NAME = 'yt-dlp' +# Internal only - no backwards compatibility guaranteed postprocessors = ContextVar('postprocessors', default={}) extractors = ContextVar('extractors', default={}) diff --git a/yt_dlp/extractor/__init__.py b/yt_dlp/extractor/__init__.py index cd0f483cd..75e659215 100644 --- a/yt_dlp/extractor/__init__.py +++ b/yt_dlp/extractor/__init__.py @@ -1,5 +1,5 @@ +from .._globals import extractors as _extractors_context from ..compat.compat_utils import passthrough_module -from ..globals import extractors as _extractors_context passthrough_module(__name__, '.extractors') del passthrough_module diff --git a/yt_dlp/extractor/extractors.py b/yt_dlp/extractor/extractors.py index 590c886ef..bbee3a702 100644 --- a/yt_dlp/extractor/extractors.py +++ b/yt_dlp/extractor/extractors.py @@ -1,8 +1,8 @@ import inspect import os -from ..globals import LAZY_EXTRACTORS -from ..globals import extractors as _extractors_context +from .._globals import LAZY_EXTRACTORS +from .._globals import extractors as _extractors_context _CLASS_LOOKUP = None if not os.environ.get('YTDLP_NO_LAZY_EXTRACTORS'): diff --git a/yt_dlp/plugins.py b/yt_dlp/plugins.py index c0888ee2f..a9e7b08b3 100644 --- a/yt_dlp/plugins.py +++ b/yt_dlp/plugins.py @@ -17,7 +17,7 @@ from contextvars import ContextVar from pathlib import Path from zipfile import ZipFile -from .globals import ( +from ._globals import ( extractors, plugin_dirs, plugin_ies, @@ -171,7 +171,7 @@ def get_regular_modules(module, module_name, suffix): and not obj.__name__.startswith('_') and obj.__name__ in getattr(module, '__all__', [obj.__name__]) and getattr(obj, '_plugin_name', None) is None - )) + )) load_module = get_regular_modules @@ -184,7 +184,7 @@ def get_override_modules(module, module_name, suffix): return False mro = inspect.getmro(obj) return ( - obj.__module__.startswith(module_name) + obj.__module__.startswith(module_name) and getattr(obj, '_plugin_name', None) is not None and mro[mro.index(obj) + 1].__name__.endswith(suffix) ) @@ -268,7 +268,7 @@ def load_plugins(plugin_type: PluginType): regular_classes.update(get_regular_modules(plugins, spec.name, suffix)) # Configure override classes - for name, klass in override_classes.items(): + for _, klass in override_classes.items(): plugin_name = getattr(klass, '_plugin_name', None) if not plugin_name: # these should always have plugin_name @@ -298,6 +298,7 @@ def load_all_plugin_types(): for plugin_type in PluginType: load_plugins(plugin_type) + sys.meta_path.insert(0, PluginFinder(f'{PACKAGE_NAME}.extractor', f'{PACKAGE_NAME}.postprocessor')) __all__ = [ diff --git a/yt_dlp/postprocessor/__init__.py b/yt_dlp/postprocessor/__init__.py index 0d76ba733..866fe3d1c 100644 --- a/yt_dlp/postprocessor/__init__.py +++ b/yt_dlp/postprocessor/__init__.py @@ -33,7 +33,7 @@ from .movefilesafterdownload import MoveFilesAfterDownloadPP from .sponskrub import SponSkrubPP from .sponsorblock import SponsorBlockPP from .xattrpp import XAttrMetadataPP -from ..globals import plugin_pps, postprocessors +from .._globals import plugin_pps, postprocessors from ..plugins import PACKAGE_NAME from ..utils import deprecation_warning diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index 0b61a77ac..57ace73e3 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -52,7 +52,7 @@ from ..compat import ( compat_os_name, ) from ..dependencies import xattr -from ..globals import IN_CLI +from .._globals import IN_CLI __name__ = __name__.rsplit('.', 1)[0] # noqa: A001: Pretend to be the parent module