]> jfr.im git - yt-dlp.git/blame - devscripts/make_lazy_extractors.py
Merge branch 'abc' of https://github.com/adrianheine/youtube-dl into adrianheine-abc
[yt-dlp.git] / devscripts / make_lazy_extractors.py
CommitLineData
779822d9
JMF
1from __future__ import unicode_literals, print_function
2
3from inspect import getsource
19f38218 4import io
779822d9
JMF
5import os
6from os.path import dirname as dirn
7import sys
8
9print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
10
11sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
12
13lazy_extractors_filename = sys.argv[1]
14if os.path.exists(lazy_extractors_filename):
15 os.remove(lazy_extractors_filename)
16
cefecac1
U
17from youtube_dlc.extractor import _ALL_CLASSES
18from youtube_dlc.extractor.common import InfoExtractor, SearchInfoExtractor
779822d9
JMF
19
20with open('devscripts/lazy_load_template.py', 'rt') as f:
21 module_template = f.read()
22
169d836f
JMF
23module_contents = [
24 module_template + '\n' + getsource(InfoExtractor.suitable) + '\n',
25 'class LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
779822d9
JMF
26
27ie_template = '''
169d836f 28class {name}({bases}):
779822d9
JMF
29 _VALID_URL = {valid_url!r}
30 _module = '{module}'
31'''
32
33make_valid_template = '''
34 @classmethod
35 def _make_valid_url(cls):
c1ce6acd 36 return {valid_url!r}
779822d9
JMF
37'''
38
39
169d836f
JMF
40def get_base_name(base):
41 if base is InfoExtractor:
42 return 'LazyLoadExtractor'
43 elif base is SearchInfoExtractor:
44 return 'LazyLoadSearchExtractor'
45 else:
46 return base.__name__
47
48
779822d9
JMF
49def build_lazy_ie(ie, name):
50 valid_url = getattr(ie, '_VALID_URL', None)
51 s = ie_template.format(
52 name=name,
169d836f 53 bases=', '.join(map(get_base_name, ie.__bases__)),
779822d9
JMF
54 valid_url=valid_url,
55 module=ie.__module__)
56 if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
6b97ca96 57 s += '\n' + getsource(ie.suitable)
779822d9
JMF
58 if hasattr(ie, '_make_valid_url'):
59 # search extractors
c1ce6acd 60 s += make_valid_template.format(valid_url=ie._make_valid_url())
779822d9
JMF
61 return s
62
582be358 63
169d836f
JMF
64# find the correct sorting and add the required base classes so that sublcasses
65# can be correctly created
66classes = _ALL_CLASSES[:-1]
67ordered_cls = []
68while classes:
69 for c in classes[:]:
70 bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))
71 stop = False
72 for b in bases:
73 if b not in classes and b not in ordered_cls:
74 if b.__name__ == 'GenericIE':
75 exit()
76 classes.insert(0, b)
77 stop = True
78 if stop:
79 break
80 if all(b in ordered_cls for b in bases):
81 ordered_cls.append(c)
82 classes.remove(c)
83 break
84ordered_cls.append(_ALL_CLASSES[-1])
85
779822d9 86names = []
169d836f
JMF
87for ie in ordered_cls:
88 name = ie.__name__
779822d9
JMF
89 src = build_lazy_ie(ie, name)
90 module_contents.append(src)
169d836f
JMF
91 if ie in _ALL_CLASSES:
92 names.append(name)
779822d9
JMF
93
94module_contents.append(
c1ce6acd 95 '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
779822d9 96
6b97ca96 97module_src = '\n'.join(module_contents) + '\n'
779822d9 98
19f38218 99with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
779822d9 100 f.write(module_src)