PHP 8.2.30
Preview: async_utils.py Size: 1.90 KB
//opt/alt/python37/lib/python3.7/site-packages/jinja2/async_utils.py

import inspect
import typing as t
from functools import wraps

from .utils import _PassArg
from .utils import pass_eval_context

V = t.TypeVar("V")


def async_variant(normal_func):  # type: ignore
    def decorator(async_func):  # type: ignore
        pass_arg = _PassArg.from_obj(normal_func)
        need_eval_context = pass_arg is None

        if pass_arg is _PassArg.environment:

            def is_async(args: t.Any) -> bool:
                return t.cast(bool, args[0].is_async)

        else:

            def is_async(args: t.Any) -> bool:
                return t.cast(bool, args[0].environment.is_async)

        @wraps(normal_func)
        def wrapper(*args, **kwargs):  # type: ignore
            b = is_async(args)

            if need_eval_context:
                args = args[1:]

            if b:
                return async_func(*args, **kwargs)

            return normal_func(*args, **kwargs)

        if need_eval_context:
            wrapper = pass_eval_context(wrapper)

        wrapper.jinja_async_variant = True
        return wrapper

    return decorator


_common_primitives = {int, float, bool, str, list, dict, tuple, type(None)}


async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V":
    # Avoid a costly call to isawaitable
    if type(value) in _common_primitives:
        return t.cast("V", value)

    if inspect.isawaitable(value):
        return await t.cast("t.Awaitable[V]", value)

    return t.cast("V", value)


async def auto_aiter(
    iterable: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
) -> "t.AsyncIterator[V]":
    if hasattr(iterable, "__aiter__"):
        async for item in t.cast("t.AsyncIterable[V]", iterable):
            yield item
    else:
        for item in t.cast("t.Iterable[V]", iterable):
            yield item


async def auto_to_list(
    value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
) -> t.List["V"]:
    return [x async for x in auto_aiter(value)]

Directory Contents

Dirs: 1 × Files: 26

Name Size Perms Modified Actions
- drwxr-xr-x 2024-03-03 23:11:10
Edit Download
1.90 KB lrw-r--r-- 2021-11-09 17:17:58
Edit Download
12.37 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
70.52 KB lrw-r--r-- 2021-11-09 16:37:43
Edit Download
1.40 KB lrw-r--r-- 2021-04-05 17:47:37
Edit Download
8.29 KB lrw-r--r-- 2021-11-09 17:17:58
Edit Download
1.24 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
59.55 KB lrw-r--r-- 2021-11-09 18:10:36
Edit Download
4.95 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
31.37 KB lrw-r--r-- 2021-05-14 01:01:18
Edit Download
51.38 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
10.47 KB lrw-r--r-- 2021-08-10 13:34:22
Edit Download
29.23 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
22.22 KB lrw-r--r-- 2021-11-09 20:21:27
Edit Download
4.29 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
3.88 KB lrw-r--r-- 2021-11-09 17:17:58
Edit Download
33.74 KB lrw-r--r-- 2021-11-09 17:18:01
Edit Download
1.61 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
38.83 KB lrw-r--r-- 2021-05-14 01:01:13
Edit Download
0 B lrw-r--r-- 2021-05-10 13:52:40
Edit Download
34.23 KB lrw-r--r-- 2021-08-10 13:34:22
Edit Download
14.26 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
5.77 KB lrw-r--r-- 2021-04-10 17:20:38
Edit Download
26.34 KB lrw-r--r-- 2021-10-04 20:41:58
Edit Download
3.49 KB lrw-r--r-- 2021-05-10 13:52:40
Edit Download
1.73 KB lrw-r--r-- 2020-02-17 17:14:56
Edit Download
2.15 KB lrw-r--r-- 2021-11-09 20:25:25
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).