]>
jfr.im git - yt-dlp.git/blob - devscripts/make_lazy_extractors.py
785d66a6a162f1c53956f1fd54a1e32fc476005b
3 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
11 from inspect
import getsource
14 STATIC_CLASS_PROPERTIES
= ['IE_NAME', 'IE_DESC', 'SEARCH_KEY', '_WORKING', '_NETRC_MACHINE', 'age_limit']
16 'ie_key', 'working', 'description', 'suitable', '_match_valid_url', '_match_id', 'get_temp_id', 'is_suitable'
19 class {name}({bases}):
22 with open('devscripts/lazy_load_template.py', encoding
='utf-8') as f
:
23 MODULE_TEMPLATE
= f
.read()
27 parser
= optparse
.OptionParser(usage
='%prog [OUTFILE.py]')
28 args
= parser
.parse_args()[1] or ['yt_dlp/extractor/lazy_extractors.py']
30 parser
.error('Expected only an output filename')
32 lazy_extractors_filename
= args
[0]
33 if os
.path
.exists(lazy_extractors_filename
):
34 os
.remove(lazy_extractors_filename
)
36 _ALL_CLASSES
= get_all_ies() # Must be before import
38 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
40 DummyInfoExtractor
= type('InfoExtractor', (InfoExtractor
,), {'IE_NAME': NO_ATTR}
)
41 module_src
= '\n'.join((
44 *extra_ie_code(DummyInfoExtractor
),
45 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
46 *build_ies(_ALL_CLASSES
, (InfoExtractor
, SearchInfoExtractor
), DummyInfoExtractor
),
49 with open(lazy_extractors_filename
, 'wt', encoding
='utf-8') as f
:
50 f
.write(f
'{module_src}\n')
54 PLUGINS_DIRNAME
= 'ytdlp_plugins'
55 BLOCKED_DIRNAME
= f
'{PLUGINS_DIRNAME}_blocked'
56 if os
.path
.exists(PLUGINS_DIRNAME
):
57 os
.rename(PLUGINS_DIRNAME
, BLOCKED_DIRNAME
)
59 from yt_dlp
.extractor
.extractors
import _ALL_CLASSES
61 if os
.path
.exists(BLOCKED_DIRNAME
):
62 os
.rename(BLOCKED_DIRNAME
, PLUGINS_DIRNAME
)
66 def extra_ie_code(ie
, base
=None):
67 for var
in STATIC_CLASS_PROPERTIES
:
68 val
= getattr(ie
, var
)
69 if val
!= (getattr(base
, var
) if base
else NO_ATTR
):
70 yield f
' {var} = {val!r}'
73 for name
in CLASS_METHODS
:
75 if not base
or f
.__func
__ != getattr(base
, name
).__func
__:
79 def build_ies(ies
, bases
, attr_base
):
81 for ie
in sort_ies(ies
, bases
):
82 yield build_lazy_ie(ie
, ie
.__name
__, attr_base
)
84 names
.append(ie
.__name
__)
86 yield f
'\n_ALL_CLASSES = [{", ".join(names)}]'
89 def sort_ies(ies
, ignored_bases
):
90 """find the correct sorting and add the required base classes so that subclasses can be correctly created"""
91 classes
, returned_classes
= ies
[:-1], set()
92 assert ies
[-1].__name
__ == 'GenericIE', 'Last IE must be GenericIE'
95 bases
= set(c
.__bases
__) - {object, *ignored_bases}
98 if b
not in classes
and b
not in returned_classes
:
99 assert b
.__name
__ != 'GenericIE', 'Cannot inherit from GenericIE'
104 if bases
<= returned_classes
:
106 returned_classes
.add(c
)
112 def build_lazy_ie(ie
, name
, attr_base
):
114 'InfoExtractor': 'LazyLoadExtractor',
115 'SearchInfoExtractor': 'LazyLoadSearchExtractor',
116 }.get(base
.__name
__, base
.__name
__) for base
in ie
.__bases
__)
118 s
= IE_TEMPLATE
.format(name
=name
, module
=ie
.__module
__, bases
=bases
)
119 valid_url
= getattr(ie
, '_VALID_URL', None)
120 if not valid_url
and hasattr(ie
, '_make_valid_url'):
121 valid_url
= ie
._make
_valid
_url
()
123 s
+= f
' _VALID_URL = {valid_url!r}\n'
124 return s
+ '\n'.join(extra_ie_code(ie
, attr_base
))
127 if __name__
== '__main__':