mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-11-17 14:41:44 +00:00
[compat_utils] Improve passthrough_module
This commit is contained in:
parent
f6a765ceb5
commit
88426d9446
2
Makefile
2
Makefile
@ -74,7 +74,7 @@ offlinetest: codetest
|
|||||||
$(PYTHON) -m pytest -k "not download"
|
$(PYTHON) -m pytest -k "not download"
|
||||||
|
|
||||||
# XXX: This is hard to maintain
|
# 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
|
yt-dlp: yt_dlp/*.py yt_dlp/*/*.py
|
||||||
mkdir -p zip
|
mkdir -p zip
|
||||||
for d in $(CODE_FOLDERS) ; do \
|
for d in $(CODE_FOLDERS) ; do \
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import collections
|
import collections
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import functools
|
||||||
import importlib
|
import importlib
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
@ -22,6 +23,10 @@ def _is_package(module):
|
|||||||
return '__path__' in vars(module)
|
return '__path__' in vars(module)
|
||||||
|
|
||||||
|
|
||||||
|
def _is_dunder(name):
|
||||||
|
return name.startswith('__') and name.endswith('__')
|
||||||
|
|
||||||
|
|
||||||
class EnhancedModule(types.ModuleType):
|
class EnhancedModule(types.ModuleType):
|
||||||
def __new__(cls, name, *args, **kwargs):
|
def __new__(cls, name, *args, **kwargs):
|
||||||
if name not in sys.modules:
|
if name not in sys.modules:
|
||||||
@ -44,7 +49,7 @@ class EnhancedModule(types.ModuleType):
|
|||||||
try:
|
try:
|
||||||
ret = super().__getattribute__(attr)
|
ret = super().__getattribute__(attr)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
if attr.startswith('__') and attr.endswith('__'):
|
if _is_dunder(attr):
|
||||||
raise
|
raise
|
||||||
getter = getattr(self, '__getattr__', None)
|
getter = getattr(self, '__getattr__', None)
|
||||||
if not getter:
|
if not getter:
|
||||||
@ -53,7 +58,7 @@ class EnhancedModule(types.ModuleType):
|
|||||||
return ret.fget() if isinstance(ret, property) else ret
|
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"""
|
"""Passthrough parent module into a child module, creating the parent if necessary"""
|
||||||
parent = EnhancedModule(parent)
|
parent = EnhancedModule(parent)
|
||||||
|
|
||||||
@ -68,24 +73,23 @@ def passthrough_module(parent, child, allowed_attributes=None, *, callback=lambd
|
|||||||
callback(attr)
|
callback(attr)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=None)
|
||||||
def from_child(attr):
|
def from_child(attr):
|
||||||
nonlocal child
|
nonlocal child
|
||||||
|
if attr not in allowed_attributes:
|
||||||
if allowed_attributes is None:
|
if ... not in allowed_attributes or _is_dunder(attr):
|
||||||
if attr.startswith('__') and attr.endswith('__'):
|
|
||||||
return _NO_ATTRIBUTE
|
return _NO_ATTRIBUTE
|
||||||
elif attr not in allowed_attributes:
|
|
||||||
return _NO_ATTRIBUTE
|
|
||||||
|
|
||||||
if isinstance(child, str):
|
if isinstance(child, str):
|
||||||
child = importlib.import_module(child, parent.__name__)
|
child = importlib.import_module(child, parent.__name__)
|
||||||
|
|
||||||
with contextlib.suppress(AttributeError):
|
|
||||||
return getattr(child, attr)
|
|
||||||
|
|
||||||
if _is_package(child):
|
if _is_package(child):
|
||||||
with contextlib.suppress(ImportError):
|
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
|
return _NO_ATTRIBUTE
|
||||||
|
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
import importlib
|
|
||||||
|
|
||||||
from ..compat import functools
|
from ..compat import functools
|
||||||
from ..compat.compat_utils import EnhancedModule, passthrough_module
|
from ..compat.compat_utils import EnhancedModule, passthrough_module
|
||||||
|
|
||||||
EnhancedModule(__name__)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import Cryptodome as _parent
|
import Cryptodome as _parent
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -14,14 +10,8 @@ except ImportError:
|
|||||||
_parent = EnhancedModule('Cryptodome')
|
_parent = EnhancedModule('Cryptodome')
|
||||||
__bool__ = lambda: False
|
__bool__ = lambda: False
|
||||||
|
|
||||||
|
passthrough_module(__name__, _parent, (..., '__version__'))
|
||||||
@functools.cache
|
del passthrough_module, EnhancedModule
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
Loading…
Reference in New Issue
Block a user