lazy_load_template.py 965 B

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