]> jfr.im git - yt-dlp.git/blob - devscripts/lazy_load_template.py
[extractor] Use classmethod/property where possible
[yt-dlp.git] / devscripts / lazy_load_template.py
1 import importlib
2 import random
3 import re
4
5 from ..utils import bug_reports_message, classproperty, write_string
6
7
8 class LazyLoadMetaClass(type):
9 def __getattr__(cls, name):
10 # "is_suitable" requires "_TESTS". However, they bloat the lazy_extractors
11 if '_real_class' not in cls.__dict__ and name not in ('is_suitable', 'get_testcases'):
12 write_string(
13 'WARNING: Falling back to normal extractor since lazy extractor '
14 f'{cls.__name__} does not have attribute {name}{bug_reports_message()}\n')
15 return getattr(cls.real_class, name)
16
17
18 class LazyLoadExtractor(metaclass=LazyLoadMetaClass):
19 @classproperty
20 def real_class(cls):
21 if '_real_class' not in cls.__dict__:
22 cls._real_class = getattr(importlib.import_module(cls._module), cls.__name__)
23 return cls._real_class
24
25 def __new__(cls, *args, **kwargs):
26 instance = cls.real_class.__new__(cls.real_class)
27 instance.__init__(*args, **kwargs)
28 return instance