]> jfr.im git - yt-dlp.git/commitdiff
[compat_utils] Improve `passthrough_module`
authorpukkandan <redacted>
Wed, 8 Feb 2023 02:44:36 +0000 (08:14 +0530)
committerpukkandan <redacted>
Wed, 8 Feb 2023 02:53:36 +0000 (08:23 +0530)
Makefile
yt_dlp/compat/compat_utils.py
yt_dlp/dependencies/Cryptodome.py

index ca7d641ab832bf37d971e177ca472ea40d6bfa5a..d5d47629b95280c9fb569c71f006f4613fd3015d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -74,7 +74,7 @@ offlinetest: codetest
        $(PYTHON) -m pytest -k "not download"
 
 # XXX: This is hard to maintain
-CODE_FOLDERS = yt_dlp yt_dlp/downloader yt_dlp/extractor yt_dlp/postprocessor yt_dlp/compat
+CODE_FOLDERS = yt_dlp yt_dlp/downloader yt_dlp/extractor yt_dlp/postprocessor yt_dlp/compat yt_dlp/dependencies
 yt-dlp: yt_dlp/*.py yt_dlp/*/*.py
        mkdir -p zip
        for d in $(CODE_FOLDERS) ; do \
index 373389a4661b88391419e89435e4d05caa15e99a..f8679c98ecf7b10467d9564029e9d2cbfa775c0e 100644 (file)
@@ -1,5 +1,6 @@
 import collections
 import contextlib
+import functools
 import importlib
 import sys
 import types
@@ -22,6 +23,10 @@ def _is_package(module):
     return '__path__' in vars(module)
 
 
+def _is_dunder(name):
+    return name.startswith('__') and name.endswith('__')
+
+
 class EnhancedModule(types.ModuleType):
     def __new__(cls, name, *args, **kwargs):
         if name not in sys.modules:
@@ -44,7 +49,7 @@ def __getattribute__(self, attr):
         try:
             ret = super().__getattribute__(attr)
         except AttributeError:
-            if attr.startswith('__') and attr.endswith('__'):
+            if _is_dunder(attr):
                 raise
             getter = getattr(self, '__getattr__', None)
             if not getter:
@@ -53,7 +58,7 @@ def __getattribute__(self, attr):
         return ret.fget() if isinstance(ret, property) else ret
 
 
-def passthrough_module(parent, child, allowed_attributes=None, *, callback=lambda _: None):
+def passthrough_module(parent, child, allowed_attributes=(..., ), *, callback=lambda _: None):
     """Passthrough parent module into a child module, creating the parent if necessary"""
     parent = EnhancedModule(parent)
 
@@ -68,24 +73,23 @@ def __getattr__(attr):
         callback(attr)
         return ret
 
+    @functools.lru_cache(maxsize=None)
     def from_child(attr):
         nonlocal child
-
-        if allowed_attributes is None:
-            if attr.startswith('__') and attr.endswith('__'):
+        if attr not in allowed_attributes:
+            if ... not in allowed_attributes or _is_dunder(attr):
                 return _NO_ATTRIBUTE
-        elif attr not in allowed_attributes:
-            return _NO_ATTRIBUTE
 
         if isinstance(child, str):
             child = importlib.import_module(child, parent.__name__)
 
-        with contextlib.suppress(AttributeError):
-            return getattr(child, attr)
-
         if _is_package(child):
             with contextlib.suppress(ImportError):
-                return importlib.import_module(f'.{attr}', child.__name__)
+                return passthrough_module(f'{parent.__name__}.{attr}',
+                                          importlib.import_module(f'.{attr}', child.__name__))
+
+        with contextlib.suppress(AttributeError):
+            return getattr(child, attr)
 
         return _NO_ATTRIBUTE
 
index b95f45d720a5eb7d23f4221d45ec5131033b2ba7..580ce07533e492b3c38b34df1bf1c5025550cfe3 100644 (file)
@@ -1,10 +1,6 @@
-import importlib
-
 from ..compat import functools
 from ..compat.compat_utils import EnhancedModule, passthrough_module
 
-EnhancedModule(__name__)
-
 try:
     import Cryptodome as _parent
 except ImportError:
         _parent = EnhancedModule('Cryptodome')
         __bool__ = lambda: False
 
-
-@functools.cache
-def __getattr__(name):
-    try:
-        submodule = importlib.import_module(f'.{name}', _parent.__name__)
-    except ImportError:
-        return getattr(_parent, name)
-    return passthrough_module(f'{__name__}.{name}', submodule)
+passthrough_module(__name__, _parent, (..., '__version__'))
+del passthrough_module, EnhancedModule
 
 
 @property