]> jfr.im git - yt-dlp.git/blame_incremental - devscripts/make_lazy_extractors.py
[devscripts/run_tests] Use markers to filter tests (#1258)
[yt-dlp.git] / devscripts / make_lazy_extractors.py
... / ...
CommitLineData
1#!/usr/bin/env python3
2from __future__ import unicode_literals, print_function
3
4from inspect import getsource
5import io
6import os
7from os.path import dirname as dirn
8import sys
9
10sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
11
12lazy_extractors_filename = sys.argv[1]
13if os.path.exists(lazy_extractors_filename):
14 os.remove(lazy_extractors_filename)
15
16# Block plugins from loading
17plugins_dirname = 'ytdlp_plugins'
18plugins_blocked_dirname = 'ytdlp_plugins_blocked'
19if os.path.exists(plugins_dirname):
20 os.rename(plugins_dirname, plugins_blocked_dirname)
21
22from yt_dlp.extractor import _ALL_CLASSES
23from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor
24
25if os.path.exists(plugins_blocked_dirname):
26 os.rename(plugins_blocked_dirname, plugins_dirname)
27
28with open('devscripts/lazy_load_template.py', 'rt') as f:
29 module_template = f.read()
30
31CLASS_PROPERTIES = ['ie_key', 'working', '_match_valid_url', 'suitable', '_match_id', 'get_temp_id']
32module_contents = [
33 module_template,
34 *[getsource(getattr(InfoExtractor, k)) for k in CLASS_PROPERTIES],
35 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
36
37ie_template = '''
38class {name}({bases}):
39 _module = '{module}'
40'''
41
42make_valid_template = '''
43 @classmethod
44 def _make_valid_url(cls):
45 return {valid_url!r}
46'''
47
48
49def get_base_name(base):
50 if base is InfoExtractor:
51 return 'LazyLoadExtractor'
52 elif base is SearchInfoExtractor:
53 return 'LazyLoadSearchExtractor'
54 else:
55 return base.__name__
56
57
58def build_lazy_ie(ie, name):
59 s = ie_template.format(
60 name=name,
61 bases=', '.join(map(get_base_name, ie.__bases__)),
62 module=ie.__module__)
63 valid_url = getattr(ie, '_VALID_URL', None)
64 if valid_url:
65 s += f' _VALID_URL = {valid_url!r}\n'
66 if not ie._WORKING:
67 s += ' _WORKING = False\n'
68 if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
69 s += f'\n{getsource(ie.suitable)}'
70 if hasattr(ie, '_make_valid_url'):
71 # search extractors
72 s += make_valid_template.format(valid_url=ie._make_valid_url())
73 return s
74
75
76# find the correct sorting and add the required base classes so that subclasses
77# can be correctly created
78classes = _ALL_CLASSES[:-1]
79ordered_cls = []
80while 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
96ordered_cls.append(_ALL_CLASSES[-1])
97
98names = []
99for ie in ordered_cls:
100 name = ie.__name__
101 src = build_lazy_ie(ie, name)
102 module_contents.append(src)
103 if ie in _ALL_CLASSES:
104 names.append(name)
105
106module_contents.append(
107 '\n_ALL_CLASSES = [{0}]'.format(', '.join(names)))
108
109module_src = '\n'.join(module_contents) + '\n'
110
111with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
112 f.write(module_src)