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