]> jfr.im git - yt-dlp.git/blame - devscripts/make_lazy_extractors.py
lazy extractors: specify the encoding
[yt-dlp.git] / devscripts / make_lazy_extractors.py
CommitLineData
779822d9
JMF
1from __future__ import unicode_literals, print_function
2
3from inspect import getsource
4import os
5from os.path import dirname as dirn
6import sys
7
8print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
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
16from youtube_dl.extractor import _ALL_CLASSES
17from youtube_dl.extractor.common import InfoExtractor
18
19with open('devscripts/lazy_load_template.py', 'rt') as f:
20 module_template = f.read()
21
22module_contents = [module_template + '\n' + getsource(InfoExtractor.suitable)]
23
24ie_template = '''
25class {name}(LazyLoadExtractor):
26 _VALID_URL = {valid_url!r}
27 _module = '{module}'
28'''
29
30make_valid_template = '''
31 @classmethod
32 def _make_valid_url(cls):
33 return {!r}
34'''
35
36
37def build_lazy_ie(ie, name):
38 valid_url = getattr(ie, '_VALID_URL', None)
39 s = ie_template.format(
40 name=name,
41 valid_url=valid_url,
42 module=ie.__module__)
43 if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
44 s += getsource(ie.suitable)
45 if hasattr(ie, '_make_valid_url'):
46 # search extractors
47 s += make_valid_template.format(ie._make_valid_url())
48 return s
49
50names = []
51for ie in _ALL_CLASSES:
52 name = ie.ie_key() + 'IE'
53 src = build_lazy_ie(ie, name)
54 module_contents.append(src)
55 names.append(name)
56
57module_contents.append(
58 '_ALL_CLASSES = [{}]'.format(', '.join(names)))
59
60module_src = '\n'.join(module_contents)
61
62with open(lazy_extractors_filename, 'wt') as f:
63 f.write(module_src)