]>
jfr.im git - yt-dlp.git/blob - devscripts/make_lazy_extractors.py
3 # Allow direct execution
8 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
11 from inspect
import getsource
13 from devscripts
.utils
import get_filename_args
, read_file
, write_file
16 STATIC_CLASS_PROPERTIES
= [
17 'IE_NAME', 'IE_DESC', 'SEARCH_KEY', '_VALID_URL', '_WORKING', '_ENABLED', '_NETRC_MACHINE', 'age_limit'
20 'ie_key', 'working', 'description', 'suitable', '_match_valid_url', '_match_id', 'get_temp_id', 'is_suitable'
23 class {name}({bases}):
26 MODULE_TEMPLATE
= read_file('devscripts/lazy_load_template.py')
30 lazy_extractors_filename
= get_filename_args(default_outfile
='yt_dlp/extractor/lazy_extractors.py')
31 if os
.path
.exists(lazy_extractors_filename
):
32 os
.remove(lazy_extractors_filename
)
34 _ALL_CLASSES
= get_all_ies() # Must be before import
36 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
38 DummyInfoExtractor
= type('InfoExtractor', (InfoExtractor
,), {'IE_NAME': NO_ATTR}
)
39 module_src
= '\n'.join((
42 *extra_ie_code(DummyInfoExtractor
),
43 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
44 *build_ies(_ALL_CLASSES
, (InfoExtractor
, SearchInfoExtractor
), DummyInfoExtractor
),
47 write_file(lazy_extractors_filename
, f
'{module_src}\n')
51 PLUGINS_DIRNAME
= 'ytdlp_plugins'
52 BLOCKED_DIRNAME
= f
'{PLUGINS_DIRNAME}_blocked'
53 if os
.path
.exists(PLUGINS_DIRNAME
):
54 # os.rename cannot be used, e.g. in Docker. See https://github.com/yt-dlp/yt-dlp/pull/4958
55 shutil
.move(PLUGINS_DIRNAME
, BLOCKED_DIRNAME
)
57 from yt_dlp
.extractor
.extractors
import _ALL_CLASSES
59 if os
.path
.exists(BLOCKED_DIRNAME
):
60 shutil
.move(BLOCKED_DIRNAME
, PLUGINS_DIRNAME
)
64 def extra_ie_code(ie
, base
=None):
65 for var
in STATIC_CLASS_PROPERTIES
:
66 val
= getattr(ie
, var
)
67 if val
!= (getattr(base
, var
) if base
else NO_ATTR
):
68 yield f
' {var} = {val!r}'
71 for name
in CLASS_METHODS
:
73 if not base
or f
.__func
__ != getattr(base
, name
).__func
__:
77 def build_ies(ies
, bases
, attr_base
):
79 for ie
in sort_ies(ies
, bases
):
80 yield build_lazy_ie(ie
, ie
.__name
__, attr_base
)
82 names
.append(ie
.__name
__)
84 yield f
'\n_ALL_CLASSES = [{", ".join(names)}]'
87 def sort_ies(ies
, ignored_bases
):
88 """find the correct sorting and add the required base classes so that subclasses can be correctly created"""
89 classes
, returned_classes
= ies
[:-1], set()
90 assert ies
[-1].__name
__ == 'GenericIE', 'Last IE must be GenericIE'
93 bases
= set(c
.__bases
__) - {object, *ignored_bases}
95 for b
in sorted(bases
, key
=lambda x
: x
.__name
__):
96 if b
not in classes
and b
not in returned_classes
:
97 assert b
.__name
__ != 'GenericIE', 'Cannot inherit from GenericIE'
102 if bases
<= returned_classes
:
104 returned_classes
.add(c
)
110 def build_lazy_ie(ie
, name
, attr_base
):
112 'InfoExtractor': 'LazyLoadExtractor',
113 'SearchInfoExtractor': 'LazyLoadSearchExtractor',
114 }.get(base
.__name
__, base
.__name
__) for base
in ie
.__bases
__)
116 s
= IE_TEMPLATE
.format(name
=name
, module
=ie
.__module
__, bases
=bases
)
117 return s
+ '\n'.join(extra_ie_code(ie
, attr_base
))
120 if __name__
== '__main__':