lazy_load_template.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import importlib
  2. import random
  3. import re
  4. from ..utils import (
  5. age_restricted,
  6. bug_reports_message,
  7. classproperty,
  8. variadic,
  9. write_string,
  10. )
  11. # These bloat the lazy_extractors, so allow them to passthrough silently
  12. ALLOWED_CLASSMETHODS = {'extract_from_webpage', 'get_testcases', 'get_webpage_testcases'}
  13. _WARNED = False
  14. class LazyLoadMetaClass(type):
  15. def __getattr__(cls, name):
  16. global _WARNED
  17. if ('_real_class' not in cls.__dict__
  18. and name not in ALLOWED_CLASSMETHODS and not _WARNED):
  19. _WARNED = True
  20. write_string('WARNING: Falling back to normal extractor since lazy extractor '
  21. f'{cls.__name__} does not have attribute {name}{bug_reports_message()}\n')
  22. return getattr(cls.real_class, name)
  23. class LazyLoadExtractor(metaclass=LazyLoadMetaClass):
  24. @classproperty
  25. def real_class(cls):
  26. if '_real_class' not in cls.__dict__:
  27. cls._real_class = getattr(importlib.import_module(cls._module), cls.__name__)
  28. return cls._real_class
  29. def __new__(cls, *args, **kwargs):
  30. instance = cls.real_class.__new__(cls.real_class)
  31. instance.__init__(*args, **kwargs)
  32. return instance