]>
jfr.im git - yt-dlp.git/blob - devscripts/make_lazy_extractors.py
2 from __future__
import unicode_literals
, print_function
4 from inspect
import getsource
7 from os
.path
import dirname
as dirn
10 print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys
.stderr
)
12 sys
.path
.insert(0, dirn(dirn((os
.path
.abspath(__file__
)))))
14 lazy_extractors_filename
= sys
.argv
[1]
15 if os
.path
.exists(lazy_extractors_filename
):
16 os
.remove(lazy_extractors_filename
)
18 # Block plugins from loading
19 plugins_dirname
= 'ytdlp_plugins'
20 plugins_blocked_dirname
= 'ytdlp_plugins_blocked'
21 if os
.path
.exists(plugins_dirname
):
22 os
.rename(plugins_dirname
, plugins_blocked_dirname
)
24 from yt_dlp
.extractor
import _ALL_CLASSES
25 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
27 if os
.path
.exists(plugins_blocked_dirname
):
28 os
.rename(plugins_blocked_dirname
, plugins_dirname
)
30 with open('devscripts/lazy_load_template.py', 'rt') as f
:
31 module_template
= f
.read()
35 getsource(InfoExtractor
.ie_key
),
36 getsource(InfoExtractor
._match
_valid
_url
),
37 getsource(InfoExtractor
.suitable
),
38 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
41 class {name}({bases}):
42 _VALID_URL = {valid_url!r}
46 make_valid_template
= '''
48 def _make_valid_url(cls):
53 def get_base_name(base
):
54 if base
is InfoExtractor
:
55 return 'LazyLoadExtractor'
56 elif base
is SearchInfoExtractor
:
57 return 'LazyLoadSearchExtractor'
62 def build_lazy_ie(ie
, name
):
63 valid_url
= getattr(ie
, '_VALID_URL', None)
64 s
= ie_template
.format(
66 bases
=', '.join(map(get_base_name
, ie
.__bases
__)),
69 if ie
.suitable
.__func
__ is not InfoExtractor
.suitable
.__func
__:
70 s
+= '\n' + getsource(ie
.suitable
)
71 if hasattr(ie
, '_make_valid_url'):
73 s
+= make_valid_template
.format(valid_url
=ie
._make
_valid
_url
())
77 # find the correct sorting and add the required base classes so that subclasses
78 # can be correctly created
79 classes
= _ALL_CLASSES
[:-1]
83 bases
= set(c
.__bases
__) - set((object, InfoExtractor
, SearchInfoExtractor
))
86 if b
not in classes
and b
not in ordered_cls
:
87 if b
.__name
__ == 'GenericIE':
93 if all(b
in ordered_cls
for b
in bases
):
97 ordered_cls
.append(_ALL_CLASSES
[-1])
100 for ie
in ordered_cls
:
102 src
= build_lazy_ie(ie
, name
)
103 module_contents
.append(src
)
104 if ie
in _ALL_CLASSES
:
107 module_contents
.append(
108 '\n_ALL_CLASSES = [{0}]'.format(', '.join(names
)))
110 module_src
= '\n'.join(module_contents
) + '\n'
112 with io
.open(lazy_extractors_filename
, 'wt', encoding
='utf-8') as f
: