lazy extractors: Style fixes
[youtube-dl] / devscripts / make_lazy_extractors.py
1 from __future__ import unicode_literals, print_function
2
3 from inspect import getsource
4 import os
5 from os.path import dirname as dirn
6 import sys
7
8 print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)
9
10 sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
11
12 lazy_extractors_filename = sys.argv[1]
13 if os.path.exists(lazy_extractors_filename):
14     os.remove(lazy_extractors_filename)
15
16 from youtube_dl.extractor import _ALL_CLASSES
17 from youtube_dl.extractor.common import InfoExtractor
18
19 with open('devscripts/lazy_load_template.py', 'rt') as f:
20     module_template = f.read()
21
22 module_contents = [module_template + '\n' + getsource(InfoExtractor.suitable)]
23
24 ie_template = '''
25 class {name}(LazyLoadExtractor):
26     _VALID_URL = {valid_url!r}
27     _module = '{module}'
28 '''
29
30 make_valid_template = '''
31     @classmethod
32     def _make_valid_url(cls):
33         return {valid_url!r}
34 '''
35
36
37 def 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 += '\n' + getsource(ie.suitable)
45     if hasattr(ie, '_make_valid_url'):
46         # search extractors
47         s += make_valid_template.format(valid_url=ie._make_valid_url())
48     return s
49
50 names = []
51 for ie in list(sorted(_ALL_CLASSES[:-1], key=lambda cls: cls.ie_key())) + _ALL_CLASSES[-1:]:
52     name = ie.ie_key() + 'IE'
53     src = build_lazy_ie(ie, name)
54     module_contents.append(src)
55     names.append(name)
56
57 module_contents.append(
58     '_ALL_CLASSES = [{0}]'.format(', '.join(names)))
59
60 module_src = '\n'.join(module_contents) + '\n'
61
62 with open(lazy_extractors_filename, 'wt') as f:
63     f.write(module_src)