]> jfr.im git - yt-dlp.git/blame - devscripts/make_lazy_extractors.py
[cleanup] Point all shebang to `python3` (#372)
[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
19os.rename('ytdlp_plugins', 'ytdlp_plugins_blocked')
20
7a5c1cfe
P
21from yt_dlp.extractor import _ALL_CLASSES
22from yt_dlp.extractor.common import InfoExtractor, SearchInfoExtractor
779822d9 23
0b2e9d2c 24os.rename('ytdlp_plugins_blocked', 'ytdlp_plugins')
25
779822d9
JMF
26with open('devscripts/lazy_load_template.py', 'rt') as f:
27 module_template = f.read()
28
169d836f
JMF
29module_contents = [
30 module_template + '\n' + getsource(InfoExtractor.suitable) + '\n',
31 'class LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
779822d9
JMF
32
33ie_template = '''
169d836f 34class {name}({bases}):
779822d9
JMF
35 _VALID_URL = {valid_url!r}
36 _module = '{module}'
37'''
38
39make_valid_template = '''
40 @classmethod
41 def _make_valid_url(cls):
c1ce6acd 42 return {valid_url!r}
779822d9
JMF
43'''
44
45
169d836f
JMF
46def get_base_name(base):
47 if base is InfoExtractor:
48 return 'LazyLoadExtractor'
49 elif base is SearchInfoExtractor:
50 return 'LazyLoadSearchExtractor'
51 else:
52 return base.__name__
53
54
779822d9
JMF
55def build_lazy_ie(ie, name):
56 valid_url = getattr(ie, '_VALID_URL', None)
57 s = ie_template.format(
58 name=name,
169d836f 59 bases=', '.join(map(get_base_name, ie.__bases__)),
779822d9
JMF
60 valid_url=valid_url,
61 module=ie.__module__)
62 if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
6b97ca96 63 s += '\n' + getsource(ie.suitable)
779822d9
JMF
64 if hasattr(ie, '_make_valid_url'):
65 # search extractors
c1ce6acd 66 s += make_valid_template.format(valid_url=ie._make_valid_url())
779822d9
JMF
67 return s
68
582be358 69
8bdd16b4 70# find the correct sorting and add the required base classes so that subclasses
169d836f
JMF
71# can be correctly created
72classes = _ALL_CLASSES[:-1]
73ordered_cls = []
74while classes:
75 for c in classes[:]:
76 bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))
77 stop = False
78 for b in bases:
79 if b not in classes and b not in ordered_cls:
80 if b.__name__ == 'GenericIE':
81 exit()
82 classes.insert(0, b)
83 stop = True
84 if stop:
85 break
86 if all(b in ordered_cls for b in bases):
87 ordered_cls.append(c)
88 classes.remove(c)
89 break
90ordered_cls.append(_ALL_CLASSES[-1])
91
779822d9 92names = []
169d836f
JMF
93for ie in ordered_cls:
94 name = ie.__name__
779822d9
JMF
95 src = build_lazy_ie(ie, name)
96 module_contents.append(src)
169d836f
JMF
97 if ie in _ALL_CLASSES:
98 names.append(name)
779822d9
JMF
99
100module_contents.append(
c1ce6acd 101 '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
779822d9 102
6b97ca96 103module_src = '\n'.join(module_contents) + '\n'
779822d9 104
19f38218 105with io.open(lazy_extractors_filename, 'wt', encoding='utf-8') as f:
779822d9 106 f.write(module_src)