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