1 from __future__ import unicode_literals, print_function
3 from inspect import getsource
6 from os.path import dirname as dirn
9 print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
11 sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
13 lazy_extractors_filename = sys.argv[1]
14 if os.path.exists(lazy_extractors_filename):
15 os.remove(lazy_extractors_filename)
17 from youtube_dl.extractor import _ALL_CLASSES
18 from youtube_dl.extractor.common import InfoExtractor, SearchInfoExtractor
20 with open('devscripts/lazy_load_template.py', 'rt') as f:
21 module_template = f.read()
24 module_template + '\n' + getsource(InfoExtractor.suitable) + '\n',
25 'class LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
28 class {name}({bases}):
29 _VALID_URL = {valid_url!r}
33 make_valid_template = '''
35 def _make_valid_url(cls):
40 def get_base_name(base):
41 if base is InfoExtractor:
42 return 'LazyLoadExtractor'
43 elif base is SearchInfoExtractor:
44 return 'LazyLoadSearchExtractor'
49 def build_lazy_ie(ie, name):
50 valid_url = getattr(ie, '_VALID_URL', None)
51 s = ie_template.format(
53 bases=', '.join(map(get_base_name, ie.__bases__)),
56 if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
57 s += '\n' + getsource(ie.suitable)
58 if hasattr(ie, '_make_valid_url'):
60 s += make_valid_template.format(valid_url=ie._make_valid_url())
64 # find the correct sorting and add the required base classes so that sublcasses
65 # can be correctly created
66 classes = _ALL_CLASSES[:-1]
70 bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))
73 if b not in classes and b not in ordered_cls:
74 if b.__name__ == 'GenericIE':
80 if all(b in ordered_cls for b in bases):
84 ordered_cls.append(_ALL_CLASSES[-1])
87 for ie in ordered_cls:
89 src = build_lazy_ie(ie, name)
90 module_contents.append(src)
91 if ie in _ALL_CLASSES:
94 module_contents.append(
95 '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
97 module_src = '\n'.join(module_contents) + '\n'
99 with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f: