]>
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 sys
.path
.insert(0, dirn(dirn((os
.path
.abspath(__file__
)))))
12 lazy_extractors_filename
= sys
.argv
[1] if len(sys
.argv
) > 1 else 'yt_dlp/extractor/lazy_extractors.py'
13 if os
.path
.exists(lazy_extractors_filename
):
14 os
.remove(lazy_extractors_filename
)
16 # Block plugins from loading
17 plugins_dirname
= 'ytdlp_plugins'
18 plugins_blocked_dirname
= 'ytdlp_plugins_blocked'
19 if os
.path
.exists(plugins_dirname
):
20 os
.rename(plugins_dirname
, plugins_blocked_dirname
)
22 from yt_dlp
.extractor
import _ALL_CLASSES
23 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
25 if os
.path
.exists(plugins_blocked_dirname
):
26 os
.rename(plugins_blocked_dirname
, plugins_dirname
)
28 with open('devscripts/lazy_load_template.py', 'rt') as f
:
29 module_template
= f
.read()
31 CLASS_PROPERTIES
= ['ie_key', 'working', '_match_valid_url', 'suitable', '_match_id', 'get_temp_id']
34 *[getsource(getattr(InfoExtractor
, k
)) for k
in CLASS_PROPERTIES
],
35 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
38 class {name}({bases}):
43 def get_base_name(base
):
44 if base
is InfoExtractor
:
45 return 'LazyLoadExtractor'
46 elif base
is SearchInfoExtractor
:
47 return 'LazyLoadSearchExtractor'
52 def build_lazy_ie(ie
, name
):
53 s
= ie_template
.format(
55 bases
=', '.join(map(get_base_name
, ie
.__bases
__)),
57 valid_url
= getattr(ie
, '_VALID_URL', None)
58 if not valid_url
and hasattr(ie
, '_make_valid_url'):
59 valid_url
= ie
._make
_valid
_url
()
61 s
+= f
' _VALID_URL = {valid_url!r}\n'
63 s
+= ' _WORKING = False\n'
64 if ie
.suitable
.__func
__ is not InfoExtractor
.suitable
.__func
__:
65 s
+= f
'\n{getsource(ie.suitable)}'
69 # find the correct sorting and add the required base classes so that subclasses
70 # can be correctly created
71 classes
= _ALL_CLASSES
[:-1]
75 bases
= set(c
.__bases
__) - set((object, InfoExtractor
, SearchInfoExtractor
))
78 if b
not in classes
and b
not in ordered_cls
:
79 if b
.__name
__ == 'GenericIE':
85 if all(b
in ordered_cls
for b
in bases
):
89 ordered_cls
.append(_ALL_CLASSES
[-1])
92 for ie
in ordered_cls
:
94 src
= build_lazy_ie(ie
, name
)
95 module_contents
.append(src
)
96 if ie
in _ALL_CLASSES
:
99 module_contents
.append(
100 '\n_ALL_CLASSES = [{0}]'.format(', '.join(names
)))
102 module_src
= '\n'.join(module_contents
) + '\n'
104 with io
.open(lazy_extractors_filename
, 'wt', encoding
='utf-8') as f
: