]>
Commit | Line | Data |
---|---|---|
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 | CLASS_PROPERTIES = ['ie_key', 'working', '_match_valid_url', 'suitable', '_match_id', 'get_temp_id'] | |
34 | module_contents = [ | |
35 | module_template, | |
36 | *[getsource(getattr(InfoExtractor, k)) for k in CLASS_PROPERTIES], | |
37 | '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n'] | |
38 | ||
39 | ie_template = ''' | |
40 | class {name}({bases}): | |
41 | _module = '{module}' | |
42 | ''' | |
43 | ||
44 | make_valid_template = ''' | |
45 | @classmethod | |
46 | def _make_valid_url(cls): | |
47 | return {valid_url!r} | |
48 | ''' | |
49 | ||
50 | ||
51 | def get_base_name(base): | |
52 | if base is InfoExtractor: | |
53 | return 'LazyLoadExtractor' | |
54 | elif base is SearchInfoExtractor: | |
55 | return 'LazyLoadSearchExtractor' | |
56 | else: | |
57 | return base.__name__ | |
58 | ||
59 | ||
60 | def build_lazy_ie(ie, name): | |
61 | s = ie_template.format( | |
62 | name=name, | |
63 | bases=', '.join(map(get_base_name, ie.__bases__)), | |
64 | module=ie.__module__) | |
65 | valid_url = getattr(ie, '_VALID_URL', None) | |
66 | if valid_url: | |
67 | s += f' _VALID_URL = {valid_url!r}\n' | |
68 | if not ie._WORKING: | |
69 | s += f' _WORKING = False\n' | |
70 | if ie.suitable.__func__ is not InfoExtractor.suitable.__func__: | |
71 | s += f'\n{getsource(ie.suitable)}' | |
72 | if hasattr(ie, '_make_valid_url'): | |
73 | # search extractors | |
74 | s += make_valid_template.format(valid_url=ie._make_valid_url()) | |
75 | return s | |
76 | ||
77 | ||
78 | # find the correct sorting and add the required base classes so that subclasses | |
79 | # can be correctly created | |
80 | classes = _ALL_CLASSES[:-1] | |
81 | ordered_cls = [] | |
82 | while classes: | |
83 | for c in classes[:]: | |
84 | bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor)) | |
85 | stop = False | |
86 | for b in bases: | |
87 | if b not in classes and b not in ordered_cls: | |
88 | if b.__name__ == 'GenericIE': | |
89 | exit() | |
90 | classes.insert(0, b) | |
91 | stop = True | |
92 | if stop: | |
93 | break | |
94 | if all(b in ordered_cls for b in bases): | |
95 | ordered_cls.append(c) | |
96 | classes.remove(c) | |
97 | break | |
98 | ordered_cls.append(_ALL_CLASSES[-1]) | |
99 | ||
100 | names = [] | |
101 | for ie in ordered_cls: | |
102 | name = ie.__name__ | |
103 | src = build_lazy_ie(ie, name) | |
104 | module_contents.append(src) | |
105 | if ie in _ALL_CLASSES: | |
106 | names.append(name) | |
107 | ||
108 | module_contents.append( | |
109 | '\n_ALL_CLASSES = [{0}]'.format(', '.join(names))) | |
110 | ||
111 | module_src = '\n'.join(module_contents) + '\n' | |
112 | ||
113 | with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f: | |
114 | f.write(module_src) |