MAJOR=4 MINOR=49 DEVNAME=tty49 $ˆl–Ðz¾”d*hŠm@·q—îå1hßÊ „hL¾ÿÉÁÇ?÷# -*- coding: utf-8 -*- """Built-in template filters used with the ``|`` operator.""" import math import random import re import warnings from collections import namedtuple from inspect import getattr_static from itertools import chain from itertools import groupby from markupsafe import escape from markupsafe import Markup from markupsafe import soft_unicode from ._compat import abc from ._compat import imap from ._compat import iteritems from ._compat import string_types from ._compat import text_type from .exceptions import FilterArgumentError from .runtime import Undefined from .utils import htmlsafe_json_dumps from .utils import pformat from .utils import unicode_urlencode from .utils import urlize _word_re = re.compile(r"\w+", re.UNICODE) _word_beginning_split_re = re.compile(r"([-\s\(\{\[\<]+)", re.UNICODE) def contextfilter(f): """Decorator for marking context dependent filters. The current :class:`Context` will be passed as first argument. """ f.contextfilter = True return f def evalcontextfilter(f): """Decorator for marking eval-context dependent filters. An eval context object is passed as first argument. For more information about the eval context, see :ref:`eval-context`. .. versionadded:: 2.4 """ f.evalcontextfilter = True return f def environmentfilter(f): """Decorator for marking environment dependent filters. The current :class:`Environment` is passed to the filter as first argument. """ f.environmentfilter = True return f def ignore_case(value): """For use as a postprocessor for :func:`make_attrgetter`. Converts strings to lowercase and returns other types as-is.""" return value.lower() if isinstance(value, string_types) else value def make_attrgetter(environment, attribute, postprocess=None, default=None): """Returns a callable that looks up the given attribute from a passed object with the rules of the environment. Dots are allowed to access attributes of attributes. Integer parts in paths are looked up as integers. """ attribute = _prepare_attribute_parts(attribute) def attrgetter(item): for part in attribute: item = environment.getitem(item, part) if default and isinstance(item, Undefined): item = default if postprocess is not None: item = postprocess(item) return item return attrgetter def make_multi_attrgetter(environment, attribute, postprocess=None): """Returns a callable that looks up the given comma separated attributes from a passed object with the rules of the environment. Dots are allowed to access attributes of each attribute. Integer parts in paths are looked up as integers. The value returned by the returned callable is a list of extracted attribute values. Examples of attribute: "attr1,attr2", "attr1.inner1.0,attr2.inner2.0", etc. """ attribute_parts = ( attribute.split(",") if isinstance(attribute, string_types) else [attribute] ) attribute = [ _prepare_attribute_parts(attribute_part) for attribute_part in attribute_parts ] def attrgetter(item): items = [None] * len(attribute) for i, attribute_part in enumerate(attribute): item_i = item for part in attribute_part: item_i = environment.getitem(item_i, part) if postprocess is not None: item_i = postprocess(item_i) items[i] = item_i return items return attrgetter def _prepare_attribute_parts(attr): if attr is None: return [] elif isinstance(attr, string_types): return [int(x) if x.isdigit() else x for x in attr.split(".")] else: return [attr] def do_forceescape(value): """Enforce HTML escaping. This will probably double escape variables.""" if hasattr(value, "__html__"): value = value.__html__() return esc