2020-01-10 21:42:02 +00:00
|
|
|
"""The runtime functions and state used by compiled templates."""
|
2021-05-08 20:03:15 +00:00
|
|
|
import functools
|
2014-06-06 16:00:04 +00:00
|
|
|
import sys
|
2021-02-24 17:47:57 +00:00
|
|
|
import typing as t
|
2020-01-28 06:05:00 +00:00
|
|
|
from collections import abc
|
2013-05-17 22:06:22 +00:00
|
|
|
from itertools import chain
|
2017-03-15 18:19:04 +00:00
|
|
|
|
2020-01-27 06:23:36 +00:00
|
|
|
from markupsafe import escape # noqa: F401
|
2020-01-10 18:40:52 +00:00
|
|
|
from markupsafe import Markup
|
2020-01-28 06:05:00 +00:00
|
|
|
from markupsafe import soft_str
|
|
|
|
|
2021-04-10 23:12:25 +00:00
|
|
|
from .async_utils import auto_aiter
|
|
|
|
from .async_utils import auto_await # noqa: F401
|
2020-01-27 06:23:36 +00:00
|
|
|
from .exceptions import TemplateNotFound # noqa: F401
|
|
|
|
from .exceptions import TemplateRuntimeError # noqa: F401
|
2020-01-09 20:03:07 +00:00
|
|
|
from .exceptions import UndefinedError
|
|
|
|
from .nodes import EvalContext
|
2021-04-10 15:58:16 +00:00
|
|
|
from .utils import _PassArg
|
2020-01-09 20:03:07 +00:00
|
|
|
from .utils import concat
|
|
|
|
from .utils import internalcode
|
|
|
|
from .utils import missing
|
2020-01-27 06:23:36 +00:00
|
|
|
from .utils import Namespace # noqa: F401
|
2020-01-09 20:03:07 +00:00
|
|
|
from .utils import object_type_repr
|
2021-04-10 15:58:16 +00:00
|
|
|
from .utils import pass_eval_context
|
2008-04-07 16:39:54 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
V = t.TypeVar("V")
|
|
|
|
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
|
|
|
|
|
2021-04-05 16:25:26 +00:00
|
|
|
if t.TYPE_CHECKING:
|
2021-05-08 20:03:15 +00:00
|
|
|
import logging
|
|
|
|
import typing_extensions as te
|
2021-04-05 16:25:26 +00:00
|
|
|
from .environment import Environment
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
class LoopRenderFunc(te.Protocol):
|
|
|
|
def __call__(
|
|
|
|
self,
|
|
|
|
reciter: t.Iterable[V],
|
|
|
|
loop_render_func: "LoopRenderFunc",
|
|
|
|
depth: int = 0,
|
|
|
|
) -> str:
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2008-04-26 14:26:52 +00:00
|
|
|
# these variables are exported to the template runtime
|
2020-01-27 06:23:36 +00:00
|
|
|
exported = [
|
2020-01-10 15:46:18 +00:00
|
|
|
"LoopContext",
|
|
|
|
"TemplateReference",
|
|
|
|
"Macro",
|
|
|
|
"Markup",
|
|
|
|
"TemplateRuntimeError",
|
|
|
|
"missing",
|
|
|
|
"escape",
|
|
|
|
"markup_join",
|
2020-01-28 06:05:00 +00:00
|
|
|
"str_join",
|
2020-01-10 15:46:18 +00:00
|
|
|
"identity",
|
|
|
|
"TemplateNotFound",
|
|
|
|
"Namespace",
|
|
|
|
"Undefined",
|
2020-06-26 19:26:04 +00:00
|
|
|
"internalcode",
|
2020-01-10 15:46:18 +00:00
|
|
|
]
|
2021-04-10 23:12:25 +00:00
|
|
|
async_exported = [
|
|
|
|
"AsyncLoopContext",
|
|
|
|
"auto_aiter",
|
|
|
|
"auto_await",
|
|
|
|
]
|
2008-04-25 21:44:14 +00:00
|
|
|
|
2010-05-29 15:35:10 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def identity(x: V) -> V:
|
2020-01-10 18:40:52 +00:00
|
|
|
"""Returns its argument. Useful for certain things in the
|
|
|
|
environment.
|
|
|
|
"""
|
|
|
|
return x
|
2012-01-24 23:42:54 +00:00
|
|
|
|
2008-04-25 21:44:14 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def markup_join(seq: t.Iterable[t.Any]) -> str:
|
2020-01-27 22:12:52 +00:00
|
|
|
"""Concatenation that escapes if necessary and converts to string."""
|
2008-04-28 10:20:12 +00:00
|
|
|
buf = []
|
2020-01-28 06:05:00 +00:00
|
|
|
iterator = map(soft_str, seq)
|
2008-04-28 10:20:12 +00:00
|
|
|
for arg in iterator:
|
|
|
|
buf.append(arg)
|
2020-01-10 15:46:18 +00:00
|
|
|
if hasattr(arg, "__html__"):
|
2020-01-29 04:16:59 +00:00
|
|
|
return Markup("").join(chain(buf, iterator))
|
2008-04-28 10:20:12 +00:00
|
|
|
return concat(buf)
|
|
|
|
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def str_join(seq: t.Iterable[t.Any]) -> str:
|
2020-01-27 22:12:52 +00:00
|
|
|
"""Simple args to string conversion and concatenation."""
|
2020-01-28 06:05:00 +00:00
|
|
|
return concat(map(str, seq))
|
|
|
|
|
|
|
|
|
2020-01-10 15:46:18 +00:00
|
|
|
def new_context(
|
2021-05-08 20:03:15 +00:00
|
|
|
environment: "Environment",
|
|
|
|
template_name: t.Optional[str],
|
|
|
|
blocks: t.Dict[str, t.Callable[["Context"], t.Iterator[str]]],
|
|
|
|
vars: t.Optional[t.Dict[str, t.Any]] = None,
|
|
|
|
shared: bool = False,
|
|
|
|
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
|
|
|
|
locals: t.Optional[t.Mapping[str, t.Any]] = None,
|
|
|
|
) -> "Context":
|
2019-07-25 13:28:26 +00:00
|
|
|
"""Internal helper for context creation."""
|
2009-02-19 14:56:53 +00:00
|
|
|
if vars is None:
|
|
|
|
vars = {}
|
|
|
|
if shared:
|
|
|
|
parent = vars
|
|
|
|
else:
|
|
|
|
parent = dict(globals or (), **vars)
|
|
|
|
if locals:
|
|
|
|
# if the parent is shared a copy should be created because
|
|
|
|
# we don't want to modify the dict passed
|
|
|
|
if shared:
|
|
|
|
parent = dict(parent)
|
2020-01-28 06:05:00 +00:00
|
|
|
for key, value in locals.items():
|
2017-01-06 19:57:30 +00:00
|
|
|
if value is not missing:
|
|
|
|
parent[key] = value
|
2020-06-22 13:54:08 +00:00
|
|
|
return environment.context_class(
|
|
|
|
environment, parent, template_name, blocks, globals=globals
|
|
|
|
)
|
2009-02-19 14:56:53 +00:00
|
|
|
|
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
class TemplateReference:
|
2009-02-19 14:56:53 +00:00
|
|
|
"""The `self` in templates."""
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __init__(self, context: "Context") -> None:
|
2009-02-19 14:56:53 +00:00
|
|
|
self.__context = context
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __getitem__(self, name: str) -> t.Any:
|
2009-02-19 14:56:53 +00:00
|
|
|
blocks = self.__context.blocks[name]
|
|
|
|
return BlockReference(name, self.__context, blocks, 0)
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"<{type(self).__name__} {self.__context.name!r}>"
|
2017-01-12 19:10:58 +00:00
|
|
|
|
2021-04-12 06:53:59 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def _dict_method_all(dict_method: F) -> F:
|
|
|
|
@functools.wraps(dict_method)
|
|
|
|
def f_all(self: "Context") -> t.Any:
|
|
|
|
return dict_method(self.get_all())
|
2017-01-12 19:10:58 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
return t.cast(F, f_all)
|
2017-01-12 19:10:58 +00:00
|
|
|
|
|
|
|
|
2020-01-28 06:05:00 +00:00
|
|
|
@abc.Mapping.register
|
2021-05-08 20:03:15 +00:00
|
|
|
class Context:
|
2008-04-30 11:03:59 +00:00
|
|
|
"""The template context holds the variables of a template. It stores the
|
|
|
|
values passed to the template and also the names the template exports.
|
|
|
|
Creating instances is neither supported nor useful as it's created
|
|
|
|
automatically at various stages of the template evaluation and should not
|
|
|
|
be created by hand.
|
|
|
|
|
|
|
|
The context is immutable. Modifications on :attr:`parent` **must not**
|
|
|
|
happen and modifications on :attr:`vars` are allowed from generated
|
|
|
|
template code only. Template filters and global functions marked as
|
2021-04-10 15:58:16 +00:00
|
|
|
:func:`pass_context` get the active context passed as first argument
|
2008-04-30 11:03:59 +00:00
|
|
|
and are allowed to access the context read-only.
|
|
|
|
|
|
|
|
The template context supports read only dict operations (`get`,
|
2008-05-06 14:04:10 +00:00
|
|
|
`keys`, `values`, `items`, `iterkeys`, `itervalues`, `iteritems`,
|
|
|
|
`__getitem__`, `__contains__`). Additionally there is a :meth:`resolve`
|
|
|
|
method that doesn't fail with a `KeyError` but returns an
|
|
|
|
:class:`Undefined` object for missing variables.
|
2008-04-08 16:49:56 +00:00
|
|
|
"""
|
2020-01-10 15:46:18 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
environment: "Environment",
|
|
|
|
parent: t.Dict[str, t.Any],
|
|
|
|
name: t.Optional[str],
|
|
|
|
blocks: t.Dict[str, t.Callable[["Context"], t.Iterator[str]]],
|
|
|
|
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
|
|
|
|
):
|
2008-04-24 19:54:44 +00:00
|
|
|
self.parent = parent
|
2021-05-08 20:03:15 +00:00
|
|
|
self.vars: t.Dict[str, t.Any] = {}
|
2021-04-05 16:25:26 +00:00
|
|
|
self.environment: "Environment" = environment
|
2010-04-05 16:11:18 +00:00
|
|
|
self.eval_ctx = EvalContext(self.environment, name)
|
2021-05-08 20:03:15 +00:00
|
|
|
self.exported_vars: t.Set[str] = set()
|
2008-04-17 09:50:39 +00:00
|
|
|
self.name = name
|
2020-06-22 13:54:08 +00:00
|
|
|
self.globals_keys = set() if globals is None else set(globals)
|
2008-04-24 19:54:44 +00:00
|
|
|
|
|
|
|
# create the initial mapping of blocks. Whenever template inheritance
|
|
|
|
# takes place the runtime will update this mapping with the new blocks
|
|
|
|
# from the template.
|
2020-01-28 06:05:00 +00:00
|
|
|
self.blocks = {k: [v] for k, v in blocks.items()}
|
2008-04-11 20:21:00 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def super(
|
|
|
|
self, name: str, current: t.Callable[["Context"], t.Iterator[str]]
|
|
|
|
) -> t.Union["BlockReference", "Undefined"]:
|
2008-04-13 21:18:05 +00:00
|
|
|
"""Render a parent block."""
|
2008-04-27 19:28:03 +00:00
|
|
|
try:
|
|
|
|
blocks = self.blocks[name]
|
2008-09-20 10:04:53 +00:00
|
|
|
index = blocks.index(current) + 1
|
|
|
|
blocks[index]
|
2008-04-27 19:28:03 +00:00
|
|
|
except LookupError:
|
2020-01-10 15:46:18 +00:00
|
|
|
return self.environment.undefined(
|
2020-01-29 04:16:59 +00:00
|
|
|
f"there is no parent block called {name!r}.", name="super"
|
2020-01-10 15:46:18 +00:00
|
|
|
)
|
2008-09-20 10:04:53 +00:00
|
|
|
return BlockReference(name, self, blocks, index)
|
2008-04-07 16:39:54 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def get(self, key: str, default: t.Any = None) -> t.Any:
|
2021-04-12 06:53:59 +00:00
|
|
|
"""Look up a variable by name, or return a default if the key is
|
|
|
|
not found.
|
|
|
|
|
|
|
|
:param key: The variable name to look up.
|
|
|
|
:param default: The value to return if the key is not found.
|
2008-04-30 11:03:59 +00:00
|
|
|
"""
|
2008-05-06 14:04:10 +00:00
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def resolve(self, key: str) -> t.Union[t.Any, "Undefined"]:
|
2021-04-12 06:53:59 +00:00
|
|
|
"""Look up a variable by name, or return an :class:`Undefined`
|
|
|
|
object if the key is not found.
|
|
|
|
|
|
|
|
If you need to add custom behavior, override
|
|
|
|
:meth:`resolve_or_missing`, not this method. The various lookup
|
|
|
|
functions use that method, not this one.
|
|
|
|
|
|
|
|
:param key: The variable name to look up.
|
2008-05-06 14:04:10 +00:00
|
|
|
"""
|
2021-04-12 06:53:59 +00:00
|
|
|
rv = self.resolve_or_missing(key)
|
|
|
|
|
2017-01-02 11:09:52 +00:00
|
|
|
if rv is missing:
|
|
|
|
return self.environment.undefined(name=key)
|
2021-04-12 06:53:59 +00:00
|
|
|
|
2017-01-02 11:09:52 +00:00
|
|
|
return rv
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def resolve_or_missing(self, key: str) -> t.Any:
|
2021-04-12 06:53:59 +00:00
|
|
|
"""Look up a variable by name, or return a ``missing`` sentinel
|
|
|
|
if the key is not found.
|
|
|
|
|
|
|
|
Override this method to add custom lookup behavior.
|
|
|
|
:meth:`resolve`, :meth:`get`, and :meth:`__getitem__` use this
|
|
|
|
method. Don't call this method directly.
|
|
|
|
|
|
|
|
:param key: The variable name to look up.
|
2017-01-02 11:09:52 +00:00
|
|
|
"""
|
2021-04-12 06:53:59 +00:00
|
|
|
if key in self.vars:
|
|
|
|
return self.vars[key]
|
|
|
|
|
|
|
|
if key in self.parent:
|
|
|
|
return self.parent[key]
|
|
|
|
|
|
|
|
return missing
|
2008-04-24 22:36:14 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def get_exported(self) -> t.Dict[str, t.Any]:
|
2008-04-24 19:54:44 +00:00
|
|
|
"""Get a new dict with the exported variables."""
|
2020-01-29 04:16:59 +00:00
|
|
|
return {k: self.vars[k] for k in self.exported_vars}
|
2008-04-24 19:54:44 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def get_all(self) -> t.Dict[str, t.Any]:
|
2017-01-07 14:35:21 +00:00
|
|
|
"""Return the complete context as dict including the exported
|
|
|
|
variables. For optimizations reasons this might not return an
|
|
|
|
actual copy so be careful with using it.
|
2008-04-30 11:03:59 +00:00
|
|
|
"""
|
2017-01-07 14:35:21 +00:00
|
|
|
if not self.vars:
|
|
|
|
return self.parent
|
|
|
|
if not self.parent:
|
|
|
|
return self.vars
|
2008-04-24 19:54:44 +00:00
|
|
|
return dict(self.parent, **self.vars)
|
|
|
|
|
2009-02-24 21:58:00 +00:00
|
|
|
@internalcode
|
2021-05-08 20:03:15 +00:00
|
|
|
def call(
|
|
|
|
__self, __obj: t.Callable, *args: t.Any, **kwargs: t.Any # noqa: B902
|
|
|
|
) -> t.Union[t.Any, "Undefined"]:
|
2008-05-28 09:26:59 +00:00
|
|
|
"""Call the callable with the arguments and keyword arguments
|
|
|
|
provided but inject the active context or environment as first
|
2021-04-10 15:58:16 +00:00
|
|
|
argument if the callable has :func:`pass_context` or
|
|
|
|
:func:`pass_environment`.
|
2008-05-28 09:26:59 +00:00
|
|
|
"""
|
2008-05-26 11:35:58 +00:00
|
|
|
if __debug__:
|
2014-06-06 16:00:04 +00:00
|
|
|
__traceback_hide__ = True # noqa
|
2013-05-18 10:58:12 +00:00
|
|
|
|
2013-04-16 05:49:02 +00:00
|
|
|
# Allow callable classes to take a context
|
2021-04-10 15:58:16 +00:00
|
|
|
if (
|
|
|
|
hasattr(__obj, "__call__") # noqa: B004
|
2024-05-28 02:37:23 +00:00
|
|
|
and _PassArg.from_obj(__obj.__call__) is not None
|
2021-04-10 15:58:16 +00:00
|
|
|
):
|
2024-05-28 02:37:23 +00:00
|
|
|
__obj = __obj.__call__
|
2021-05-08 20:03:15 +00:00
|
|
|
|
|
|
|
pass_arg = _PassArg.from_obj(__obj)
|
|
|
|
|
|
|
|
if pass_arg is _PassArg.context:
|
|
|
|
# the active context should have access to variables set in
|
|
|
|
# loops and blocks without mutating the context itself
|
|
|
|
if kwargs.get("_loop_vars"):
|
|
|
|
__self = __self.derived(kwargs["_loop_vars"])
|
|
|
|
if kwargs.get("_block_vars"):
|
|
|
|
__self = __self.derived(kwargs["_block_vars"])
|
|
|
|
args = (__self,) + args
|
|
|
|
elif pass_arg is _PassArg.eval_context:
|
|
|
|
args = (__self.eval_ctx,) + args
|
|
|
|
elif pass_arg is _PassArg.environment:
|
|
|
|
args = (__self.environment,) + args
|
2020-06-23 14:53:59 +00:00
|
|
|
|
|
|
|
kwargs.pop("_block_vars", None)
|
|
|
|
kwargs.pop("_loop_vars", None)
|
2021-05-08 20:03:15 +00:00
|
|
|
|
2010-06-05 12:32:06 +00:00
|
|
|
try:
|
|
|
|
return __obj(*args, **kwargs)
|
|
|
|
except StopIteration:
|
2020-01-10 15:46:18 +00:00
|
|
|
return __self.environment.undefined(
|
2020-01-29 04:16:59 +00:00
|
|
|
"value was undefined because a callable raised a"
|
|
|
|
" StopIteration exception"
|
2020-01-10 15:46:18 +00:00
|
|
|
)
|
2008-05-24 22:16:51 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def derived(self, locals: t.Optional[t.Dict[str, t.Any]] = None) -> "Context":
|
2017-01-08 10:21:32 +00:00
|
|
|
"""Internal helper function to create a derived context. This is
|
|
|
|
used in situations where the system needs a new context in the same
|
|
|
|
template that is independent.
|
|
|
|
"""
|
2020-01-10 15:46:18 +00:00
|
|
|
context = new_context(
|
|
|
|
self.environment, self.name, {}, self.get_all(), True, None, locals
|
|
|
|
)
|
2010-03-14 18:43:47 +00:00
|
|
|
context.eval_ctx = self.eval_ctx
|
2020-01-28 06:05:00 +00:00
|
|
|
context.blocks.update((k, list(v)) for k, v in self.blocks.items())
|
2009-02-19 15:11:11 +00:00
|
|
|
return context
|
2009-02-19 14:56:53 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
keys = _dict_method_all(dict.keys)
|
|
|
|
values = _dict_method_all(dict.values)
|
|
|
|
items = _dict_method_all(dict.items)
|
2008-05-06 14:04:10 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __contains__(self, name: str) -> bool:
|
2008-04-24 22:36:14 +00:00
|
|
|
return name in self.vars or name in self.parent
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __getitem__(self, key: str) -> t.Any:
|
2021-04-12 06:53:59 +00:00
|
|
|
"""Look up a variable by name with ``[]`` syntax, or raise a
|
|
|
|
``KeyError`` if the key is not found.
|
2008-05-18 22:23:37 +00:00
|
|
|
"""
|
2017-01-02 11:09:52 +00:00
|
|
|
item = self.resolve_or_missing(key)
|
2021-04-12 06:53:59 +00:00
|
|
|
|
2017-01-02 11:09:52 +00:00
|
|
|
if item is missing:
|
2008-05-18 22:23:37 +00:00
|
|
|
raise KeyError(key)
|
2021-04-12 06:53:59 +00:00
|
|
|
|
2008-05-18 22:23:37 +00:00
|
|
|
return item
|
2008-04-08 16:09:13 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"<{type(self).__name__} {self.get_all()!r} of {self.name!r}>"
|
2008-04-11 20:21:00 +00:00
|
|
|
|
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
class BlockReference:
|
2008-09-20 10:04:53 +00:00
|
|
|
"""One block on a template reference."""
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
context: "Context",
|
|
|
|
stack: t.List[t.Callable[["Context"], t.Iterator[str]]],
|
|
|
|
depth: int,
|
|
|
|
) -> None:
|
2008-09-20 10:04:53 +00:00
|
|
|
self.name = name
|
|
|
|
self._context = context
|
|
|
|
self._stack = stack
|
|
|
|
self._depth = depth
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def super(self) -> t.Union["BlockReference", "Undefined"]:
|
2008-09-20 10:04:53 +00:00
|
|
|
"""Super the block."""
|
|
|
|
if self._depth + 1 >= len(self._stack):
|
2020-01-10 15:46:18 +00:00
|
|
|
return self._context.environment.undefined(
|
2020-01-29 04:16:59 +00:00
|
|
|
f"there is no parent block called {self.name!r}.", name="super"
|
2020-01-10 15:46:18 +00:00
|
|
|
)
|
|
|
|
return BlockReference(self.name, self._context, self._stack, self._depth + 1)
|
2008-09-20 10:04:53 +00:00
|
|
|
|
2021-04-10 23:12:25 +00:00
|
|
|
@internalcode
|
2021-05-08 20:03:15 +00:00
|
|
|
async def _async_call(self) -> str:
|
|
|
|
rv = concat(
|
|
|
|
[x async for x in self._stack[self._depth](self._context)] # type: ignore
|
|
|
|
)
|
2021-04-10 23:12:25 +00:00
|
|
|
|
|
|
|
if self._context.eval_ctx.autoescape:
|
|
|
|
return Markup(rv)
|
|
|
|
|
|
|
|
return rv
|
|
|
|
|
2009-02-24 21:58:00 +00:00
|
|
|
@internalcode
|
2021-05-08 20:03:15 +00:00
|
|
|
def __call__(self) -> str:
|
2021-04-10 23:12:25 +00:00
|
|
|
if self._context.environment.is_async:
|
2021-05-08 20:03:15 +00:00
|
|
|
return self._async_call() # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
|
2008-09-20 10:04:53 +00:00
|
|
|
rv = concat(self._stack[self._depth](self._context))
|
2021-04-10 23:12:25 +00:00
|
|
|
|
2010-04-05 16:11:18 +00:00
|
|
|
if self._context.eval_ctx.autoescape:
|
2021-04-10 23:12:25 +00:00
|
|
|
return Markup(rv)
|
|
|
|
|
2008-09-20 10:04:53 +00:00
|
|
|
return rv
|
|
|
|
|
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
class LoopContext:
|
|
|
|
"""A wrapper iterable for dynamic ``for`` loops, with information
|
|
|
|
about the loop and iteration.
|
|
|
|
"""
|
|
|
|
|
|
|
|
#: Current iteration of the loop, starting at 0.
|
|
|
|
index0 = -1
|
2008-04-09 10:14:24 +00:00
|
|
|
|
2021-02-24 17:47:57 +00:00
|
|
|
_length: t.Optional[int] = None
|
2021-05-08 20:03:15 +00:00
|
|
|
_after: t.Any = missing
|
|
|
|
_current: t.Any = missing
|
|
|
|
_before: t.Any = missing
|
|
|
|
_last_changed_value: t.Any = missing
|
2016-12-28 19:06:34 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
iterable: t.Iterable[V],
|
|
|
|
undefined: t.Type["Undefined"],
|
|
|
|
recurse: t.Optional["LoopRenderFunc"] = None,
|
|
|
|
depth0: int = 0,
|
|
|
|
) -> None:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""
|
|
|
|
:param iterable: Iterable to wrap.
|
|
|
|
:param undefined: :class:`Undefined` class to use for next and
|
|
|
|
previous items.
|
|
|
|
:param recurse: The function to render the loop body when the
|
|
|
|
loop is marked recursive.
|
|
|
|
:param depth0: Incremented when looping recursively.
|
|
|
|
"""
|
|
|
|
self._iterable = iterable
|
|
|
|
self._iterator = self._to_iterator(iterable)
|
2017-02-01 20:05:03 +00:00
|
|
|
self._undefined = undefined
|
2008-05-11 21:21:16 +00:00
|
|
|
self._recurse = recurse
|
2019-11-07 21:35:57 +00:00
|
|
|
#: How many levels deep a recursive loop currently is, starting at 0.
|
2013-05-20 08:26:57 +00:00
|
|
|
self.depth0 = depth0
|
2008-07-04 14:35:10 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
@staticmethod
|
2021-05-08 20:03:15 +00:00
|
|
|
def _to_iterator(iterable: t.Iterable[V]) -> t.Iterator[V]:
|
2019-11-07 21:35:57 +00:00
|
|
|
return iter(iterable)
|
2008-04-16 12:21:57 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def length(self) -> int:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Length of the iterable.
|
2017-02-01 20:47:17 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
If the iterable is a generator or otherwise does not have a
|
|
|
|
size, it is eagerly evaluated to get a size.
|
|
|
|
"""
|
|
|
|
if self._length is not None:
|
|
|
|
return self._length
|
2008-04-16 12:21:57 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
try:
|
2021-05-08 20:03:15 +00:00
|
|
|
self._length = len(self._iterable) # type: ignore
|
2019-11-07 21:35:57 +00:00
|
|
|
except TypeError:
|
|
|
|
iterable = list(self._iterator)
|
|
|
|
self._iterator = self._to_iterator(iterable)
|
|
|
|
self._length = len(iterable) + self.index + (self._after is not missing)
|
2017-02-01 20:05:03 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
return self._length
|
2017-02-01 20:05:03 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __len__(self) -> int:
|
2008-04-16 12:21:57 +00:00
|
|
|
return self.length
|
2008-04-09 12:02:55 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def depth(self) -> int:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""How many levels deep a recursive loop currently is, starting at 1."""
|
|
|
|
return self.depth0 + 1
|
2008-05-11 21:21:16 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def index(self) -> int:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Current iteration of the loop, starting at 1."""
|
|
|
|
return self.index0 + 1
|
2008-05-11 21:42:19 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def revindex0(self) -> int:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Number of iterations from the end of the loop, ending at 0.
|
2008-04-16 12:21:57 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
Requires calculating :attr:`length`.
|
|
|
|
"""
|
|
|
|
return self.length - self.index
|
2008-04-09 10:14:24 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def revindex(self) -> int:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Number of iterations from the end of the loop, ending at 1.
|
2016-12-28 19:06:34 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
Requires calculating :attr:`length`.
|
|
|
|
"""
|
|
|
|
return self.length - self.index0
|
2016-12-28 19:06:34 +00:00
|
|
|
|
2016-12-28 20:49:00 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def first(self) -> bool:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Whether this is the first iteration of the loop."""
|
|
|
|
return self.index0 == 0
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def _peek_next(self) -> t.Any:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Return the next element in the iterable, or :data:`missing`
|
|
|
|
if the iterable is exhausted. Only peeks one item ahead, caching
|
|
|
|
the result in :attr:`_last` for use in subsequent checks. The
|
|
|
|
cache is reset when :meth:`__next__` is called.
|
2019-05-06 21:17:43 +00:00
|
|
|
"""
|
2019-11-07 21:35:57 +00:00
|
|
|
if self._after is not missing:
|
|
|
|
return self._after
|
|
|
|
|
|
|
|
self._after = next(self._iterator, missing)
|
|
|
|
return self._after
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def last(self) -> bool:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Whether this is the last iteration of the loop.
|
|
|
|
|
|
|
|
Causes the iterable to advance early. See
|
|
|
|
:func:`itertools.groupby` for issues this can cause.
|
|
|
|
The :func:`groupby` filter avoids that issue.
|
2019-05-06 21:17:43 +00:00
|
|
|
"""
|
2019-11-07 21:35:57 +00:00
|
|
|
return self._peek_next() is missing
|
2016-12-28 20:49:00 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def previtem(self) -> t.Union[t.Any, "Undefined"]:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""The item in the previous iteration. Undefined during the
|
|
|
|
first iteration.
|
|
|
|
"""
|
|
|
|
if self.first:
|
|
|
|
return self._undefined("there is no previous item")
|
2016-12-28 19:06:34 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
return self._before
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def nextitem(self) -> t.Union[t.Any, "Undefined"]:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""The item in the next iteration. Undefined during the last
|
|
|
|
iteration.
|
2016-12-28 19:06:34 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
Causes the iterable to advance early. See
|
|
|
|
:func:`itertools.groupby` for issues this can cause.
|
2021-05-08 20:03:15 +00:00
|
|
|
The :func:`jinja-filters.groupby` filter avoids that issue.
|
2019-11-07 21:35:57 +00:00
|
|
|
"""
|
|
|
|
rv = self._peek_next()
|
2016-12-28 19:06:34 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
if rv is missing:
|
|
|
|
return self._undefined("there is no next item")
|
2008-05-18 18:25:28 +00:00
|
|
|
|
2019-11-07 21:35:57 +00:00
|
|
|
return rv
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def cycle(self, *args: V) -> V:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Return a value from the given args, cycling through based on
|
|
|
|
the current :attr:`index0`.
|
|
|
|
|
|
|
|
:param args: One or more values to cycle through.
|
|
|
|
"""
|
|
|
|
if not args:
|
|
|
|
raise TypeError("no items for cycling given")
|
|
|
|
|
|
|
|
return args[self.index0 % len(args)]
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def changed(self, *value: t.Any) -> bool:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""Return ``True`` if previously called with a different value
|
|
|
|
(including when called for the first time).
|
|
|
|
|
|
|
|
:param value: One or more values to compare to the last call.
|
|
|
|
"""
|
|
|
|
if self._last_changed_value != value:
|
|
|
|
self._last_changed_value = value
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2008-05-18 18:25:28 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __iter__(self) -> "LoopContext":
|
2008-05-18 18:25:28 +00:00
|
|
|
return self
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __next__(self) -> t.Tuple[t.Any, "LoopContext"]:
|
2019-11-07 21:35:57 +00:00
|
|
|
if self._after is not missing:
|
|
|
|
rv = self._after
|
|
|
|
self._after = missing
|
|
|
|
else:
|
|
|
|
rv = next(self._iterator)
|
|
|
|
|
|
|
|
self.index0 += 1
|
|
|
|
self._before = self._current
|
|
|
|
self._current = rv
|
|
|
|
return rv, self
|
|
|
|
|
2019-11-20 20:38:16 +00:00
|
|
|
@internalcode
|
2021-05-08 20:03:15 +00:00
|
|
|
def __call__(self, iterable: t.Iterable[V]) -> str:
|
2019-11-07 21:35:57 +00:00
|
|
|
"""When iterating over nested data, render the body of the loop
|
|
|
|
recursively with the given inner iterable data.
|
|
|
|
|
|
|
|
The loop must have the ``recursive`` marker for this to work.
|
|
|
|
"""
|
|
|
|
if self._recurse is None:
|
|
|
|
raise TypeError(
|
2020-01-10 15:46:18 +00:00
|
|
|
"The loop must have the 'recursive' marker to be called recursively."
|
2019-11-07 21:35:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return self._recurse(iterable, self._recurse, depth=self.depth)
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"<{type(self).__name__} {self.index}/{self.length}>"
|
2008-05-18 18:25:28 +00:00
|
|
|
|
|
|
|
|
2021-04-10 23:12:25 +00:00
|
|
|
class AsyncLoopContext(LoopContext):
|
2021-05-08 20:03:15 +00:00
|
|
|
_iterator: t.AsyncIterator[t.Any] # type: ignore
|
|
|
|
|
2021-04-10 23:12:25 +00:00
|
|
|
@staticmethod
|
2021-05-08 20:03:15 +00:00
|
|
|
def _to_iterator( # type: ignore
|
|
|
|
iterable: t.Union[t.Iterable[V], t.AsyncIterable[V]]
|
|
|
|
) -> t.AsyncIterator[V]:
|
2021-04-10 23:12:25 +00:00
|
|
|
return auto_aiter(iterable)
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
async def length(self) -> int: # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
if self._length is not None:
|
|
|
|
return self._length
|
|
|
|
|
|
|
|
try:
|
2021-05-08 20:03:15 +00:00
|
|
|
self._length = len(self._iterable) # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
except TypeError:
|
|
|
|
iterable = [x async for x in self._iterator]
|
|
|
|
self._iterator = self._to_iterator(iterable)
|
|
|
|
self._length = len(iterable) + self.index + (self._after is not missing)
|
|
|
|
|
|
|
|
return self._length
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
async def revindex0(self) -> int: # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
return await self.length - self.index
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
async def revindex(self) -> int: # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
return await self.length - self.index0
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
async def _peek_next(self) -> t.Any:
|
2021-04-10 23:12:25 +00:00
|
|
|
if self._after is not missing:
|
|
|
|
return self._after
|
|
|
|
|
|
|
|
try:
|
|
|
|
self._after = await self._iterator.__anext__()
|
|
|
|
except StopAsyncIteration:
|
|
|
|
self._after = missing
|
|
|
|
|
|
|
|
return self._after
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
async def last(self) -> bool: # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
return await self._peek_next() is missing
|
|
|
|
|
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
async def nextitem(self) -> t.Union[t.Any, "Undefined"]:
|
2021-04-10 23:12:25 +00:00
|
|
|
rv = await self._peek_next()
|
|
|
|
|
|
|
|
if rv is missing:
|
|
|
|
return self._undefined("there is no next item")
|
|
|
|
|
|
|
|
return rv
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __aiter__(self) -> "AsyncLoopContext":
|
2021-04-10 23:12:25 +00:00
|
|
|
return self
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
async def __anext__(self) -> t.Tuple[t.Any, "AsyncLoopContext"]:
|
2021-04-10 23:12:25 +00:00
|
|
|
if self._after is not missing:
|
|
|
|
rv = self._after
|
|
|
|
self._after = missing
|
|
|
|
else:
|
|
|
|
rv = await self._iterator.__anext__()
|
|
|
|
|
|
|
|
self.index0 += 1
|
|
|
|
self._before = self._current
|
|
|
|
self._current = rv
|
|
|
|
return rv, self
|
|
|
|
|
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
class Macro:
|
2010-06-05 12:32:06 +00:00
|
|
|
"""Wraps a macro function."""
|
2008-04-08 16:09:13 +00:00
|
|
|
|
2020-01-10 15:46:18 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-05-08 20:03:15 +00:00
|
|
|
environment: "Environment",
|
|
|
|
func: t.Callable[..., str],
|
|
|
|
name: str,
|
|
|
|
arguments: t.List[str],
|
|
|
|
catch_kwargs: bool,
|
|
|
|
catch_varargs: bool,
|
|
|
|
caller: bool,
|
|
|
|
default_autoescape: t.Optional[bool] = None,
|
2020-01-10 15:46:18 +00:00
|
|
|
):
|
2008-04-14 20:53:58 +00:00
|
|
|
self._environment = environment
|
2008-04-12 12:19:36 +00:00
|
|
|
self._func = func
|
2008-04-29 11:43:16 +00:00
|
|
|
self._argument_count = len(arguments)
|
2008-04-08 16:09:13 +00:00
|
|
|
self.name = name
|
|
|
|
self.arguments = arguments
|
2008-04-25 09:44:59 +00:00
|
|
|
self.catch_kwargs = catch_kwargs
|
|
|
|
self.catch_varargs = catch_varargs
|
2008-04-12 12:19:36 +00:00
|
|
|
self.caller = caller
|
2020-01-10 15:46:18 +00:00
|
|
|
self.explicit_caller = "caller" in arguments
|
2021-05-08 20:03:15 +00:00
|
|
|
|
2017-01-06 13:29:23 +00:00
|
|
|
if default_autoescape is None:
|
2021-05-08 20:03:15 +00:00
|
|
|
if callable(environment.autoescape):
|
|
|
|
default_autoescape = environment.autoescape(None)
|
|
|
|
else:
|
|
|
|
default_autoescape = environment.autoescape
|
|
|
|
|
2017-01-06 13:29:23 +00:00
|
|
|
self._default_autoescape = default_autoescape
|
2008-04-08 16:09:13 +00:00
|
|
|
|
2009-02-24 21:58:00 +00:00
|
|
|
@internalcode
|
2021-04-10 15:58:16 +00:00
|
|
|
@pass_eval_context
|
2021-05-08 20:03:15 +00:00
|
|
|
def __call__(self, *args: t.Any, **kwargs: t.Any) -> str:
|
2017-01-06 13:29:23 +00:00
|
|
|
# This requires a bit of explanation, In the past we used to
|
|
|
|
# decide largely based on compile-time information if a macro is
|
|
|
|
# safe or unsafe. While there was a volatile mode it was largely
|
|
|
|
# unused for deciding on escaping. This turns out to be
|
2019-09-04 22:12:07 +00:00
|
|
|
# problematic for macros because whether a macro is safe depends not
|
|
|
|
# on the escape mode when it was defined, but rather when it was used.
|
2017-01-06 13:29:23 +00:00
|
|
|
#
|
|
|
|
# Because however we export macros from the module system and
|
|
|
|
# there are historic callers that do not pass an eval context (and
|
|
|
|
# will continue to not pass one), we need to perform an instance
|
|
|
|
# check here.
|
|
|
|
#
|
|
|
|
# This is considered safe because an eval context is not a valid
|
2019-07-07 13:07:29 +00:00
|
|
|
# argument to callables otherwise anyway. Worst case here is
|
2017-01-06 13:29:23 +00:00
|
|
|
# that if no eval context is passed we fall back to the compile
|
|
|
|
# time autoescape flag.
|
|
|
|
if args and isinstance(args[0], EvalContext):
|
|
|
|
autoescape = args[0].autoescape
|
|
|
|
args = args[1:]
|
|
|
|
else:
|
|
|
|
autoescape = self._default_autoescape
|
|
|
|
|
2010-06-05 12:32:06 +00:00
|
|
|
# try to consume the positional arguments
|
2020-01-10 15:46:18 +00:00
|
|
|
arguments = list(args[: self._argument_count])
|
2010-06-05 12:32:06 +00:00
|
|
|
off = len(arguments)
|
|
|
|
|
2017-01-08 01:16:41 +00:00
|
|
|
# For information why this is necessary refer to the handling
|
|
|
|
# of caller in the `macro_body` handler in the compiler.
|
|
|
|
found_caller = False
|
|
|
|
|
2010-06-05 12:32:06 +00:00
|
|
|
# if the number of arguments consumed is not the number of
|
|
|
|
# arguments expected we start filling in keyword arguments
|
|
|
|
# and defaults.
|
|
|
|
if off != self._argument_count:
|
2020-01-10 18:40:52 +00:00
|
|
|
for name in self.arguments[len(arguments) :]:
|
2008-04-08 16:49:56 +00:00
|
|
|
try:
|
|
|
|
value = kwargs.pop(name)
|
2010-06-05 12:32:06 +00:00
|
|
|
except KeyError:
|
2017-01-03 17:19:31 +00:00
|
|
|
value = missing
|
2020-01-10 15:46:18 +00:00
|
|
|
if name == "caller":
|
2017-01-08 01:16:41 +00:00
|
|
|
found_caller = True
|
2010-06-05 12:32:06 +00:00
|
|
|
arguments.append(value)
|
2017-01-08 01:16:41 +00:00
|
|
|
else:
|
|
|
|
found_caller = self.explicit_caller
|
2008-04-26 14:26:52 +00:00
|
|
|
|
|
|
|
# it's important that the order of these arguments does not change
|
|
|
|
# if not also changed in the compiler's `function_scoping` method.
|
|
|
|
# the order is caller, keyword arguments, positional arguments!
|
2017-01-08 01:16:41 +00:00
|
|
|
if self.caller and not found_caller:
|
2020-01-10 15:46:18 +00:00
|
|
|
caller = kwargs.pop("caller", None)
|
2008-04-12 12:19:36 +00:00
|
|
|
if caller is None:
|
2020-01-10 15:46:18 +00:00
|
|
|
caller = self._environment.undefined("No caller defined", name="caller")
|
2008-04-26 14:26:52 +00:00
|
|
|
arguments.append(caller)
|
2017-01-08 01:16:41 +00:00
|
|
|
|
2008-04-25 09:44:59 +00:00
|
|
|
if self.catch_kwargs:
|
2008-04-26 14:26:52 +00:00
|
|
|
arguments.append(kwargs)
|
2008-04-25 09:44:59 +00:00
|
|
|
elif kwargs:
|
2020-01-10 15:46:18 +00:00
|
|
|
if "caller" in kwargs:
|
|
|
|
raise TypeError(
|
2020-01-29 04:16:59 +00:00
|
|
|
f"macro {self.name!r} was invoked with two values for the special"
|
|
|
|
" caller argument. This is most likely a bug."
|
2020-01-10 15:46:18 +00:00
|
|
|
)
|
|
|
|
raise TypeError(
|
2020-01-29 04:16:59 +00:00
|
|
|
f"macro {self.name!r} takes no keyword argument {next(iter(kwargs))!r}"
|
2020-01-10 15:46:18 +00:00
|
|
|
)
|
2008-04-25 09:44:59 +00:00
|
|
|
if self.catch_varargs:
|
2020-01-10 15:46:18 +00:00
|
|
|
arguments.append(args[self._argument_count :])
|
2008-05-01 10:49:53 +00:00
|
|
|
elif len(args) > self._argument_count:
|
2020-01-10 15:46:18 +00:00
|
|
|
raise TypeError(
|
2020-01-29 04:16:59 +00:00
|
|
|
f"macro {self.name!r} takes not more than"
|
|
|
|
f" {len(self.arguments)} argument(s)"
|
2020-01-10 15:46:18 +00:00
|
|
|
)
|
2017-01-06 13:29:23 +00:00
|
|
|
|
2017-01-28 14:33:09 +00:00
|
|
|
return self._invoke(arguments, autoescape)
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
async def _async_invoke(self, arguments: t.List[t.Any], autoescape: bool) -> str:
|
|
|
|
rv = await self._func(*arguments) # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
|
|
|
|
if autoescape:
|
|
|
|
return Markup(rv)
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
return rv # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def _invoke(self, arguments: t.List[t.Any], autoescape: bool) -> str:
|
2021-04-10 23:12:25 +00:00
|
|
|
if self._environment.is_async:
|
2021-05-08 20:03:15 +00:00
|
|
|
return self._async_invoke(arguments, autoescape) # type: ignore
|
2021-04-10 23:12:25 +00:00
|
|
|
|
2017-01-06 13:29:23 +00:00
|
|
|
rv = self._func(*arguments)
|
2021-04-10 23:12:25 +00:00
|
|
|
|
2017-01-06 13:29:23 +00:00
|
|
|
if autoescape:
|
|
|
|
rv = Markup(rv)
|
2021-04-10 23:12:25 +00:00
|
|
|
|
2017-01-06 13:29:23 +00:00
|
|
|
return rv
|
2008-04-12 12:19:36 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __repr__(self) -> str:
|
2020-01-29 04:16:59 +00:00
|
|
|
name = "anonymous" if self.name is None else repr(self.name)
|
2021-05-08 20:03:15 +00:00
|
|
|
return f"<{type(self).__name__} {name}>"
|
2008-04-09 12:02:55 +00:00
|
|
|
|
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
class Undefined:
|
2008-04-28 10:20:12 +00:00
|
|
|
"""The default undefined type. This undefined type can be printed and
|
2019-05-08 14:47:33 +00:00
|
|
|
iterated over, but every other access will raise an :exc:`UndefinedError`:
|
2008-04-28 10:20:12 +00:00
|
|
|
|
|
|
|
>>> foo = Undefined(name='foo')
|
|
|
|
>>> str(foo)
|
|
|
|
''
|
|
|
|
>>> not foo
|
|
|
|
True
|
|
|
|
>>> foo + 42
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-01-27 05:12:52 +00:00
|
|
|
jinja2.exceptions.UndefinedError: 'foo' is undefined
|
2008-04-14 20:53:58 +00:00
|
|
|
"""
|
2020-01-10 15:46:18 +00:00
|
|
|
|
|
|
|
__slots__ = (
|
|
|
|
"_undefined_hint",
|
|
|
|
"_undefined_obj",
|
|
|
|
"_undefined_name",
|
|
|
|
"_undefined_exception",
|
|
|
|
)
|
2008-04-09 12:02:55 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hint: t.Optional[str] = None,
|
|
|
|
obj: t.Any = missing,
|
|
|
|
name: t.Optional[str] = None,
|
|
|
|
exc: t.Type[TemplateRuntimeError] = UndefinedError,
|
|
|
|
) -> None:
|
2008-04-17 16:44:07 +00:00
|
|
|
self._undefined_hint = hint
|
|
|
|
self._undefined_obj = obj
|
|
|
|
self._undefined_name = name
|
2008-05-07 10:17:18 +00:00
|
|
|
self._undefined_exception = exc
|
2008-04-09 12:02:55 +00:00
|
|
|
|
2019-12-05 21:46:36 +00:00
|
|
|
@property
|
2021-05-08 20:03:15 +00:00
|
|
|
def _undefined_message(self) -> str:
|
2019-12-05 21:46:36 +00:00
|
|
|
"""Build a message about the undefined value based on how it was
|
|
|
|
accessed.
|
|
|
|
"""
|
|
|
|
if self._undefined_hint:
|
|
|
|
return self._undefined_hint
|
|
|
|
|
|
|
|
if self._undefined_obj is missing:
|
2020-01-29 04:16:59 +00:00
|
|
|
return f"{self._undefined_name!r} is undefined"
|
2019-12-05 21:46:36 +00:00
|
|
|
|
2020-01-28 06:05:00 +00:00
|
|
|
if not isinstance(self._undefined_name, str):
|
2020-01-29 04:16:59 +00:00
|
|
|
return (
|
|
|
|
f"{object_type_repr(self._undefined_obj)} has no"
|
|
|
|
f" element {self._undefined_name!r}"
|
2019-12-05 21:46:36 +00:00
|
|
|
)
|
|
|
|
|
2020-01-29 04:16:59 +00:00
|
|
|
return (
|
|
|
|
f"{object_type_repr(self._undefined_obj)!r} has no"
|
|
|
|
f" attribute {self._undefined_name!r}"
|
2019-12-05 21:46:36 +00:00
|
|
|
)
|
|
|
|
|
2009-02-24 21:58:00 +00:00
|
|
|
@internalcode
|
2021-05-14 00:24:19 +00:00
|
|
|
def _fail_with_undefined_error(
|
|
|
|
self, *args: t.Any, **kwargs: t.Any
|
|
|
|
) -> "te.NoReturn":
|
2019-12-05 21:46:36 +00:00
|
|
|
"""Raise an :exc:`UndefinedError` when operations are performed
|
|
|
|
on the undefined value.
|
2008-05-28 09:26:59 +00:00
|
|
|
"""
|
2019-12-05 21:46:36 +00:00
|
|
|
raise self._undefined_exception(self._undefined_message)
|
2008-05-28 09:26:59 +00:00
|
|
|
|
2010-11-19 12:51:38 +00:00
|
|
|
@internalcode
|
2021-05-08 20:03:15 +00:00
|
|
|
def __getattr__(self, name: str) -> t.Any:
|
2020-01-10 15:46:18 +00:00
|
|
|
if name[:2] == "__":
|
2010-11-19 12:51:38 +00:00
|
|
|
raise AttributeError(name)
|
2021-05-08 20:03:15 +00:00
|
|
|
|
2010-11-19 12:51:38 +00:00
|
|
|
return self._fail_with_undefined_error()
|
|
|
|
|
2020-01-28 06:05:00 +00:00
|
|
|
__add__ = __radd__ = __sub__ = __rsub__ = _fail_with_undefined_error
|
|
|
|
__mul__ = __rmul__ = __div__ = __rdiv__ = _fail_with_undefined_error
|
|
|
|
__truediv__ = __rtruediv__ = _fail_with_undefined_error
|
|
|
|
__floordiv__ = __rfloordiv__ = _fail_with_undefined_error
|
|
|
|
__mod__ = __rmod__ = _fail_with_undefined_error
|
|
|
|
__pos__ = __neg__ = _fail_with_undefined_error
|
2021-05-26 09:54:58 +00:00
|
|
|
__call__ = __getitem__ = _fail_with_undefined_error
|
2020-01-28 06:05:00 +00:00
|
|
|
__lt__ = __le__ = __gt__ = __ge__ = _fail_with_undefined_error
|
|
|
|
__int__ = __float__ = __complex__ = _fail_with_undefined_error
|
|
|
|
__pow__ = __rpow__ = _fail_with_undefined_error
|
2008-04-14 20:53:58 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __eq__(self, other: t.Any) -> bool:
|
2013-08-07 11:48:37 +00:00
|
|
|
return type(self) is type(other)
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __ne__(self, other: t.Any) -> bool:
|
2013-08-07 11:48:37 +00:00
|
|
|
return not self.__eq__(other)
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __hash__(self) -> int:
|
2013-08-07 11:48:37 +00:00
|
|
|
return id(type(self))
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __str__(self) -> str:
|
2020-01-29 04:16:59 +00:00
|
|
|
return ""
|
2008-04-24 19:54:44 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __len__(self) -> int:
|
2008-04-09 12:02:55 +00:00
|
|
|
return 0
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __iter__(self) -> t.Iterator[t.Any]:
|
2021-02-24 17:47:57 +00:00
|
|
|
yield from ()
|
2008-04-14 20:53:58 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
async def __aiter__(self) -> t.AsyncIterator[t.Any]:
|
2020-09-23 10:34:58 +00:00
|
|
|
for _ in ():
|
|
|
|
yield
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __bool__(self) -> bool:
|
2008-04-14 20:53:58 +00:00
|
|
|
return False
|
2020-01-10 15:46:18 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __repr__(self) -> str:
|
2020-01-10 15:46:18 +00:00
|
|
|
return "Undefined"
|
2008-05-28 09:26:59 +00:00
|
|
|
|
2008-04-14 20:53:58 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def make_logging_undefined(
|
|
|
|
logger: t.Optional["logging.Logger"] = None, base: t.Type[Undefined] = Undefined
|
|
|
|
) -> t.Type[Undefined]:
|
2014-06-06 16:14:45 +00:00
|
|
|
"""Given a logger object this returns a new undefined class that will
|
|
|
|
log certain failures. It will log iterations and printing. If no
|
|
|
|
logger is given a default logger is created.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
LoggingUndefined = make_logging_undefined(
|
|
|
|
logger=logger,
|
|
|
|
base=Undefined
|
|
|
|
)
|
|
|
|
|
|
|
|
.. versionadded:: 2.8
|
|
|
|
|
|
|
|
:param logger: the logger to use. If not provided, a default logger
|
|
|
|
is created.
|
|
|
|
:param base: the base class to add logging functionality to. This
|
|
|
|
defaults to :class:`Undefined`.
|
2014-06-06 16:00:04 +00:00
|
|
|
"""
|
|
|
|
if logger is None:
|
|
|
|
import logging
|
2020-01-10 15:46:18 +00:00
|
|
|
|
2014-06-06 16:00:04 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger.addHandler(logging.StreamHandler(sys.stderr))
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def _log_message(undef: Undefined) -> None:
|
2024-05-28 02:37:23 +00:00
|
|
|
logger.warning("Template variable warning: %s", undef._undefined_message)
|
2021-05-08 20:03:15 +00:00
|
|
|
|
|
|
|
class LoggingUndefined(base): # type: ignore
|
|
|
|
__slots__ = ()
|
2014-06-06 16:14:45 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def _fail_with_undefined_error( # type: ignore
|
|
|
|
self, *args: t.Any, **kwargs: t.Any
|
2021-05-14 00:24:19 +00:00
|
|
|
) -> "te.NoReturn":
|
2014-06-06 16:14:45 +00:00
|
|
|
try:
|
2021-05-08 20:03:15 +00:00
|
|
|
super()._fail_with_undefined_error(*args, **kwargs)
|
2014-06-06 16:14:45 +00:00
|
|
|
except self._undefined_exception as e:
|
2021-05-08 20:03:15 +00:00
|
|
|
logger.error("Template variable error: %s", e) # type: ignore
|
2014-06-06 16:14:45 +00:00
|
|
|
raise e
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __str__(self) -> str:
|
2014-06-06 16:14:45 +00:00
|
|
|
_log_message(self)
|
2021-05-08 20:03:15 +00:00
|
|
|
return super().__str__() # type: ignore
|
2014-06-06 16:14:45 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __iter__(self) -> t.Iterator[t.Any]:
|
2014-06-06 16:14:45 +00:00
|
|
|
_log_message(self)
|
2021-05-08 20:03:15 +00:00
|
|
|
return super().__iter__() # type: ignore
|
2014-06-06 16:14:45 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __bool__(self) -> bool:
|
2020-01-28 06:05:00 +00:00
|
|
|
_log_message(self)
|
2021-05-08 20:03:15 +00:00
|
|
|
return super().__bool__() # type: ignore
|
2014-06-06 16:00:04 +00:00
|
|
|
|
|
|
|
return LoggingUndefined
|
|
|
|
|
|
|
|
|
2019-05-08 14:47:33 +00:00
|
|
|
class ChainableUndefined(Undefined):
|
2019-08-10 21:08:39 +00:00
|
|
|
"""An undefined that is chainable, where both ``__getattr__`` and
|
|
|
|
``__getitem__`` return itself rather than raising an
|
|
|
|
:exc:`UndefinedError`.
|
2019-05-08 14:47:33 +00:00
|
|
|
|
|
|
|
>>> foo = ChainableUndefined(name='foo')
|
|
|
|
>>> str(foo.bar['baz'])
|
|
|
|
''
|
|
|
|
>>> foo.bar['baz'] + 42
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-01-27 05:12:52 +00:00
|
|
|
jinja2.exceptions.UndefinedError: 'foo' is undefined
|
2019-05-08 14:47:33 +00:00
|
|
|
|
2019-08-10 21:08:39 +00:00
|
|
|
.. versionadded:: 2.11.0
|
2019-05-08 14:47:33 +00:00
|
|
|
"""
|
2020-01-10 15:46:18 +00:00
|
|
|
|
2019-05-08 14:47:33 +00:00
|
|
|
__slots__ = ()
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __html__(self) -> str:
|
|
|
|
return str(self)
|
2019-08-10 21:08:39 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __getattr__(self, _: str) -> "ChainableUndefined":
|
2019-05-08 14:47:33 +00:00
|
|
|
return self
|
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
__getitem__ = __getattr__ # type: ignore
|
2019-05-08 14:47:33 +00:00
|
|
|
|
|
|
|
|
2008-04-14 20:53:58 +00:00
|
|
|
class DebugUndefined(Undefined):
|
2008-04-28 10:20:12 +00:00
|
|
|
"""An undefined that returns the debug info when printed.
|
|
|
|
|
|
|
|
>>> foo = DebugUndefined(name='foo')
|
|
|
|
>>> str(foo)
|
|
|
|
'{{ foo }}'
|
|
|
|
>>> not foo
|
|
|
|
True
|
|
|
|
>>> foo + 42
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-01-27 05:12:52 +00:00
|
|
|
jinja2.exceptions.UndefinedError: 'foo' is undefined
|
2008-04-28 10:20:12 +00:00
|
|
|
"""
|
2020-01-10 15:46:18 +00:00
|
|
|
|
2008-04-26 16:30:19 +00:00
|
|
|
__slots__ = ()
|
2008-04-14 20:53:58 +00:00
|
|
|
|
2021-05-08 20:03:15 +00:00
|
|
|
def __str__(self) -> str:
|
2020-01-29 04:16:59 +00:00
|
|
|
if self._undefined_hint:
|
|
|
|
message = f"undefined value printed: {self._undefined_hint}"
|
|
|
|
|
|
|
|
elif self._undefined_obj is missing:
|
2021-05-08 20:03:15 +00:00
|
|
|
message = self._undefined_name # type: ignore
|
2020-01-29 04:16:59 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
message = (
|
|
|
|
f"no such element: {object_type_repr(self._undefined_obj)}"
|
|
|
|
f"[{self._undefined_name!r}]"
|
2008-04-17 16:44:07 +00:00
|
|
|
)
|
2020-01-29 04:16:59 +00:00
|
|
|
|
|
|
|
return f"{{{{ {message} }}}}"
|
2008-04-14 20:53:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StrictUndefined(Undefined):
|
2008-04-24 19:54:44 +00:00
|
|
|
"""An undefined that barks on print and iteration as well as boolean
|
2008-04-26 16:30:19 +00:00
|
|
|
tests and all kinds of comparisons. In other words: you can do nothing
|
|
|
|
with it except checking if it's defined using the `defined` test.
|
2008-04-28 10:20:12 +00:00
|
|
|
|
|
|
|
>>> foo = StrictUndefined(name='foo')
|
|
|
|
>>> str(foo)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-01-27 05:12:52 +00:00
|
|
|
jinja2.exceptions.UndefinedError: 'foo' is undefined
|
2008-04-28 10:20:12 +00:00
|
|
|
>>> not foo
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-01-27 05:12:52 +00:00
|
|
|
jinja2.exceptions.UndefinedError: 'foo' is undefined
|
2008-04-28 10:20:12 +00:00
|
|
|
>>> foo + 42
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2020-01-27 05:12:52 +00:00
|
|
|
jinja2.exceptions.UndefinedError: 'foo' is undefined
|
2008-04-17 17:04:44 +00:00
|
|
|
"""
|
2020-01-10 15:46:18 +00:00
|
|
|
|
2008-04-26 16:30:19 +00:00
|
|
|
__slots__ = ()
|
2020-01-28 06:05:00 +00:00
|
|
|
__iter__ = __str__ = __len__ = Undefined._fail_with_undefined_error
|
|
|
|
__eq__ = __ne__ = __bool__ = __hash__ = Undefined._fail_with_undefined_error
|
2021-05-26 09:54:58 +00:00
|
|
|
__contains__ = Undefined._fail_with_undefined_error
|
2008-04-26 16:30:19 +00:00
|
|
|
|
2008-04-14 20:53:58 +00:00
|
|
|
|
2020-01-28 06:05:00 +00:00
|
|
|
# Remove slots attributes, after the metaclass is applied they are
|
|
|
|
# unneeded and contain wrong data for subclasses.
|
2020-01-10 15:46:18 +00:00
|
|
|
del (
|
|
|
|
Undefined.__slots__,
|
|
|
|
ChainableUndefined.__slots__,
|
|
|
|
DebugUndefined.__slots__,
|
|
|
|
StrictUndefined.__slots__,
|
|
|
|
)
|