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