3 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
10 from inspect
import getsource
12 from devscripts
.utils
import get_filename_args
, read_file
, write_file
15 STATIC_CLASS_PROPERTIES
= [
16 'IE_NAME', '_ENABLED', '_VALID_URL', # Used for URL matching
17 '_WORKING', 'IE_DESC', '_NETRC_MACHINE', 'SEARCH_KEY', # Used for --extractor-descriptions
18 'age_limit', # Used for --age-limit (evaluated)
19 '_RETURN_TYPE', # Accessed in CLI only with instance (evaluated)
22 'ie_key', 'suitable', '_match_valid_url', # Used for URL matching
23 'working', 'get_temp_id', '_match_id', # Accessed just before instance creation
24 'description', # Used for --extractor-descriptions
25 'is_suitable', # Used for --age-limit
26 'supports_login', 'is_single_video', # Accessed in CLI only with instance
29 class {name}({bases}):
32 MODULE_TEMPLATE
= read_file('devscripts/lazy_load_template.py')
36 os
.environ
['YTDLP_NO_PLUGINS'] = 'true'
37 os
.environ
['YTDLP_NO_LAZY_EXTRACTORS'] = 'true'
39 lazy_extractors_filename
= get_filename_args(default_outfile
='yt_dlp/extractor/lazy_extractors.py')
41 from yt_dlp
.extractor
.extractors
import _ALL_CLASSES
42 from yt_dlp
.extractor
.common
import InfoExtractor
, SearchInfoExtractor
44 DummyInfoExtractor
= type('InfoExtractor', (InfoExtractor
,), {'IE_NAME': NO_ATTR
})
45 module_src
= '\n'.join((
48 *extra_ie_code(DummyInfoExtractor
),
49 '\nclass LazyLoadSearchExtractor(LazyLoadExtractor):\n pass\n',
50 *build_ies(_ALL_CLASSES
, (InfoExtractor
, SearchInfoExtractor
), DummyInfoExtractor
),
53 write_file(lazy_extractors_filename
, f
'{module_src}\n')
56 def extra_ie_code(ie
, base
=None):
57 for var
in STATIC_CLASS_PROPERTIES
:
58 val
= getattr(ie
, var
)
59 if val
!= (getattr(base
, var
) if base
else NO_ATTR
):
60 yield f
' {var} = {val!r}'
63 for name
in CLASS_METHODS
:
65 if not base
or f
.__func
__ != getattr(base
, name
).__func
__:
69 def build_ies(ies
, bases
, attr_base
):
71 for ie
in sort_ies(ies
, bases
):
72 yield build_lazy_ie(ie
, ie
.__name
__, attr_base
)
74 names
.append(ie
.__name
__)
76 yield f
'\n_ALL_CLASSES = [{", ".join(names)}]'
79 def sort_ies(ies
, ignored_bases
):
80 """find the correct sorting and add the required base classes so that subclasses can be correctly created"""
81 classes
, returned_classes
= ies
[:-1], set()
82 assert ies
[-1].__name
__ == 'GenericIE', 'Last IE must be GenericIE'
85 bases
= set(c
.__bases
__) - {object, *ignored_bases
}
87 for b
in sorted(bases
, key
=lambda x
: x
.__name
__):
88 if b
not in classes
and b
not in returned_classes
:
89 assert b
.__name
__ != 'GenericIE', 'Cannot inherit from GenericIE'
94 if bases
<= returned_classes
:
96 returned_classes
.add(c
)
102 def build_lazy_ie(ie
, name
, attr_base
):
104 'InfoExtractor': 'LazyLoadExtractor',
105 'SearchInfoExtractor': 'LazyLoadSearchExtractor',
106 }.get(base
.__name
__, base
.__name
__) for base
in ie
.__bases
__)
108 s
= IE_TEMPLATE
.format(name
=name
, module
=ie
.__module
__, bases
=bases
)
109 return s
+ '\n'.join(extra_ie_code(ie
, attr_base
))
112 if __name__
== '__main__':