]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
779822d9 JMF |
2 | from __future__ import unicode_literals, print_function |
3 | ||
4 | from inspect import getsource | |
19f38218 | 5 | import io |
779822d9 JMF |
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 | ||
0b2e9d2c | 18 | # Block plugins from loading |
5c333d74 K |
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) | |
0b2e9d2c | 23 | |
7a5c1cfe P |
24 | from yt_dlp.extractor import _ALL_CLASSES |
25 | from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor | |
779822d9 | 26 | |
5c333d74 K |
27 | if os.path.exists(plugins_blocked_dirname): |
28 | os.rename(plugins_blocked_dirname, plugins_dirname) | |
0b2e9d2c | 29 | |
779822d9 JMF |
30 | with open('devscripts/lazy_load_template.py', 'rt') as f: |
31 | module_template = f.read() | |
32 | ||
169d836f | 33 | module_contents = [ |
5ad28e7f | 34 | module_template, |
3fb4e21b | 35 | getsource(InfoExtractor.ie_key), |
5ad28e7f | 36 | getsource(InfoExtractor._match_valid_url), |
37 | getsource(InfoExtractor.suitable), | |
38 | '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n'] | |
779822d9 JMF |
39 | |
40 | ie_template = ''' | |
169d836f | 41 | class {name}({bases}): |
779822d9 JMF |
42 | _VALID_URL = {valid_url!r} |
43 | _module = '{module}' | |
44 | ''' | |
45 | ||
46 | make_valid_template = ''' | |
47 | @classmethod | |
48 | def _make_valid_url(cls): | |
c1ce6acd | 49 | return {valid_url!r} |
779822d9 JMF |
50 | ''' |
51 | ||
52 | ||
169d836f JMF |
53 | def get_base_name(base): |
54 | if base is InfoExtractor: | |
55 | return 'LazyLoadExtractor' | |
56 | elif base is SearchInfoExtractor: | |
57 | return 'LazyLoadSearchExtractor' | |
58 | else: | |
59 | return base.__name__ | |
60 | ||
61 | ||
779822d9 JMF |
62 | def build_lazy_ie(ie, name): |
63 | valid_url = getattr(ie, '_VALID_URL', None) | |
64 | s = ie_template.format( | |
65 | name=name, | |
169d836f | 66 | bases=', '.join(map(get_base_name, ie.__bases__)), |
779822d9 JMF |
67 | valid_url=valid_url, |
68 | module=ie.__module__) | |
69 | if ie.suitable.__func__ is not InfoExtractor.suitable.__func__: | |
6b97ca96 | 70 | s += '\n' + getsource(ie.suitable) |
779822d9 JMF |
71 | if hasattr(ie, '_make_valid_url'): |
72 | # search extractors | |
c1ce6acd | 73 | s += make_valid_template.format(valid_url=ie._make_valid_url()) |
779822d9 JMF |
74 | return s |
75 | ||
582be358 | 76 | |
8bdd16b4 | 77 | # find the correct sorting and add the required base classes so that subclasses |
169d836f JMF |
78 | # can be correctly created |
79 | classes = _ALL_CLASSES[:-1] | |
80 | ordered_cls = [] | |
81 | while classes: | |
82 | for c in classes[:]: | |
83 | bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor)) | |
84 | stop = False | |
85 | for b in bases: | |
86 | if b not in classes and b not in ordered_cls: | |
87 | if b.__name__ == 'GenericIE': | |
88 | exit() | |
89 | classes.insert(0, b) | |
90 | stop = True | |
91 | if stop: | |
92 | break | |
93 | if all(b in ordered_cls for b in bases): | |
94 | ordered_cls.append(c) | |
95 | classes.remove(c) | |
96 | break | |
97 | ordered_cls.append(_ALL_CLASSES[-1]) | |
98 | ||
779822d9 | 99 | names = [] |
169d836f JMF |
100 | for ie in ordered_cls: |
101 | name = ie.__name__ | |
779822d9 JMF |
102 | src = build_lazy_ie(ie, name) |
103 | module_contents.append(src) | |
169d836f JMF |
104 | if ie in _ALL_CLASSES: |
105 | names.append(name) | |
779822d9 JMF |
106 | |
107 | module_contents.append( | |
3fb4e21b | 108 | '\n_ALL_CLASSES = [{0}]'.format(', '.join(names))) |
779822d9 | 109 | |
6b97ca96 | 110 | module_src = '\n'.join(module_contents) + '\n' |
779822d9 | 111 | |
19f38218 | 112 | with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f: |
779822d9 | 113 | f.write(module_src) |