]> jfr.im git - yt-dlp.git/blame - devscripts/lazy_load_template.py
[cleanup,build] Cleanup some build-related code
[yt-dlp.git] / devscripts / lazy_load_template.py
CommitLineData
779822d9
JMF
1import re
2
e6f21b3d 3from ..utils import bug_reports_message, write_string
4
779822d9 5
5bc4a65e 6class LazyLoadMetaClass(type):
7 def __getattr__(cls, name):
e6f21b3d 8 if '_real_class' not in cls.__dict__:
9 write_string(
10 f'WARNING: Falling back to normal extractor since lazy extractor '
11 f'{cls.__name__} does not have attribute {name}{bug_reports_message()}')
5bc4a65e 12 return getattr(cls._get_real_class(), name)
13
14
15class LazyLoadExtractor(metaclass=LazyLoadMetaClass):
779822d9 16 _module = None
251ae04e 17 _WORKING = True
779822d9 18
5bc4a65e 19 @classmethod
20 def _get_real_class(cls):
e6f21b3d 21 if '_real_class' not in cls.__dict__:
5bc4a65e 22 mod = __import__(cls._module, fromlist=(cls.__name__,))
e6f21b3d 23 cls._real_class = getattr(mod, cls.__name__)
24 return cls._real_class
5bc4a65e 25
8a5dc1c1 26 def __new__(cls, *args, **kwargs):
5bc4a65e 27 real_cls = cls._get_real_class()
8a5dc1c1
JMF
28 instance = real_cls.__new__(real_cls)
29 instance.__init__(*args, **kwargs)
30 return instance