lazy_load_template.py 981 B

12345678910111213141516171819202122232425262728293031
  1. # coding: utf-8
  2. import re
  3. from ..utils import bug_reports_message, write_string
  4. class LazyLoadMetaClass(type):
  5. def __getattr__(cls, name):
  6. if '_real_class' not in cls.__dict__:
  7. write_string(
  8. f'WARNING: Falling back to normal extractor since lazy extractor '
  9. f'{cls.__name__} does not have attribute {name}{bug_reports_message()}')
  10. return getattr(cls._get_real_class(), name)
  11. class LazyLoadExtractor(metaclass=LazyLoadMetaClass):
  12. _module = None
  13. _WORKING = True
  14. @classmethod
  15. def _get_real_class(cls):
  16. if '_real_class' not in cls.__dict__:
  17. mod = __import__(cls._module, fromlist=(cls.__name__,))
  18. cls._real_class = getattr(mod, cls.__name__)
  19. return cls._real_class
  20. def __new__(cls, *args, **kwargs):
  21. real_cls = cls._get_real_class()
  22. instance = real_cls.__new__(real_cls)
  23. instance.__init__(*args, **kwargs)
  24. return instance