def with_nested_contexts(context_managers, func, args, kwargs):
    """Nested context manager calling

    Given a function, and keyword arguments to call it with, it will
    be wrapped in a with statment using every context manager in the
    context_managers list for nested with calling.

    Every context_manager will get the function reference, and keyword
    arguments.

    Example::

        with ContextA(func, *args, **kwargs):
            with ContextB(func, *args, **kwargs):
                return func(**kwargs)

        # is equivilant to
        ctx_managers = [ContextA, ContextB]
        return with_nested_contexts(ctx_managers, func, kwargs)

    """
    if not context_managers:
        return func(**kwargs)
    else:
        ctx_manager = context_managers[0]
        with ctx_manager(func, *args, **kwargs):
            return with_nested_contexts(context_managers[1:],
                  func, args, kwargs)