]> jfr.im git - yt-dlp.git/blob - devscripts/lazy_load_template.py
[cleanup,build] Cleanup some build-related code
[yt-dlp.git] / devscripts / lazy_load_template.py
1 import re
2
3 from ..utils import bug_reports_message, write_string
4
5
6 class LazyLoadMetaClass(type):
7 def __getattr__(cls, name):
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()}')
12 return getattr(cls._get_real_class(), name)
13
14
15 class LazyLoadExtractor(metaclass=LazyLoadMetaClass):
16 _module = None
17 _WORKING = True
18
19 @classmethod
20 def _get_real_class(cls):
21 if '_real_class' not in cls.__dict__:
22 mod = __import__(cls._module, fromlist=(cls.__name__,))
23 cls._real_class = getattr(mod, cls.__name__)
24 return cls._real_class
25
26 def __new__(cls, *args, **kwargs):
27 real_cls = cls._get_real_class()
28 instance = real_cls.__new__(real_cls)
29 instance.__init__(*args, **kwargs)
30 return instance