]>
Commit | Line | Data |
---|---|---|
dcdb292f | 1 | # coding: utf-8 |
779822d9 JMF |
2 | import re |
3 | ||
e6f21b3d | 4 | from ..utils import bug_reports_message, write_string |
5 | ||
779822d9 | 6 | |
5bc4a65e | 7 | class LazyLoadMetaClass(type): |
8 | def __getattr__(cls, name): | |
e6f21b3d | 9 | if '_real_class' not in cls.__dict__: |
10 | write_string( | |
11 | f'WARNING: Falling back to normal extractor since lazy extractor ' | |
12 | f'{cls.__name__} does not have attribute {name}{bug_reports_message()}') | |
5bc4a65e | 13 | return getattr(cls._get_real_class(), name) |
14 | ||
15 | ||
16 | class LazyLoadExtractor(metaclass=LazyLoadMetaClass): | |
779822d9 | 17 | _module = None |
251ae04e | 18 | _WORKING = True |
779822d9 | 19 | |
5bc4a65e | 20 | @classmethod |
21 | def _get_real_class(cls): | |
e6f21b3d | 22 | if '_real_class' not in cls.__dict__: |
5bc4a65e | 23 | mod = __import__(cls._module, fromlist=(cls.__name__,)) |
e6f21b3d | 24 | cls._real_class = getattr(mod, cls.__name__) |
25 | return cls._real_class | |
5bc4a65e | 26 | |
8a5dc1c1 | 27 | def __new__(cls, *args, **kwargs): |
5bc4a65e | 28 | real_cls = cls._get_real_class() |
8a5dc1c1 JMF |
29 | instance = real_cls.__new__(real_cls) |
30 | instance.__init__(*args, **kwargs) | |
31 | return instance |