2 from inspect
import getsource
4 from os
.path
import dirname
as dirn
7 sys
.path
.insert(0, dirn(dirn(os
.path
.abspath(__file__
))))
9 lazy_extractors_filename
= sys
.argv
[1] if len(sys
.argv
) > 1 else 'yt_dlp/extractor/lazy_extractors.py'
10 if os
.path
.exists(lazy_extractors_filename
):
11 os
.remove(lazy_extractors_filename
)
13 # Block plugins from loading
14 plugins_dirname
= 'ytdlp_plugins'
15 plugins_blocked_dirname
= 'ytdlp_plugins_blocked'
16 if os
.path
.exists(plugins_dirname
):
17 os
.rename(plugins_dirname
, plugins_blocked_dirname
)
19 from yt_dlp
.extractor
import _ALL_CLASSES
20 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
22 if os
.path
.exists(plugins_blocked_dirname
):
23 os
.rename(plugins_blocked_dirname
, plugins_dirname
)
25 with open('devscripts/lazy_load_template.py') as f
:
26 module_template
= f
.read()
28 CLASS_PROPERTIES
= ['ie_key', 'working', '_match_valid_url', 'suitable', '_match_id', 'get_temp_id']
31 *[getsource(getattr(InfoExtractor
, k
)) for k
in CLASS_PROPERTIES
],
32 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n']
35 class {name}({bases}):
40 def get_base_name(base
):
41 if base
is InfoExtractor
:
42 return 'LazyLoadExtractor'
43 elif base
is SearchInfoExtractor
:
44 return 'LazyLoadSearchExtractor'
49 def build_lazy_ie(ie
, name
):
50 s
= ie_template
.format(
52 bases
=', '.join(map(get_base_name
, ie
.__bases
__)),
54 valid_url
= getattr(ie
, '_VALID_URL', None)
55 if not valid_url
and hasattr(ie
, '_make_valid_url'):
56 valid_url
= ie
._make
_valid
_url
()
58 s
+= f
' _VALID_URL = {valid_url!r}\n'
60 s
+= ' _WORKING = False\n'
61 if ie
.suitable
.__func
__ is not InfoExtractor
.suitable
.__func
__:
62 s
+= f
'\n{getsource(ie.suitable)}'
66 # find the correct sorting and add the required base classes so that subclasses
67 # can be correctly created
68 classes
= _ALL_CLASSES
[:-1]
72 bases
= set(c
.__bases
__) - {object, InfoExtractor, SearchInfoExtractor}
75 if b
not in classes
and b
not in ordered_cls
:
76 if b
.__name
__ == 'GenericIE':
82 if all(b
in ordered_cls
for b
in bases
):
86 ordered_cls
.append(_ALL_CLASSES
[-1])
89 for ie
in ordered_cls
:
91 src
= build_lazy_ie(ie
, name
)
92 module_contents
.append(src
)
93 if ie
in _ALL_CLASSES
:
96 module_contents
.append(
97 '\n_ALL_CLASSES = [{}]'.format(', '.join(names
)))
99 module_src
= '\n'.join(module_contents
) + '\n'
101 with open(lazy_extractors_filename
, 'wt', encoding
='utf-8') as f
: