2022-04-29 03:48:36 +02:00
|
|
|
import collections
|
2022-04-24 18:28:18 +02:00
|
|
|
import contextlib
|
|
|
|
import importlib
|
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
|
2022-04-29 03:48:36 +02:00
|
|
|
_NO_ATTRIBUTE = object()
|
|
|
|
|
|
|
|
_Package = collections.namedtuple('Package', ('name', 'version'))
|
|
|
|
|
|
|
|
|
|
|
|
def get_package_info(module):
|
|
|
|
parent = module.__name__.split('.')[0]
|
|
|
|
parent_module = None
|
|
|
|
with contextlib.suppress(ImportError):
|
|
|
|
parent_module = importlib.import_module(parent)
|
|
|
|
|
|
|
|
for attr in ('__version__', 'version_string', 'version'):
|
|
|
|
version = getattr(parent_module, attr, None)
|
|
|
|
if version is not None:
|
|
|
|
break
|
|
|
|
return _Package(getattr(module, '_yt_dlp__identifier', parent), str(version))
|
|
|
|
|
|
|
|
|
2022-04-24 18:28:18 +02:00
|
|
|
def _is_package(module):
|
2023-02-08 02:55:36 +01:00
|
|
|
return '__path__' in vars(module)
|
|
|
|
|
|
|
|
|
|
|
|
class EnhancedModule(types.ModuleType):
|
|
|
|
def __new__(cls, name, *args, **kwargs):
|
|
|
|
if name not in sys.modules:
|
|
|
|
return super().__new__(cls, name, *args, **kwargs)
|
|
|
|
|
|
|
|
assert not args and not kwargs, 'Cannot pass additional arguments to an existing module'
|
|
|
|
module = sys.modules[name]
|
|
|
|
module.__class__ = cls
|
|
|
|
return module
|
|
|
|
|
|
|
|
def __init__(self, name, *args, **kwargs):
|
|
|
|
# Prevent __new__ from trigerring __init__ again
|
|
|
|
if name not in sys.modules:
|
|
|
|
super().__init__(name, *args, **kwargs)
|
|
|
|
|
|
|
|
def __bool__(self):
|
|
|
|
return vars(self).get('__bool__', lambda: True)()
|
|
|
|
|
|
|
|
def __getattribute__(self, attr):
|
|
|
|
try:
|
|
|
|
ret = super().__getattribute__(attr)
|
|
|
|
except AttributeError:
|
|
|
|
if attr.startswith('__') and attr.endswith('__'):
|
|
|
|
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 18:28:18 +02:00
|
|
|
|
|
|
|
|
2022-06-24 12:10:13 +02:00
|
|
|
def passthrough_module(parent, child, allowed_attributes=None, *, callback=lambda _: None):
|
2023-02-08 02:55:36 +01:00
|
|
|
"""Passthrough parent module into a child module, creating the parent if necessary"""
|
|
|
|
parent = EnhancedModule(parent)
|
2022-06-24 12:10:13 +02:00
|
|
|
|
2023-02-08 02:55:36 +01:00
|
|
|
def __getattr__(attr):
|
|
|
|
if _is_package(parent):
|
|
|
|
with contextlib.suppress(ImportError):
|
|
|
|
return importlib.import_module(f'.{attr}', parent.__name__)
|
2022-06-15 14:30:34 +02:00
|
|
|
|
2023-02-08 02:55:36 +01: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 18:28:18 +02:00
|
|
|
|
2023-02-08 02:55:36 +01:00
|
|
|
def from_child(attr):
|
|
|
|
nonlocal child
|
2022-04-24 18:28:18 +02:00
|
|
|
|
2023-02-08 02:55:36 +01:00
|
|
|
if allowed_attributes is None:
|
|
|
|
if attr.startswith('__') and attr.endswith('__'):
|
|
|
|
return _NO_ATTRIBUTE
|
|
|
|
elif attr not in allowed_attributes:
|
2022-06-24 12:10:13 +02:00
|
|
|
return _NO_ATTRIBUTE
|
2022-04-24 18:28:18 +02:00
|
|
|
|
2023-02-08 02:55:36 +01:00
|
|
|
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 _NO_ATTRIBUTE
|
|
|
|
|
|
|
|
parent.__getattr__ = __getattr__
|
|
|
|
return parent
|