]> jfr.im git - yt-dlp.git/blame - devscripts/make_lazy_extractors.py
[devscripts] Create `utils` and refactor
[yt-dlp.git] / devscripts / make_lazy_extractors.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
54007a45 2
3# Allow direct execution
21633673 4import os
779822d9
JMF
5import sys
6
e5a998f3 7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
779822d9 8
779822d9 9
54007a45 10from inspect import getsource
11
115add43 12from devscripts.utils import get_filename_args, read_file, write_file
13
82d02080 14NO_ATTR = object()
8f97a15d 15STATIC_CLASS_PROPERTIES = ['IE_NAME', 'IE_DESC', 'SEARCH_KEY', '_VALID_URL', '_WORKING', '_NETRC_MACHINE', 'age_limit']
82d02080 16CLASS_METHODS = [
24146491 17 'ie_key', 'working', 'description', 'suitable', '_match_valid_url', '_match_id', 'get_temp_id', 'is_suitable'
82d02080 18]
19IE_TEMPLATE = '''
169d836f 20class {name}({bases}):
82d02080 21 _module = {module!r}
779822d9 22'''
115add43 23MODULE_TEMPLATE = read_file('devscripts/lazy_load_template.py')
82d02080 24
25
26def main():
115add43 27 lazy_extractors_filename = get_filename_args(default_outfile='yt_dlp/extractor/lazy_extractors.py')
82d02080 28 if os.path.exists(lazy_extractors_filename):
29 os.remove(lazy_extractors_filename)
30
31 _ALL_CLASSES = get_all_ies() # Must be before import
32
33 from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor
34
35 DummyInfoExtractor = type('InfoExtractor', (InfoExtractor,), {'IE_NAME': NO_ATTR})
36 module_src = '\n'.join((
37 MODULE_TEMPLATE,
38 ' _module = None',
39 *extra_ie_code(DummyInfoExtractor),
40 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
41 *build_ies(_ALL_CLASSES, (InfoExtractor, SearchInfoExtractor), DummyInfoExtractor),
42 ))
43
115add43 44 write_file(lazy_extractors_filename, f'{module_src}\n')
82d02080 45
46
47def get_all_ies():
48 PLUGINS_DIRNAME = 'ytdlp_plugins'
49 BLOCKED_DIRNAME = f'{PLUGINS_DIRNAME}_blocked'
50 if os.path.exists(PLUGINS_DIRNAME):
51 os.rename(PLUGINS_DIRNAME, BLOCKED_DIRNAME)
52 try:
560738f3 53 from yt_dlp.extractor.extractors import _ALL_CLASSES
82d02080 54 finally:
55 if os.path.exists(BLOCKED_DIRNAME):
56 os.rename(BLOCKED_DIRNAME, PLUGINS_DIRNAME)
57 return _ALL_CLASSES
58
59
60def extra_ie_code(ie, base=None):
61 for var in STATIC_CLASS_PROPERTIES:
62 val = getattr(ie, var)
63 if val != (getattr(base, var) if base else NO_ATTR):
64 yield f' {var} = {val!r}'
65 yield ''
66
67 for name in CLASS_METHODS:
68 f = getattr(ie, name)
69 if not base or f.__func__ != getattr(base, name).__func__:
70 yield getsource(f)
71
72
73def build_ies(ies, bases, attr_base):
74 names = []
75 for ie in sort_ies(ies, bases):
76 yield build_lazy_ie(ie, ie.__name__, attr_base)
77 if ie in ies:
78 names.append(ie.__name__)
79
80 yield f'\n_ALL_CLASSES = [{", ".join(names)}]'
81
82
83def sort_ies(ies, ignored_bases):
84 """find the correct sorting and add the required base classes so that subclasses can be correctly created"""
85 classes, returned_classes = ies[:-1], set()
86 assert ies[-1].__name__ == 'GenericIE', 'Last IE must be GenericIE'
87 while classes:
88 for c in classes[:]:
89 bases = set(c.__bases__) - {object, *ignored_bases}
90 restart = False
5b836d47 91 for b in sorted(bases, key=lambda x: x.__name__):
82d02080 92 if b not in classes and b not in returned_classes:
93 assert b.__name__ != 'GenericIE', 'Cannot inherit from GenericIE'
94 classes.insert(0, b)
95 restart = True
96 if restart:
97 break
98 if bases <= returned_classes:
99 yield c
100 returned_classes.add(c)
101 classes.remove(c)
102 break
103 yield ies[-1]
104
105
106def build_lazy_ie(ie, name, attr_base):
107 bases = ', '.join({
108 'InfoExtractor': 'LazyLoadExtractor',
109 'SearchInfoExtractor': 'LazyLoadSearchExtractor',
110 }.get(base.__name__, base.__name__) for base in ie.__bases__)
111
112 s = IE_TEMPLATE.format(name=name, module=ie.__module__, bases=bases)
82d02080 113 return s + '\n'.join(extra_ie_code(ie, attr_base))
114
115
116if __name__ == '__main__':
117 main()