from __future__ import unicode_literals, print_function

from inspect import getsource
import os
from os.path import dirname as dirn
import sys

print('WARNING: Lazy loading extractors is an experimental feature that may not always work', file=sys.stderr)

sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))

lazy_extractors_filename = sys.argv[1]
if os.path.exists(lazy_extractors_filename):
    os.remove(lazy_extractors_filename)

from youtube_dl.extractor import _ALL_CLASSES
from youtube_dl.extractor.common import InfoExtractor

with open('devscripts/lazy_load_template.py', 'rt') as f:
    module_template = f.read()

module_contents = [module_template + '\n' + getsource(InfoExtractor.suitable)]

ie_template = '''
class {name}(LazyLoadExtractor):
    _VALID_URL = {valid_url!r}
    _module = '{module}'
'''

make_valid_template = '''
    @classmethod
    def _make_valid_url(cls):
        return {valid_url!r}
'''


def build_lazy_ie(ie, name):
    valid_url = getattr(ie, '_VALID_URL', None)
    s = ie_template.format(
        name=name,
        valid_url=valid_url,
        module=ie.__module__)
    if ie.suitable.__func__ is not InfoExtractor.suitable.__func__:
        s += '\n' + getsource(ie.suitable)
    if hasattr(ie, '_make_valid_url'):
        # search extractors
        s += make_valid_template.format(valid_url=ie._make_valid_url())
    return s

names = []
for ie in list(sorted(_ALL_CLASSES[:-1], key=lambda cls: cls.ie_key())) + _ALL_CLASSES[-1:]:
    name = ie.ie_key() + 'IE'
    src = build_lazy_ie(ie, name)
    module_contents.append(src)
    names.append(name)

module_contents.append(
    '_ALL_CLASSES = [{0}]'.format(', '.join(names)))

module_src = '\n'.join(module_contents) + '\n'

with open(lazy_extractors_filename, 'wt') as f:
    f.write(module_src)

@@ -535,16 +535,37 @@
 xtractor
+, SearchInfoExtractor
 %0A%0Awith o
@@ -667,16 +667,21 @@
 ents = %5B
+%0A    
 module_t
@@ -730,16 +730,92 @@
 uitable)
+ + '%5Cn',%0A    'class LazyLoadSearchExtractor(LazyLoadExtractor):%5Cn    pass%5Cn'
 %5D%0A%0Aie_te
@@ -840,33 +840,23 @@
  %7Bname%7D(
-LazyLoadExtractor
+%7Bbases%7D
 ):%0A    _
@@ -1019,16 +1019,226 @@
 %7D%0A'''%0A%0A%0A
+def get_base_name(base):%0A    if base is InfoExtractor:%0A        return 'LazyLoadExtractor'%0A    elif base is SearchInfoExtractor:%0A        return 'LazyLoadSearchExtractor'%0A    else:%0A        return base.__name__%0A%0A%0A
 def buil
@@ -1353,16 +1353,75 @@
 e=name,%0A
+        bases=', '.join(map(get_base_name, ie.__bases__)),%0A
         
@@ -1739,110 +1739,762 @@
  s%0A%0A
-names = %5B%5D%0Afor ie in list(sorted(_ALL_CLASSES%5B:-1%5D, key=lambda cls: cls.ie_key())) + _ALL_CLASSES%5B-1:%5D
+# find the correct sorting and add the required base classes so that sublcasses%0A# can be correctly created%0Aclasses = _ALL_CLASSES%5B:-1%5D%0Aordered_cls = %5B%5D%0Awhile classes:%0A    for c in classes%5B:%5D:%0A        bases = set(c.__bases__) - set((object, InfoExtractor, SearchInfoExtractor))%0A        stop = False%0A        for b in bases:%0A            if b not in classes and b not in ordered_cls:%0A                if b.__name__ == 'GenericIE':%0A                    exit()%0A                classes.insert(0, b)%0A                stop = True%0A        if stop:%0A            break%0A        if all(b in ordered_cls for b in bases):%0A            ordered_cls.append(c)%0A            classes.remove(c)%0A            break%0Aordered_cls.append(_ALL_CLASSES%5B-1%5D)%0A%0Anames = %5B%5D%0Afor ie in ordered_cls
 :%0A  
@@ -2509,23 +2509,16 @@
  ie.
-ie_key() + 'IE'
+__name__
 %0A   
@@ -2580,16 +2580,47 @@
 nd(src)%0A
+    if ie in _ALL_CLASSES:%0A    
     name

Fix after commit 6e6b9f600f2f447604f6108fb6486b73cc25def1