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