]>
jfr.im git - yt-dlp.git/blob - yt_dlp/compat/compat_utils.py
8 _NO_ATTRIBUTE
= object()
10 _Package
= collections
.namedtuple('Package', ('name', 'version'))
13 def get_package_info(module
):
14 parent
= module
.__name
__.split('.')[0]
16 with contextlib
.suppress(ImportError):
17 parent_module
= importlib
.import_module(parent
)
19 for attr
in ('__version__', 'version_string', 'version'):
20 version
= getattr(parent_module
, attr
, None)
21 if version
is not None:
23 return _Package(getattr(module
, '_yt_dlp__identifier', parent
), str(version
))
26 def _is_package(module
):
28 module
.__getattribute
__('__path__')
29 except AttributeError:
34 def passthrough_module(parent
, child
, *, callback
=lambda _
: None):
35 parent_module
= importlib
.import_module(parent
)
36 child_module
= importlib
.import_module(child
, parent
)
38 class PassthroughModule(types
.ModuleType
):
39 def __getattr__(self
, attr
):
40 if _is_package(parent_module
):
41 with contextlib
.suppress(ImportError):
42 return importlib
.import_module(f
'.{attr}', parent
)
45 with contextlib
.suppress(AttributeError):
46 ret
= getattr(child_module
, attr
)
48 if _is_package(child_module
):
49 with contextlib
.suppress(ImportError):
50 ret
= importlib
.import_module(f
'.{attr}', child
)
52 if ret
is _NO_ATTRIBUTE
:
53 raise AttributeError(f
'module {parent} has no attribute {attr}')
58 # Python 3.6 does not have module level __getattr__
59 # https://peps.python.org/pep-0562/
60 sys
.modules
[parent
].__class
__ = PassthroughModule