You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.8 KiB

  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. from youtube_dl.extractor import _ALL_CLASSES
  13. from youtube_dl.extractor.common import InfoExtractor, SearchInfoExtractor
  14. with open('devscripts/lazy_load_template.py', 'rt') as f:
  15. module_template = f.read()
  16. module_contents = [
  17. module_template + '\n' + getsource(InfoExtractor.suitable) + '\n',
  18. 'class LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
  19. ie_template = '''
  20. class {name}({bases}):
  21. _VALID_URL = {valid_url!r}
  22. _module = '{module}'
  23. '''
  24. make_valid_template = '''
  25. @classmethod
  26. def _make_valid_url(cls):
  27. return {valid_url!r}
  28. '''
  29. def get_base_name(base):
  30. if base is InfoExtractor:
  31. return 'LazyLoadExtractor'
  32. elif base is SearchInfoExtractor:
  33. return 'LazyLoadSearchExtractor'
  34. else:
  35. return base.__name__
  36. def build_lazy_ie(ie, name):
  37. valid_url = getattr(ie, '_VALID_URL', None)
  38. s = ie_template.format(
  39. name=name,
  40. bases=', '.join(map(get_base_name, ie.__bases__)),
  41. valid_url=valid_url,
  42. module=ie.__module__)
  43. if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
  44. s += '\n' + getsource(ie.suitable)
  45. if hasattr(ie, '_make_valid_url'):
  46. # search extractors
  47. s += make_valid_template.format(valid_url=ie._make_valid_url())
  48. return s
  49. # find the correct sorting and add the required base classes so that sublcasses
  50. # can be correctly created
  51. classes = _ALL_CLASSES[:-1]
  52. ordered_cls = []
  53. while classes:
  54. for c in classes[:]:
  55. bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))
  56. stop = False
  57. for b in bases:
  58. if b not in classes and b not in ordered_cls:
  59. if b.__name__ == 'GenericIE':
  60. exit()
  61. classes.insert(0, b)
  62. stop = True
  63. if stop:
  64. break
  65. if all(b in ordered_cls for b in bases):
  66. ordered_cls.append(c)
  67. classes.remove(c)
  68. break
  69. ordered_cls.append(_ALL_CLASSES[-1])
  70. names = []
  71. for ie in ordered_cls:
  72. name = ie.__name__
  73. src = build_lazy_ie(ie, name)
  74. module_contents.append(src)
  75. if ie in _ALL_CLASSES:
  76. names.append(name)
  77. module_contents.append(
  78. '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
  79. module_src = '\n'.join(module_contents) + '\n'
  80. with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
  81. f.write(module_src)