def wrap_class_based_view(fun, injector):
    cls = fun.view_class
    name = fun.__name__

    closure_contents = (c.cell_contents for c in fun.__closure__)
    fun_closure = dict(zip(fun.__code__.co_freevars, closure_contents))
    try:
        class_kwargs = fun_closure['class_kwargs']
    except __HOLE__:
        # Most likely flask_restful resource, we'll see in a second
        flask_restful_api = fun_closure['self']
        # flask_restful wraps ResourceClass.as_view() result in its own wrapper
        # the as_view() result is available under 'resource' name in this closure
        fun = fun_closure['resource']
        fun_closure = {}
        class_kwargs = {}
        # if the lines above succeeded we're quite sure it's flask_restful resource
    else:
        flask_restful_api = None
        class_args = fun_closure.get('class_args')
        assert not class_args, 'Class args are not supported, use kwargs instead'

    if (
        flask_restful_api and
        flask_restplus and
        isinstance(flask_restful_api, flask_restplus.Api)
    ):
        # This is flask_restplus' add_resource implementation:
        #
        #     def add_resource(self, resource, *urls, **kwargs):
        #     (...)
        #     args = kwargs.pop('resource_class_args', [])
        #     if isinstance(args, tuple):
        #         args = list(args)
        #     args.insert(0, self)
        #     kwargs['resource_class_args'] = args
        #
        #     super(Api, self).add_resource(resource, *urls, **kwargs)
        #
        # Correspondingly, flask_restplus.Resource's constructor expects
        # flask_restplus.Api instance in its first argument; since we detected
        # that we're very likely dealing with flask_restplus we'll provide the Api
        # instance as keyword argument instead (it'll work just as well unless
        # flask_restplus changes the name of the parameter; also
        # Injector.create_object doesn't support extra positional arguments anyway).
        if 'api' in class_kwargs:
            raise AssertionError('api keyword argument is reserved')

        class_kwargs['api'] = flask_restful_api

    # This section is flask.views.View.as_view code modified to make the injection
    # possible without relying on modifying view function in place
    # Copyright (c) 2014 by Armin Ronacher and Flask contributors, see Flask
    # license for details

    def view(*args, **kwargs):
        self = injector.create_object(cls, additional_kwargs=class_kwargs)
        return self.dispatch_request(*args, **kwargs)

    if cls.decorators:
        view.__name__ = name
        view.__module__ = cls.__module__
        for decorator in cls.decorators:
            view = decorator(view)

    # We attach the view class to the view function for two reasons:
    # first of all it allows us to easily figure out what class-based
    # view this thing came from, secondly it's also used for instantiating
    # the view class so you can actually replace it with something else
    # for testing purposes and debugging.
    view.view_class = cls
    view.__name__ = name
    view.__doc__ = cls.__doc__
    view.__module__ = cls.__module__
    view.methods = cls.methods

    fun = view

    if flask_restful_api:
        return wrap_flask_restful_resource(fun, flask_restful_api, injector)

    return fun
dataset/ETHPy150Open alecthomas/flask_injector/flask_injector.py/wrap_class_based_view