]>
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', '_ENABLED', '_VALID_URL', # Used for URL matching
18 '_WORKING', 'IE_DESC', '_NETRC_MACHINE', 'SEARCH_KEY', # Used for --extractor-descriptions
19 'age_limit', # Used for --age-limit (evaluated)
20 '_RETURN_TYPE', # Accessed in CLI only with instance (evaluated)
23 'ie_key', 'suitable', '_match_valid_url', # Used for URL matching
24 'working', 'get_temp_id', '_match_id', # Accessed just before instance creation
25 'description', # Used for --extractor-descriptions
26 'is_suitable', # Used for --age-limit
27 'supports_login', 'is_single_video', # Accessed in CLI only with instance
30 class {name}({bases}):
33 MODULE_TEMPLATE
= read_file('devscripts/lazy_load_template.py')
37 lazy_extractors_filename
= get_filename_args(default_outfile
='yt_dlp/extractor/lazy_extractors.py')
38 if os
.path
.exists(lazy_extractors_filename
):
39 os
.remove(lazy_extractors_filename
)
41 _ALL_CLASSES
= get_all_ies() # Must be before import
43 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
45 DummyInfoExtractor
= type('InfoExtractor', (InfoExtractor
,), {'IE_NAME': NO_ATTR}
)
46 module_src
= '\n'.join((
49 *extra_ie_code(DummyInfoExtractor
),
50 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
51 *build_ies(_ALL_CLASSES
, (InfoExtractor
, SearchInfoExtractor
), DummyInfoExtractor
),
54 write_file(lazy_extractors_filename
, f
'{module_src}\n')
58 PLUGINS_DIRNAME
= 'ytdlp_plugins'
59 BLOCKED_DIRNAME
= f
'{PLUGINS_DIRNAME}_blocked'
60 if os
.path
.exists(PLUGINS_DIRNAME
):
61 # os.rename cannot be used, e.g. in Docker. See https://github.com/yt-dlp/yt-dlp/pull/4958
62 shutil
.move(PLUGINS_DIRNAME
, BLOCKED_DIRNAME
)
64 from yt_dlp
.extractor
.extractors
import _ALL_CLASSES
66 if os
.path
.exists(BLOCKED_DIRNAME
):
67 shutil
.move(BLOCKED_DIRNAME
, PLUGINS_DIRNAME
)
71 def extra_ie_code(ie
, base
=None):
72 for var
in STATIC_CLASS_PROPERTIES
:
73 val
= getattr(ie
, var
)
74 if val
!= (getattr(base
, var
) if base
else NO_ATTR
):
75 yield f
' {var} = {val!r}'
78 for name
in CLASS_METHODS
:
80 if not base
or f
.__func
__ != getattr(base
, name
).__func
__:
84 def build_ies(ies
, bases
, attr_base
):
86 for ie
in sort_ies(ies
, bases
):
87 yield build_lazy_ie(ie
, ie
.__name
__, attr_base
)
89 names
.append(ie
.__name
__)
91 yield f
'\n_ALL_CLASSES = [{", ".join(names)}]'
94 def sort_ies(ies
, ignored_bases
):
95 """find the correct sorting and add the required base classes so that subclasses can be correctly created"""
96 classes
, returned_classes
= ies
[:-1], set()
97 assert ies
[-1].__name
__ == 'GenericIE', 'Last IE must be GenericIE'
100 bases
= set(c
.__bases
__) - {object, *ignored_bases}
102 for b
in sorted(bases
, key
=lambda x
: x
.__name
__):
103 if b
not in classes
and b
not in returned_classes
:
104 assert b
.__name
__ != 'GenericIE', 'Cannot inherit from GenericIE'
109 if bases
<= returned_classes
:
111 returned_classes
.add(c
)
117 def build_lazy_ie(ie
, name
, attr_base
):
119 'InfoExtractor': 'LazyLoadExtractor',
120 'SearchInfoExtractor': 'LazyLoadSearchExtractor',
121 }.get(base
.__name
__, base
.__name
__) for base
in ie
.__bases
__)
123 s
= IE_TEMPLATE
.format(name
=name
, module
=ie
.__module
__, bases
=bases
)
124 return s
+ '\n'.join(extra_ie_code(ie
, attr_base
))
127 if __name__
== '__main__':