]>
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]
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}):
42 make_valid_template
= '''
44 def _make_valid_url(cls):
49 def get_base_name(base
):
50 if base
is InfoExtractor
:
51 return 'LazyLoadExtractor'
52 elif base
is SearchInfoExtractor
:
53 return 'LazyLoadSearchExtractor'
58 def build_lazy_ie(ie
, name
):
59 s
= ie_template
.format(
61 bases
=', '.join(map(get_base_name
, ie
.__bases
__)),
63 valid_url
= getattr(ie
, '_VALID_URL', None)
65 s
+= f
' _VALID_URL = {valid_url!r}\n'
67 s
+= ' _WORKING = False\n'
68 if ie
.suitable
.__func
__ is not InfoExtractor
.suitable
.__func
__:
69 s
+= f
'\n{getsource(ie.suitable)}'
70 if hasattr(ie
, '_make_valid_url'):
72 s
+= make_valid_template
.format(valid_url
=ie
._make
_valid
_url
())
76 # find the correct sorting and add the required base classes so that subclasses
77 # can be correctly created
78 classes
= _ALL_CLASSES
[:-1]
82 bases
= set(c
.__bases
__) - set((object, InfoExtractor
, SearchInfoExtractor
))
85 if b
not in classes
and b
not in ordered_cls
:
86 if b
.__name
__ == 'GenericIE':
92 if all(b
in ordered_cls
for b
in bases
):
96 ordered_cls
.append(_ALL_CLASSES
[-1])
99 for ie
in ordered_cls
:
101 src
= build_lazy_ie(ie
, name
)
102 module_contents
.append(src
)
103 if ie
in _ALL_CLASSES
:
106 module_contents
.append(
107 '\n_ALL_CLASSES = [{0}]'.format(', '.join(names
)))
109 module_src
= '\n'.join(module_contents
) + '\n'
111 with io
.open(lazy_extractors_filename
, 'wt', encoding
='utf-8') as f
: