2016-10-02 07:39:18 -04:00
|
|
|
# coding: utf-8
|
2016-02-10 08:01:31 -05:00
|
|
|
import re
|
|
|
|
|
2021-09-17 14:23:55 -04:00
|
|
|
from ..utils import bug_reports_message, write_string
|
|
|
|
|
2016-02-10 08:01:31 -05:00
|
|
|
|
2021-08-22 18:18:26 -04:00
|
|
|
class LazyLoadMetaClass(type):
|
|
|
|
def __getattr__(cls, name):
|
2021-09-17 14:23:55 -04:00
|
|
|
if '_real_class' not in cls.__dict__:
|
|
|
|
write_string(
|
|
|
|
f'WARNING: Falling back to normal extractor since lazy extractor '
|
|
|
|
f'{cls.__name__} does not have attribute {name}{bug_reports_message()}')
|
2021-08-22 18:18:26 -04:00
|
|
|
return getattr(cls._get_real_class(), name)
|
|
|
|
|
|
|
|
|
|
|
|
class LazyLoadExtractor(metaclass=LazyLoadMetaClass):
|
2016-02-10 08:01:31 -05:00
|
|
|
_module = None
|
2021-08-22 19:15:30 -04:00
|
|
|
_WORKING = True
|
2016-02-10 08:01:31 -05:00
|
|
|
|
2021-08-22 18:18:26 -04:00
|
|
|
@classmethod
|
|
|
|
def _get_real_class(cls):
|
2021-09-17 14:23:55 -04:00
|
|
|
if '_real_class' not in cls.__dict__:
|
2021-08-22 18:18:26 -04:00
|
|
|
mod = __import__(cls._module, fromlist=(cls.__name__,))
|
2021-09-17 14:23:55 -04:00
|
|
|
cls._real_class = getattr(mod, cls.__name__)
|
|
|
|
return cls._real_class
|
2021-08-22 18:18:26 -04:00
|
|
|
|
2016-02-21 06:46:14 -05:00
|
|
|
def __new__(cls, *args, **kwargs):
|
2021-08-22 18:18:26 -04:00
|
|
|
real_cls = cls._get_real_class()
|
2016-02-21 06:46:14 -05:00
|
|
|
instance = real_cls.__new__(real_cls)
|
|
|
|
instance.__init__(*args, **kwargs)
|
|
|
|
return instance
|