[ie/olympics] Fix extractor (#10604)
[yt-dlp3.git] / devscripts / make_lazy_extractors.py
blobd74ea202f08b44b709a73c372d9c42d2004d01e9
1 #!/usr/bin/env python3
3 # Allow direct execution
4 import os
5 import shutil
6 import sys
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 from inspect import getsource
13 from devscripts.utils import get_filename_args, read_file, write_file
15 NO_ATTR = object()
16 STATIC_CLASS_PROPERTIES = [
17 'IE_NAME', '_ENABLED', '_VALID_URL', # Used for URL matching
18 '_WORKING', 'IE_DESC', '_NETRC_MACHINE', 'SEARCH_KEY', # Used for --extractor-descriptions
19 'age_limit', # Used for --age-limit (evaluated)
20 '_RETURN_TYPE', # Accessed in CLI only with instance (evaluated)
22 CLASS_METHODS = [
23 'ie_key', 'suitable', '_match_valid_url', # Used for URL matching
24 'working', 'get_temp_id', '_match_id', # Accessed just before instance creation
25 'description', # Used for --extractor-descriptions
26 'is_suitable', # Used for --age-limit
27 'supports_login', 'is_single_video', # Accessed in CLI only with instance
29 IE_TEMPLATE = '''
30 class {name}({bases}):
31 _module = {module!r}
32 '''
33 MODULE_TEMPLATE = read_file('devscripts/lazy_load_template.py')
36 def main():
37 lazy_extractors_filename = get_filename_args(default_outfile='yt_dlp/extractor/lazy_extractors.py')
38 if os.path.exists(lazy_extractors_filename):
39 os.remove(lazy_extractors_filename)
41 _ALL_CLASSES = get_all_ies() # Must be before import
43 import yt_dlp.plugins
44 from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor
46 # Filter out plugins
47 _ALL_CLASSES = [cls for cls in _ALL_CLASSES if not cls.__module__.startswith(f'{yt_dlp.plugins.PACKAGE_NAME}.')]
49 DummyInfoExtractor = type('InfoExtractor', (InfoExtractor,), {'IE_NAME': NO_ATTR})
50 module_src = '\n'.join((
51 MODULE_TEMPLATE,
52 ' _module = None',
53 *extra_ie_code(DummyInfoExtractor),
54 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
55 *build_ies(_ALL_CLASSES, (InfoExtractor, SearchInfoExtractor), DummyInfoExtractor),
58 write_file(lazy_extractors_filename, f'{module_src}\n')
61 def get_all_ies():
62 PLUGINS_DIRNAME = 'ytdlp_plugins'
63 BLOCKED_DIRNAME = f'{PLUGINS_DIRNAME}_blocked'
64 if os.path.exists(PLUGINS_DIRNAME):
65 # os.rename cannot be used, e.g. in Docker. See https://github.com/yt-dlp/yt-dlp/pull/4958
66 shutil.move(PLUGINS_DIRNAME, BLOCKED_DIRNAME)
67 try:
68 from yt_dlp.extractor.extractors import _ALL_CLASSES
69 finally:
70 if os.path.exists(BLOCKED_DIRNAME):
71 shutil.move(BLOCKED_DIRNAME, PLUGINS_DIRNAME)
72 return _ALL_CLASSES
75 def extra_ie_code(ie, base=None):
76 for var in STATIC_CLASS_PROPERTIES:
77 val = getattr(ie, var)
78 if val != (getattr(base, var) if base else NO_ATTR):
79 yield f' {var} = {val!r}'
80 yield ''
82 for name in CLASS_METHODS:
83 f = getattr(ie, name)
84 if not base or f.__func__ != getattr(base, name).__func__:
85 yield getsource(f)
88 def build_ies(ies, bases, attr_base):
89 names = []
90 for ie in sort_ies(ies, bases):
91 yield build_lazy_ie(ie, ie.__name__, attr_base)
92 if ie in ies:
93 names.append(ie.__name__)
95 yield f'\n_ALL_CLASSES = [{", ".join(names)}]'
98 def sort_ies(ies, ignored_bases):
99 """find the correct sorting and add the required base classes so that subclasses can be correctly created"""
100 classes, returned_classes = ies[:-1], set()
101 assert ies[-1].__name__ == 'GenericIE', 'Last IE must be GenericIE'
102 while classes:
103 for c in classes[:]:
104 bases = set(c.__bases__) - {object, *ignored_bases}
105 restart = False
106 for b in sorted(bases, key=lambda x: x.__name__):
107 if b not in classes and b not in returned_classes:
108 assert b.__name__ != 'GenericIE', 'Cannot inherit from GenericIE'
109 classes.insert(0, b)
110 restart = True
111 if restart:
112 break
113 if bases <= returned_classes:
114 yield c
115 returned_classes.add(c)
116 classes.remove(c)
117 break
118 yield ies[-1]
121 def build_lazy_ie(ie, name, attr_base):
122 bases = ', '.join({
123 'InfoExtractor': 'LazyLoadExtractor',
124 'SearchInfoExtractor': 'LazyLoadSearchExtractor',
125 }.get(base.__name__, base.__name__) for base in ie.__bases__)
127 s = IE_TEMPLATE.format(name=name, module=ie.__module__, bases=bases)
128 return s + '\n'.join(extra_ie_code(ie, attr_base))
131 if __name__ == '__main__':
132 main()