]>
Commit | Line | Data |
---|---|---|
1 | import importlib | |
2 | import random | |
3 | import re | |
4 | ||
5 | from ..utils import ( | |
6 | age_restricted, | |
7 | bug_reports_message, | |
8 | classproperty, | |
9 | variadic, | |
10 | write_string, | |
11 | ) | |
12 | ||
13 | # These bloat the lazy_extractors, so allow them to passthrough silently | |
14 | ALLOWED_CLASSMETHODS = {'extract_from_webpage', 'get_testcases', 'get_webpage_testcases'} | |
15 | _WARNED = False | |
16 | ||
17 | ||
18 | class LazyLoadMetaClass(type): | |
19 | def __getattr__(cls, name): | |
20 | global _WARNED | |
21 | if ('_real_class' not in cls.__dict__ | |
22 | and name not in ALLOWED_CLASSMETHODS and not _WARNED): | |
23 | _WARNED = True | |
24 | write_string('WARNING: Falling back to normal extractor since lazy extractor ' | |
25 | f'{cls.__name__} does not have attribute {name}{bug_reports_message()}\n') | |
26 | return getattr(cls.real_class, name) | |
27 | ||
28 | ||
29 | class LazyLoadExtractor(metaclass=LazyLoadMetaClass): | |
30 | @classproperty | |
31 | def real_class(cls): | |
32 | if '_real_class' not in cls.__dict__: | |
33 | cls._real_class = getattr(importlib.import_module(cls._module), cls.__name__) | |
34 | return cls._real_class | |
35 | ||
36 | def __new__(cls, *args, **kwargs): | |
37 | instance = cls.real_class.__new__(cls.real_class) | |
38 | instance.__init__(*args, **kwargs) | |
39 | return instance |