make_lazy_extractors.py 3.0 KB

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