diff options
author | Jesús <heckyel@hyperbola.info> | 2022-05-17 10:10:39 +0800 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2022-05-17 10:10:39 +0800 |
commit | 4bbf329feb5a820ac21269fa426c95ca14d7af25 (patch) | |
tree | 2c147a162b4bddc7862ed5895f1f66edd9a675e8 /yt_dlp/compat/compat_utils.py | |
parent | e21342911839b7796a5c788a7c3f13b06d975c64 (diff) | |
parent | 5faf6528fb701724ac32e0a487f92281c7800bda (diff) | |
download | hypervideo-pre-4bbf329feb5a820ac21269fa426c95ca14d7af25.tar.lz hypervideo-pre-4bbf329feb5a820ac21269fa426c95ca14d7af25.tar.xz hypervideo-pre-4bbf329feb5a820ac21269fa426c95ca14d7af25.zip |
updated from upstream | 17/05/2022 at 10:10
Diffstat (limited to 'yt_dlp/compat/compat_utils.py')
-rw-r--r-- | yt_dlp/compat/compat_utils.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/yt_dlp/compat/compat_utils.py b/yt_dlp/compat/compat_utils.py new file mode 100644 index 000000000..b1d58f5b9 --- /dev/null +++ b/yt_dlp/compat/compat_utils.py @@ -0,0 +1,60 @@ +import collections +import contextlib +import importlib +import sys +import types + + +_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)) + + +def _is_package(module): + try: + module.__getattribute__('__path__') + except AttributeError: + return False + return True + + +def passthrough_module(parent, child, *, callback=lambda _: None): + parent_module = importlib.import_module(parent) + child_module = importlib.import_module(child, parent) + + class PassthroughModule(types.ModuleType): + def __getattr__(self, attr): + if _is_package(parent_module): + with contextlib.suppress(ImportError): + return importlib.import_module(f'.{attr}', parent) + + ret = _NO_ATTRIBUTE + with contextlib.suppress(AttributeError): + ret = getattr(child_module, attr) + + if _is_package(child_module): + with contextlib.suppress(ImportError): + ret = importlib.import_module(f'.{attr}', child) + + if ret is _NO_ATTRIBUTE: + raise AttributeError(f'module {parent} has no attribute {attr}') + + callback(attr) + return ret + + # Python 3.6 does not have module level __getattr__ + # https://peps.python.org/pep-0562/ + sys.modules[parent].__class__ = PassthroughModule |