]>
Commit | Line | Data |
---|---|---|
82d02080 | 1 | import importlib |
2 | import random | |
779822d9 JMF |
3 | import re |
4 | ||
24146491 | 5 | from ..utils import ( |
6 | age_restricted, | |
7 | bug_reports_message, | |
8 | classproperty, | |
5fd83674 | 9 | variadic, |
24146491 | 10 | write_string, |
11 | ) | |
e6f21b3d | 12 | |
8f97a15d | 13 | # These bloat the lazy_extractors, so allow them to passthrough silently |
6368e2e6 | 14 | ALLOWED_CLASSMETHODS = {'extract_from_webpage', 'get_testcases', 'get_webpage_testcases'} |
e5458d1d | 15 | _WARNED = False |
8f97a15d | 16 | |
779822d9 | 17 | |
5bc4a65e | 18 | class LazyLoadMetaClass(type): |
19 | def __getattr__(cls, name): | |
e5458d1d | 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') | |
82d02080 | 26 | return getattr(cls.real_class, name) |
5bc4a65e | 27 | |
28 | ||
29 | class LazyLoadExtractor(metaclass=LazyLoadMetaClass): | |
82d02080 | 30 | @classproperty |
31 | def real_class(cls): | |
e6f21b3d | 32 | if '_real_class' not in cls.__dict__: |
82d02080 | 33 | cls._real_class = getattr(importlib.import_module(cls._module), cls.__name__) |
e6f21b3d | 34 | return cls._real_class |
5bc4a65e | 35 | |
8a5dc1c1 | 36 | def __new__(cls, *args, **kwargs): |
82d02080 | 37 | instance = cls.real_class.__new__(cls.real_class) |
8a5dc1c1 JMF |
38 | instance.__init__(*args, **kwargs) |
39 | return instance |