4 from inspect
import getsource
6 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
8 lazy_extractors_filename
= sys
.argv
[1] if len(sys
.argv
) > 1 else 'yt_dlp/extractor/lazy_extractors.py'
9 if os
.path
.exists(lazy_extractors_filename
):
10 os
.remove(lazy_extractors_filename
)
12 # Block plugins from loading
13 plugins_dirname
= 'ytdlp_plugins'
14 plugins_blocked_dirname
= 'ytdlp_plugins_blocked'
15 if os
.path
.exists(plugins_dirname
):
16 os
.rename(plugins_dirname
, plugins_blocked_dirname
)
18 from yt_dlp
.extractor
import _ALL_CLASSES
19 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
21 if os
.path
.exists(plugins_blocked_dirname
):
22 os
.rename(plugins_blocked_dirname
, plugins_dirname
)
24 with open('devscripts/lazy_load_template.py', encoding
='utf-8') as f
:
25 module_template
= f
.read()
27 CLASS_PROPERTIES
= ['ie_key', 'working', '_match_valid_url', 'suitable', '_match_id', 'get_temp_id']
30 *[getsource(getattr(InfoExtractor
, k
)) for k
in CLASS_PROPERTIES
],
31 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
34 class {name}({bases}):
39 def get_base_name(base
):
40 if base
is InfoExtractor
:
41 return 'LazyLoadExtractor'
42 elif base
is SearchInfoExtractor
:
43 return 'LazyLoadSearchExtractor'
48 def build_lazy_ie(ie
, name
):
49 s
= ie_template
.format(
51 bases
=', '.join(map(get_base_name
, ie
.__bases
__)),
53 valid_url
= getattr(ie
, '_VALID_URL', None)
54 if not valid_url
and hasattr(ie
, '_make_valid_url'):
55 valid_url
= ie
._make
_valid
_url
()
57 s
+= f
' _VALID_URL = {valid_url!r}\n'
59 s
+= ' _WORKING = False\n'
60 if ie
.suitable
.__func
__ is not InfoExtractor
.suitable
.__func
__:
61 s
+= f
'\n{getsource(ie.suitable)}'
65 # find the correct sorting and add the required base classes so that subclasses
66 # can be correctly created
67 classes
= _ALL_CLASSES
[:-1]
71 bases
= set(c
.__bases
__) - {object, InfoExtractor, SearchInfoExtractor}
74 if b
not in classes
and b
not in ordered_cls
:
75 if b
.__name
__ == 'GenericIE':
81 if all(b
in ordered_cls
for b
in bases
):
85 ordered_cls
.append(_ALL_CLASSES
[-1])
88 for ie
in ordered_cls
:
90 src
= build_lazy_ie(ie
, name
)
91 module_contents
.append(src
)
92 if ie
in _ALL_CLASSES
:
95 module_contents
.append(
96 '\n_ALL_CLASSES = [{}]'.format(', '.join(names
)))
98 module_src
= '\n'.join(module_contents
) + '\n'
100 with open(lazy_extractors_filename
, 'wt', encoding
='utf-8') as f
: