make_lazy_extractors.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from __future__ import unicode_literals, print_function
  2. from inspect import getsource
  3. import io
  4. import os
  5. from os.path import dirname as dirn
  6. import sys
  7. from youtube_dl.compat import compat_register_utf8
  8. compat_register_utf8()
  9. print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
  10. sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
  11. lazy_extractors_filename = sys.argv[1]
  12. if os.path.exists(lazy_extractors_filename):
  13. os.remove(lazy_extractors_filename)
  14. # Py2: may be confused by leftover lazy_extractors.pyc
  15. try:
  16. os.remove(lazy_extractors_filename + 'c')
  17. except OSError:
  18. pass
  19. from youtube_dl.extractor import _ALL_CLASSES
  20. from youtube_dl.extractor.common import InfoExtractor, SearchInfoExtractor
  21. with open('devscripts/lazy_load_template.py', 'rt') as f:
  22. module_template = f.read()
  23. module_contents = [
  24. module_template + '\n' + getsource(InfoExtractor.suitable) + '\n',
  25. 'class LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
  26. # needed for suitable() methods of Youtube extractor (see #28780)
  27. 'from youtube_dl.utils import parse_qs\n',
  28. ]
  29. ie_template = '''
  30. class {name}({bases}):
  31. _VALID_URL = {valid_url!r}
  32. _module = '{module}'
  33. '''
  34. make_valid_template = '''
  35. @classmethod
  36. def _make_valid_url(cls):
  37. return {valid_url!r}
  38. '''
  39. def get_base_name(base):
  40. if base is InfoExtractor:
  41. return 'LazyLoadExtractor'
  42. elif base is SearchInfoExtractor:
  43. return 'LazyLoadSearchExtractor'
  44. else:
  45. return base.__name__
  46. def build_lazy_ie(ie, name):
  47. valid_url = getattr(ie, '_VALID_URL', None)
  48. s = ie_template.format(
  49. name=name,
  50. bases=', '.join(map(get_base_name, ie.__bases__)),
  51. valid_url=valid_url,
  52. module=ie.__module__)
  53. if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
  54. s += '\n' + getsource(ie.suitable)
  55. if hasattr(ie, '_make_valid_url'):
  56. # search extractors
  57. s += make_valid_template.format(valid_url=ie._make_valid_url())
  58. return s
  59. # find the correct sorting and add the required base classes so that subclasses
  60. # can be correctly created
  61. classes = _ALL_CLASSES[:-1]
  62. ordered_cls = []
  63. while classes:
  64. for c in classes[:]:
  65. bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))
  66. stop = False
  67. for b in bases:
  68. if b not in classes and b not in ordered_cls:
  69. if b.__name__ == 'GenericIE':
  70. exit()
  71. classes.insert(0, b)
  72. stop = True
  73. if stop:
  74. break
  75. if all(b in ordered_cls for b in bases):
  76. ordered_cls.append(c)
  77. classes.remove(c)
  78. break
  79. ordered_cls.append(_ALL_CLASSES[-1])
  80. names = []
  81. for ie in ordered_cls:
  82. name = ie.__name__
  83. src = build_lazy_ie(ie, name)
  84. module_contents.append(src)
  85. if ie in _ALL_CLASSES:
  86. names.append(name)
  87. module_contents.append(
  88. '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
  89. module_src = '\n'.join(module_contents) + '\n'
  90. with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
  91. f.write(module_src)