2022-04-29 01:48:36 +00:00
|
|
|
import collections
|
2022-04-24 16:28:18 +00:00
|
|
|
import contextlib
|
2023-02-08 02:44:36 +00:00
|
|
|
import functools
|
2022-04-24 16:28:18 +00:00
|
|
|
import importlib
|
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
|
2022-04-29 01:48:36 +00:00
|
|
|
_NO_ATTRIBUTE = object()
|
|
|
|
|
|
|
|
_Package = collections.namedtuple('Package', ('name', 'version'))
|
|
|
|
|
|
|
|
|
|
|
|
def get_package_info(module):
|
2023-02-06 21:52:29 +00:00
|
|
|
return _Package(
|
|
|
|
name=getattr(module, '_yt_dlp__identifier', module.__name__),
|
|
|
|
version=str(next(filter(None, (
|
|
|
|
getattr(module, attr, None)
|
|
|
|
for attr in ('__version__', 'version_string', 'version')
|
|
|
|
)), None)))
|
2022-04-29 01:48:36 +00:00
|
|
|
|
|
|
|
|
2022-04-24 16:28:18 +00:00
|
|
|
def _is_package(module):
|
2023-02-08 01:55:36 +00:00
|
|
|
return '__path__' in vars(module)
|
|
|
|
|
|
|
|
|
2023-02-08 02:44:36 +00:00
|
|
|
def _is_dunder(name):
|
|
|
|
return name.startswith('__') and name.endswith('__')
|
|
|
|
|
|
|
|
|
2023-02-08 01:55:36 +00:00
|
|
|
class EnhancedModule(types.ModuleType):
|
|
|
|
def __bool__(self):
|
|
|
|
return vars(self).get('__bool__', lambda: True)()
|
|
|
|
|
|
|
|
def __getattribute__(self, attr):
|
|
|
|
try:
|
|
|
|
ret = super().__getattribute__(attr)
|
|
|
|
except AttributeError:
|
2023-02-08 02:44:36 +00:00
|
|
|
if _is_dunder(attr):
|
2023-02-08 01:55:36 +00:00
|
|
|
raise
|
|
|
|
getter = getattr(self, '__getattr__', None)
|
|
|
|
if not getter:
|
|
|
|
raise
|
|
|
|
ret = getter(attr)
|
|
|
|
return ret.fget() if isinstance(ret, property) else ret
|
2022-04-24 16:28:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 02:44:36 +00:00
|
|
|
def passthrough_module(parent, child, allowed_attributes=(..., ), *, callback=lambda _: None):
|
2023-02-08 01:55:36 +00:00
|
|
|
"""Passthrough parent module into a child module, creating the parent if necessary"""
|
|
|
|
def __getattr__(attr):
|
|
|
|
if _is_package(parent):
|
|
|
|
with contextlib.suppress(ImportError):
|
|
|
|
return importlib.import_module(f'.{attr}', parent.__name__)
|
2022-06-15 12:30:34 +00:00
|
|
|
|
2023-02-08 01:55:36 +00:00
|
|
|
ret = from_child(attr)
|
|
|
|
if ret is _NO_ATTRIBUTE:
|
|
|
|
raise AttributeError(f'module {parent.__name__} has no attribute {attr}')
|
|
|
|
callback(attr)
|
|
|
|
return ret
|
2022-04-24 16:28:18 +00:00
|
|
|
|
2023-02-08 02:44:36 +00:00
|
|
|
@functools.lru_cache(maxsize=None)
|
2023-02-08 01:55:36 +00:00
|
|
|
def from_child(attr):
|
|
|
|
nonlocal child
|
2023-02-08 02:44:36 +00:00
|
|
|
if attr not in allowed_attributes:
|
|
|
|
if ... not in allowed_attributes or _is_dunder(attr):
|
2023-02-08 01:55:36 +00:00
|
|
|
return _NO_ATTRIBUTE
|
2022-04-24 16:28:18 +00:00
|
|
|
|
2023-02-08 01:55:36 +00:00
|
|
|
if isinstance(child, str):
|
|
|
|
child = importlib.import_module(child, parent.__name__)
|
|
|
|
|
|
|
|
if _is_package(child):
|
|
|
|
with contextlib.suppress(ImportError):
|
2023-02-08 02:44:36 +00:00
|
|
|
return passthrough_module(f'{parent.__name__}.{attr}',
|
|
|
|
importlib.import_module(f'.{attr}', child.__name__))
|
|
|
|
|
|
|
|
with contextlib.suppress(AttributeError):
|
|
|
|
return getattr(child, attr)
|
2023-02-08 01:55:36 +00:00
|
|
|
|
|
|
|
return _NO_ATTRIBUTE
|
|
|
|
|
2023-02-08 20:04:39 +00:00
|
|
|
parent = sys.modules.get(parent, types.ModuleType(parent))
|
|
|
|
parent.__class__ = EnhancedModule
|
2023-02-08 01:55:36 +00:00
|
|
|
parent.__getattr__ = __getattr__
|
|
|
|
return parent
|