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