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