]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
779822d9 | 2 | import os |
779822d9 | 3 | import sys |
f8271158 | 4 | from inspect import getsource |
779822d9 | 5 | |
e5a998f3 | 6 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
779822d9 | 7 | |
49a57e70 | 8 | lazy_extractors_filename = sys.argv[1] if len(sys.argv) > 1 else 'yt_dlp/extractor/lazy_extractors.py' |
779822d9 JMF |
9 | if os.path.exists(lazy_extractors_filename): |
10 | os.remove(lazy_extractors_filename) | |
11 | ||
0b2e9d2c | 12 | # Block plugins from loading |
5c333d74 K |
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) | |
0b2e9d2c | 17 | |
7a5c1cfe P |
18 | from yt_dlp.extractor import _ALL_CLASSES |
19 | from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor | |
779822d9 | 20 | |
5c333d74 K |
21 | if os.path.exists(plugins_blocked_dirname): |
22 | os.rename(plugins_blocked_dirname, plugins_dirname) | |
0b2e9d2c | 23 | |
86e5f3ed | 24 | with open('devscripts/lazy_load_template.py') as f: |
779822d9 JMF |
25 | module_template = f.read() |
26 | ||
251ae04e | 27 | CLASS_PROPERTIES = ['ie_key', 'working', '_match_valid_url', 'suitable', '_match_id', 'get_temp_id'] |
169d836f | 28 | module_contents = [ |
5ad28e7f | 29 | module_template, |
251ae04e | 30 | *[getsource(getattr(InfoExtractor, k)) for k in CLASS_PROPERTIES], |
5ad28e7f | 31 | '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n'] |
779822d9 JMF |
32 | |
33 | ie_template = ''' | |
169d836f | 34 | class {name}({bases}): |
779822d9 JMF |
35 | _module = '{module}' |
36 | ''' | |
37 | ||
779822d9 | 38 | |
169d836f JMF |
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 | ||
779822d9 | 48 | def build_lazy_ie(ie, name): |
779822d9 JMF |
49 | s = ie_template.format( |
50 | name=name, | |
169d836f | 51 | bases=', '.join(map(get_base_name, ie.__bases__)), |
779822d9 | 52 | module=ie.__module__) |
251ae04e | 53 | valid_url = getattr(ie, '_VALID_URL', None) |
2c4aaadd | 54 | if not valid_url and hasattr(ie, '_make_valid_url'): |
55 | valid_url = ie._make_valid_url() | |
251ae04e | 56 | if valid_url: |
57 | s += f' _VALID_URL = {valid_url!r}\n' | |
58 | if not ie._WORKING: | |
8b7491c8 | 59 | s += ' _WORKING = False\n' |
779822d9 | 60 | if ie.suitable.__func__ is not InfoExtractor.suitable.__func__: |
251ae04e | 61 | s += f'\n{getsource(ie.suitable)}' |
779822d9 JMF |
62 | return s |
63 | ||
582be358 | 64 | |
8bdd16b4 | 65 | # find the correct sorting and add the required base classes so that subclasses |
169d836f JMF |
66 | # can be correctly created |
67 | classes = _ALL_CLASSES[:-1] | |
68 | ordered_cls = [] | |
69 | while classes: | |
70 | for c in classes[:]: | |
86e5f3ed | 71 | bases = set(c.__bases__) - {object, InfoExtractor, SearchInfoExtractor} |
169d836f JMF |
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 | ||
779822d9 | 87 | names = [] |
169d836f JMF |
88 | for ie in ordered_cls: |
89 | name = ie.__name__ | |
779822d9 JMF |
90 | src = build_lazy_ie(ie, name) |
91 | module_contents.append(src) | |
169d836f JMF |
92 | if ie in _ALL_CLASSES: |
93 | names.append(name) | |
779822d9 JMF |
94 | |
95 | module_contents.append( | |
86e5f3ed | 96 | '\n_ALL_CLASSES = [{}]'.format(', '.join(names))) |
779822d9 | 97 | |
6b97ca96 | 98 | module_src = '\n'.join(module_contents) + '\n' |
779822d9 | 99 | |
86e5f3ed | 100 | with open(lazy_extractors_filename, 'wt', encoding='utf-8') as f: |
779822d9 | 101 | f.write(module_src) |