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