]> jfr.im git - yt-dlp.git/blob - devscripts/make_lazy_extractors.py
[cleanup] Upgrade syntax
[yt-dlp.git] / devscripts / make_lazy_extractors.py
1 #!/usr/bin/env python3
2 from inspect import getsource
3 import os
4 from os.path import dirname as dirn
5 import sys
6
7 sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))
8
9 lazy_extractors_filename = sys.argv[1] if len(sys.argv) > 1 else 'yt_dlp/extractor/lazy_extractors.py'
10 if os.path.exists(lazy_extractors_filename):
11 os.remove(lazy_extractors_filename)
12
13 # Block plugins from loading
14 plugins_dirname = 'ytdlp_plugins'
15 plugins_blocked_dirname = 'ytdlp_plugins_blocked'
16 if os.path.exists(plugins_dirname):
17 os.rename(plugins_dirname, plugins_blocked_dirname)
18
19 from yt_dlp.extractor import _ALL_CLASSES
20 from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor
21
22 if os.path.exists(plugins_blocked_dirname):
23 os.rename(plugins_blocked_dirname, plugins_dirname)
24
25 with open('devscripts/lazy_load_template.py') as f:
26 module_template = f.read()
27
28 CLASS_PROPERTIES = ['ie_key', 'working', '_match_valid_url', 'suitable', '_match_id', 'get_temp_id']
29 module_contents = [
30 module_template,
31 *[getsource(getattr(InfoExtractor, k)) for k in CLASS_PROPERTIES],
32 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
33
34 ie_template = '''
35 class {name}({bases}):
36 _module = '{module}'
37 '''
38
39
40 def 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
49 def build_lazy_ie(ie, name):
50 s = ie_template.format(
51 name=name,
52 bases=', '.join(map(get_base_name, ie.__bases__)),
53 module=ie.__module__)
54 valid_url = getattr(ie, '_VALID_URL', None)
55 if not valid_url and hasattr(ie, '_make_valid_url'):
56 valid_url = ie._make_valid_url()
57 if valid_url:
58 s += f' _VALID_URL = {valid_url!r}\n'
59 if not ie._WORKING:
60 s += ' _WORKING = False\n'
61 if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
62 s += f'\n{getsource(ie.suitable)}'
63 return s
64
65
66 # find the correct sorting and add the required base classes so that subclasses
67 # can be correctly created
68 classes = _ALL_CLASSES[:-1]
69 ordered_cls = []
70 while classes:
71 for c in classes[:]:
72 bases = set(c.__bases__) - {object, InfoExtractor, SearchInfoExtractor}
73 stop = False
74 for b in bases:
75 if b not in classes and b not in ordered_cls:
76 if b.__name__ == 'GenericIE':
77 exit()
78 classes.insert(0, b)
79 stop = True
80 if stop:
81 break
82 if all(b in ordered_cls for b in bases):
83 ordered_cls.append(c)
84 classes.remove(c)
85 break
86 ordered_cls.append(_ALL_CLASSES[-1])
87
88 names = []
89 for ie in ordered_cls:
90 name = ie.__name__
91 src = build_lazy_ie(ie, name)
92 module_contents.append(src)
93 if ie in _ALL_CLASSES:
94 names.append(name)
95
96 module_contents.append(
97 '\n_ALL_CLASSES = [{}]'.format(', '.join(names)))
98
99 module_src = '\n'.join(module_contents) + '\n'
100
101 with open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
102 f.write(module_src)