PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/rql_test/connections/http_support/jinja2/filters.py

https://gitlab.com/Mashamba/rethinkdb
Python | 987 lines | 908 code | 28 blank | 51 comment | 25 complexity | d13aa39b6aeadbb31ac857b8c4e3f357 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.filters
  4. ~~~~~~~~~~~~~~
  5. Bundled jinja filters.
  6. :copyright: (c) 2010 by the Jinja Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import re
  10. import math
  11. from random import choice
  12. from operator import itemgetter
  13. from itertools import groupby
  14. from jinja2.utils import Markup, escape, pformat, urlize, soft_unicode, \
  15. unicode_urlencode
  16. from jinja2.runtime import Undefined
  17. from jinja2.exceptions import FilterArgumentError
  18. from jinja2._compat import next, imap, string_types, text_type, iteritems
  19. _word_re = re.compile(r'\w+(?u)')
  20. def contextfilter(f):
  21. """Decorator for marking context dependent filters. The current
  22. :class:`Context` will be passed as first argument.
  23. """
  24. f.contextfilter = True
  25. return f
  26. def evalcontextfilter(f):
  27. """Decorator for marking eval-context dependent filters. An eval
  28. context object is passed as first argument. For more information
  29. about the eval context, see :ref:`eval-context`.
  30. .. versionadded:: 2.4
  31. """
  32. f.evalcontextfilter = True
  33. return f
  34. def environmentfilter(f):
  35. """Decorator for marking evironment dependent filters. The current
  36. :class:`Environment` is passed to the filter as first argument.
  37. """
  38. f.environmentfilter = True
  39. return f
  40. def make_attrgetter(environment, attribute):
  41. """Returns a callable that looks up the given attribute from a
  42. passed object with the rules of the environment. Dots are allowed
  43. to access attributes of attributes. Integer parts in paths are
  44. looked up as integers.
  45. """
  46. if not isinstance(attribute, string_types) \
  47. or ('.' not in attribute and not attribute.isdigit()):
  48. return lambda x: environment.getitem(x, attribute)
  49. attribute = attribute.split('.')
  50. def attrgetter(item):
  51. for part in attribute:
  52. if part.isdigit():
  53. part = int(part)
  54. item = environment.getitem(item, part)
  55. return item
  56. return attrgetter
  57. def do_forceescape(value):
  58. """Enforce HTML escaping. This will probably double escape variables."""
  59. if hasattr(value, '__html__'):
  60. value = value.__html__()
  61. return escape(text_type(value))
  62. def do_urlencode(value):
  63. """Escape strings for use in URLs (uses UTF-8 encoding). It accepts both
  64. dictionaries and regular strings as well as pairwise iterables.
  65. .. versionadded:: 2.7
  66. """
  67. itemiter = None
  68. if isinstance(value, dict):
  69. itemiter = iteritems(value)
  70. elif not isinstance(value, string_types):
  71. try:
  72. itemiter = iter(value)
  73. except TypeError:
  74. pass
  75. if itemiter is None:
  76. return unicode_urlencode(value)
  77. return u'&'.join(unicode_urlencode(k) + '=' +
  78. unicode_urlencode(v) for k, v in itemiter)
  79. @evalcontextfilter
  80. def do_replace(eval_ctx, s, old, new, count=None):
  81. """Return a copy of the value with all occurrences of a substring
  82. replaced with a new one. The first argument is the substring
  83. that should be replaced, the second is the replacement string.
  84. If the optional third argument ``count`` is given, only the first
  85. ``count`` occurrences are replaced:
  86. .. sourcecode:: jinja
  87. {{ "Hello World"|replace("Hello", "Goodbye") }}
  88. -> Goodbye World
  89. {{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
  90. -> d'oh, d'oh, aaargh
  91. """
  92. if count is None:
  93. count = -1
  94. if not eval_ctx.autoescape:
  95. return text_type(s).replace(text_type(old), text_type(new), count)
  96. if hasattr(old, '__html__') or hasattr(new, '__html__') and \
  97. not hasattr(s, '__html__'):
  98. s = escape(s)
  99. else:
  100. s = soft_unicode(s)
  101. return s.replace(soft_unicode(old), soft_unicode(new), count)
  102. def do_upper(s):
  103. """Convert a value to uppercase."""
  104. return soft_unicode(s).upper()
  105. def do_lower(s):
  106. """Convert a value to lowercase."""
  107. return soft_unicode(s).lower()
  108. @evalcontextfilter
  109. def do_xmlattr(_eval_ctx, d, autospace=True):
  110. """Create an SGML/XML attribute string based on the items in a dict.
  111. All values that are neither `none` nor `undefined` are automatically
  112. escaped:
  113. .. sourcecode:: html+jinja
  114. <ul{{ {'class': 'my_list', 'missing': none,
  115. 'id': 'list-%d'|format(variable)}|xmlattr }}>
  116. ...
  117. </ul>
  118. Results in something like this:
  119. .. sourcecode:: html
  120. <ul class="my_list" id="list-42">
  121. ...
  122. </ul>
  123. As you can see it automatically prepends a space in front of the item
  124. if the filter returned something unless the second parameter is false.
  125. """
  126. rv = u' '.join(
  127. u'%s="%s"' % (escape(key), escape(value))
  128. for key, value in iteritems(d)
  129. if value is not None and not isinstance(value, Undefined)
  130. )
  131. if autospace and rv:
  132. rv = u' ' + rv
  133. if _eval_ctx.autoescape:
  134. rv = Markup(rv)
  135. return rv
  136. def do_capitalize(s):
  137. """Capitalize a value. The first character will be uppercase, all others
  138. lowercase.
  139. """
  140. return soft_unicode(s).capitalize()
  141. def do_title(s):
  142. """Return a titlecased version of the value. I.e. words will start with
  143. uppercase letters, all remaining characters are lowercase.
  144. """
  145. rv = []
  146. for item in re.compile(r'([-\s]+)(?u)').split(s):
  147. if not item:
  148. continue
  149. rv.append(item[0].upper() + item[1:].lower())
  150. return ''.join(rv)
  151. def do_dictsort(value, case_sensitive=False, by='key'):
  152. """Sort a dict and yield (key, value) pairs. Because python dicts are
  153. unsorted you may want to use this function to order them by either
  154. key or value:
  155. .. sourcecode:: jinja
  156. {% for item in mydict|dictsort %}
  157. sort the dict by key, case insensitive
  158. {% for item in mydict|dictsort(true) %}
  159. sort the dict by key, case sensitive
  160. {% for item in mydict|dictsort(false, 'value') %}
  161. sort the dict by key, case insensitive, sorted
  162. normally and ordered by value.
  163. """
  164. if by == 'key':
  165. pos = 0
  166. elif by == 'value':
  167. pos = 1
  168. else:
  169. raise FilterArgumentError('You can only sort by either '
  170. '"key" or "value"')
  171. def sort_func(item):
  172. value = item[pos]
  173. if isinstance(value, string_types) and not case_sensitive:
  174. value = value.lower()
  175. return value
  176. return sorted(value.items(), key=sort_func)
  177. @environmentfilter
  178. def do_sort(environment, value, reverse=False, case_sensitive=False,
  179. attribute=None):
  180. """Sort an iterable. Per default it sorts ascending, if you pass it
  181. true as first argument it will reverse the sorting.
  182. If the iterable is made of strings the third parameter can be used to
  183. control the case sensitiveness of the comparison which is disabled by
  184. default.
  185. .. sourcecode:: jinja
  186. {% for item in iterable|sort %}
  187. ...
  188. {% endfor %}
  189. It is also possible to sort by an attribute (for example to sort
  190. by the date of an object) by specifying the `attribute` parameter:
  191. .. sourcecode:: jinja
  192. {% for item in iterable|sort(attribute='date') %}
  193. ...
  194. {% endfor %}
  195. .. versionchanged:: 2.6
  196. The `attribute` parameter was added.
  197. """
  198. if not case_sensitive:
  199. def sort_func(item):
  200. if isinstance(item, string_types):
  201. item = item.lower()
  202. return item
  203. else:
  204. sort_func = None
  205. if attribute is not None:
  206. getter = make_attrgetter(environment, attribute)
  207. def sort_func(item, processor=sort_func or (lambda x: x)):
  208. return processor(getter(item))
  209. return sorted(value, key=sort_func, reverse=reverse)
  210. def do_default(value, default_value=u'', boolean=False):
  211. """If the value is undefined it will return the passed default value,
  212. otherwise the value of the variable:
  213. .. sourcecode:: jinja
  214. {{ my_variable|default('my_variable is not defined') }}
  215. This will output the value of ``my_variable`` if the variable was
  216. defined, otherwise ``'my_variable is not defined'``. If you want
  217. to use default with variables that evaluate to false you have to
  218. set the second parameter to `true`:
  219. .. sourcecode:: jinja
  220. {{ ''|default('the string was empty', true) }}
  221. """
  222. if isinstance(value, Undefined) or (boolean and not value):
  223. return default_value
  224. return value
  225. @evalcontextfilter
  226. def do_join(eval_ctx, value, d=u'', attribute=None):
  227. """Return a string which is the concatenation of the strings in the
  228. sequence. The separator between elements is an empty string per
  229. default, you can define it with the optional parameter:
  230. .. sourcecode:: jinja
  231. {{ [1, 2, 3]|join('|') }}
  232. -> 1|2|3
  233. {{ [1, 2, 3]|join }}
  234. -> 123
  235. It is also possible to join certain attributes of an object:
  236. .. sourcecode:: jinja
  237. {{ users|join(', ', attribute='username') }}
  238. .. versionadded:: 2.6
  239. The `attribute` parameter was added.
  240. """
  241. if attribute is not None:
  242. value = imap(make_attrgetter(eval_ctx.environment, attribute), value)
  243. # no automatic escaping? joining is a lot eaiser then
  244. if not eval_ctx.autoescape:
  245. return text_type(d).join(imap(text_type, value))
  246. # if the delimiter doesn't have an html representation we check
  247. # if any of the items has. If yes we do a coercion to Markup
  248. if not hasattr(d, '__html__'):
  249. value = list(value)
  250. do_escape = False
  251. for idx, item in enumerate(value):
  252. if hasattr(item, '__html__'):
  253. do_escape = True
  254. else:
  255. value[idx] = text_type(item)
  256. if do_escape:
  257. d = escape(d)
  258. else:
  259. d = text_type(d)
  260. return d.join(value)
  261. # no html involved, to normal joining
  262. return soft_unicode(d).join(imap(soft_unicode, value))
  263. def do_center(value, width=80):
  264. """Centers the value in a field of a given width."""
  265. return text_type(value).center(width)
  266. @environmentfilter
  267. def do_first(environment, seq):
  268. """Return the first item of a sequence."""
  269. try:
  270. return next(iter(seq))
  271. except StopIteration:
  272. return environment.undefined('No first item, sequence was empty.')
  273. @environmentfilter
  274. def do_last(environment, seq):
  275. """Return the last item of a sequence."""
  276. try:
  277. return next(iter(reversed(seq)))
  278. except StopIteration:
  279. return environment.undefined('No last item, sequence was empty.')
  280. @environmentfilter
  281. def do_random(environment, seq):
  282. """Return a random item from the sequence."""
  283. try:
  284. return choice(seq)
  285. except IndexError:
  286. return environment.undefined('No random item, sequence was empty.')
  287. def do_filesizeformat(value, binary=False):
  288. """Format the value like a 'human-readable' file size (i.e. 13 kB,
  289. 4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega,
  290. Giga, etc.), if the second parameter is set to `True` the binary
  291. prefixes are used (Mebi, Gibi).
  292. """
  293. bytes = float(value)
  294. base = binary and 1024 or 1000
  295. prefixes = [
  296. (binary and 'KiB' or 'kB'),
  297. (binary and 'MiB' or 'MB'),
  298. (binary and 'GiB' or 'GB'),
  299. (binary and 'TiB' or 'TB'),
  300. (binary and 'PiB' or 'PB'),
  301. (binary and 'EiB' or 'EB'),
  302. (binary and 'ZiB' or 'ZB'),
  303. (binary and 'YiB' or 'YB')
  304. ]
  305. if bytes == 1:
  306. return '1 Byte'
  307. elif bytes < base:
  308. return '%d Bytes' % bytes
  309. else:
  310. for i, prefix in enumerate(prefixes):
  311. unit = base ** (i + 2)
  312. if bytes < unit:
  313. return '%.1f %s' % ((base * bytes / unit), prefix)
  314. return '%.1f %s' % ((base * bytes / unit), prefix)
  315. def do_pprint(value, verbose=False):
  316. """Pretty print a variable. Useful for debugging.
  317. With Jinja 1.2 onwards you can pass it a parameter. If this parameter
  318. is truthy the output will be more verbose (this requires `pretty`)
  319. """
  320. return pformat(value, verbose=verbose)
  321. @evalcontextfilter
  322. def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False):
  323. """Converts URLs in plain text into clickable links.
  324. If you pass the filter an additional integer it will shorten the urls
  325. to that number. Also a third argument exists that makes the urls
  326. "nofollow":
  327. .. sourcecode:: jinja
  328. {{ mytext|urlize(40, true) }}
  329. links are shortened to 40 chars and defined with rel="nofollow"
  330. """
  331. rv = urlize(value, trim_url_limit, nofollow)
  332. if eval_ctx.autoescape:
  333. rv = Markup(rv)
  334. return rv
  335. def do_indent(s, width=4, indentfirst=False):
  336. """Return a copy of the passed string, each line indented by
  337. 4 spaces. The first line is not indented. If you want to
  338. change the number of spaces or indent the first line too
  339. you can pass additional parameters to the filter:
  340. .. sourcecode:: jinja
  341. {{ mytext|indent(2, true) }}
  342. indent by two spaces and indent the first line too.
  343. """
  344. indention = u' ' * width
  345. rv = (u'\n' + indention).join(s.splitlines())
  346. if indentfirst:
  347. rv = indention + rv
  348. return rv
  349. def do_truncate(s, length=255, killwords=False, end='...'):
  350. """Return a truncated copy of the string. The length is specified
  351. with the first parameter which defaults to ``255``. If the second
  352. parameter is ``true`` the filter will cut the text at length. Otherwise
  353. it will discard the last word. If the text was in fact
  354. truncated it will append an ellipsis sign (``"..."``). If you want a
  355. different ellipsis sign than ``"..."`` you can specify it using the
  356. third parameter.
  357. .. sourcecode:: jinja
  358. {{ "foo bar"|truncate(5) }}
  359. -> "foo ..."
  360. {{ "foo bar"|truncate(5, True) }}
  361. -> "foo b..."
  362. """
  363. if len(s) <= length:
  364. return s
  365. elif killwords:
  366. return s[:length] + end
  367. words = s.split(' ')
  368. result = []
  369. m = 0
  370. for word in words:
  371. m += len(word) + 1
  372. if m > length:
  373. break
  374. result.append(word)
  375. result.append(end)
  376. return u' '.join(result)
  377. @environmentfilter
  378. def do_wordwrap(environment, s, width=79, break_long_words=True,
  379. wrapstring=None):
  380. """
  381. Return a copy of the string passed to the filter wrapped after
  382. ``79`` characters. You can override this default using the first
  383. parameter. If you set the second parameter to `false` Jinja will not
  384. split words apart if they are longer than `width`. By default, the newlines
  385. will be the default newlines for the environment, but this can be changed
  386. using the wrapstring keyword argument.
  387. .. versionadded:: 2.7
  388. Added support for the `wrapstring` parameter.
  389. """
  390. if not wrapstring:
  391. wrapstring = environment.newline_sequence
  392. import textwrap
  393. return wrapstring.join(textwrap.wrap(s, width=width, expand_tabs=False,
  394. replace_whitespace=False,
  395. break_long_words=break_long_words))
  396. def do_wordcount(s):
  397. """Count the words in that string."""
  398. return len(_word_re.findall(s))
  399. def do_int(value, default=0):
  400. """Convert the value into an integer. If the
  401. conversion doesn't work it will return ``0``. You can
  402. override this default using the first parameter.
  403. """
  404. try:
  405. return int(value)
  406. except (TypeError, ValueError):
  407. # this quirk is necessary so that "42.23"|int gives 42.
  408. try:
  409. return int(float(value))
  410. except (TypeError, ValueError):
  411. return default
  412. def do_float(value, default=0.0):
  413. """Convert the value into a floating point number. If the
  414. conversion doesn't work it will return ``0.0``. You can
  415. override this default using the first parameter.
  416. """
  417. try:
  418. return float(value)
  419. except (TypeError, ValueError):
  420. return default
  421. def do_format(value, *args, **kwargs):
  422. """
  423. Apply python string formatting on an object:
  424. .. sourcecode:: jinja
  425. {{ "%s - %s"|format("Hello?", "Foo!") }}
  426. -> Hello? - Foo!
  427. """
  428. if args and kwargs:
  429. raise FilterArgumentError('can\'t handle positional and keyword '
  430. 'arguments at the same time')
  431. return soft_unicode(value) % (kwargs or args)
  432. def do_trim(value):
  433. """Strip leading and trailing whitespace."""
  434. return soft_unicode(value).strip()
  435. def do_striptags(value):
  436. """Strip SGML/XML tags and replace adjacent whitespace by one space.
  437. """
  438. if hasattr(value, '__html__'):
  439. value = value.__html__()
  440. return Markup(text_type(value)).striptags()
  441. def do_slice(value, slices, fill_with=None):
  442. """Slice an iterator and return a list of lists containing
  443. those items. Useful if you want to create a div containing
  444. three ul tags that represent columns:
  445. .. sourcecode:: html+jinja
  446. <div class="columwrapper">
  447. {%- for column in items|slice(3) %}
  448. <ul class="column-{{ loop.index }}">
  449. {%- for item in column %}
  450. <li>{{ item }}</li>
  451. {%- endfor %}
  452. </ul>
  453. {%- endfor %}
  454. </div>
  455. If you pass it a second argument it's used to fill missing
  456. values on the last iteration.
  457. """
  458. seq = list(value)
  459. length = len(seq)
  460. items_per_slice = length // slices
  461. slices_with_extra = length % slices
  462. offset = 0
  463. for slice_number in range(slices):
  464. start = offset + slice_number * items_per_slice
  465. if slice_number < slices_with_extra:
  466. offset += 1
  467. end = offset + (slice_number + 1) * items_per_slice
  468. tmp = seq[start:end]
  469. if fill_with is not None and slice_number >= slices_with_extra:
  470. tmp.append(fill_with)
  471. yield tmp
  472. def do_batch(value, linecount, fill_with=None):
  473. """
  474. A filter that batches items. It works pretty much like `slice`
  475. just the other way round. It returns a list of lists with the
  476. given number of items. If you provide a second parameter this
  477. is used to fill up missing items. See this example:
  478. .. sourcecode:: html+jinja
  479. <table>
  480. {%- for row in items|batch(3, '&nbsp;') %}
  481. <tr>
  482. {%- for column in row %}
  483. <td>{{ column }}</td>
  484. {%- endfor %}
  485. </tr>
  486. {%- endfor %}
  487. </table>
  488. """
  489. result = []
  490. tmp = []
  491. for item in value:
  492. if len(tmp) == linecount:
  493. yield tmp
  494. tmp = []
  495. tmp.append(item)
  496. if tmp:
  497. if fill_with is not None and len(tmp) < linecount:
  498. tmp += [fill_with] * (linecount - len(tmp))
  499. yield tmp
  500. def do_round(value, precision=0, method='common'):
  501. """Round the number to a given precision. The first
  502. parameter specifies the precision (default is ``0``), the
  503. second the rounding method:
  504. - ``'common'`` rounds either up or down
  505. - ``'ceil'`` always rounds up
  506. - ``'floor'`` always rounds down
  507. If you don't specify a method ``'common'`` is used.
  508. .. sourcecode:: jinja
  509. {{ 42.55|round }}
  510. -> 43.0
  511. {{ 42.55|round(1, 'floor') }}
  512. -> 42.5
  513. Note that even if rounded to 0 precision, a float is returned. If
  514. you need a real integer, pipe it through `int`:
  515. .. sourcecode:: jinja
  516. {{ 42.55|round|int }}
  517. -> 43
  518. """
  519. if not method in ('common', 'ceil', 'floor'):
  520. raise FilterArgumentError('method must be common, ceil or floor')
  521. if method == 'common':
  522. return round(value, precision)
  523. func = getattr(math, method)
  524. return func(value * (10 ** precision)) / (10 ** precision)
  525. @environmentfilter
  526. def do_groupby(environment, value, attribute):
  527. """Group a sequence of objects by a common attribute.
  528. If you for example have a list of dicts or objects that represent persons
  529. with `gender`, `first_name` and `last_name` attributes and you want to
  530. group all users by genders you can do something like the following
  531. snippet:
  532. .. sourcecode:: html+jinja
  533. <ul>
  534. {% for group in persons|groupby('gender') %}
  535. <li>{{ group.grouper }}<ul>
  536. {% for person in group.list %}
  537. <li>{{ person.first_name }} {{ person.last_name }}</li>
  538. {% endfor %}</ul></li>
  539. {% endfor %}
  540. </ul>
  541. Additionally it's possible to use tuple unpacking for the grouper and
  542. list:
  543. .. sourcecode:: html+jinja
  544. <ul>
  545. {% for grouper, list in persons|groupby('gender') %}
  546. ...
  547. {% endfor %}
  548. </ul>
  549. As you can see the item we're grouping by is stored in the `grouper`
  550. attribute and the `list` contains all the objects that have this grouper
  551. in common.
  552. .. versionchanged:: 2.6
  553. It's now possible to use dotted notation to group by the child
  554. attribute of another attribute.
  555. """
  556. expr = make_attrgetter(environment, attribute)
  557. return sorted(map(_GroupTuple, groupby(sorted(value, key=expr), expr)))
  558. class _GroupTuple(tuple):
  559. __slots__ = ()
  560. grouper = property(itemgetter(0))
  561. list = property(itemgetter(1))
  562. def __new__(cls, xxx_todo_changeme):
  563. (key, value) = xxx_todo_changeme
  564. return tuple.__new__(cls, (key, list(value)))
  565. @environmentfilter
  566. def do_sum(environment, iterable, attribute=None, start=0):
  567. """Returns the sum of a sequence of numbers plus the value of parameter
  568. 'start' (which defaults to 0). When the sequence is empty it returns
  569. start.
  570. It is also possible to sum up only certain attributes:
  571. .. sourcecode:: jinja
  572. Total: {{ items|sum(attribute='price') }}
  573. .. versionchanged:: 2.6
  574. The `attribute` parameter was added to allow suming up over
  575. attributes. Also the `start` parameter was moved on to the right.
  576. """
  577. if attribute is not None:
  578. iterable = imap(make_attrgetter(environment, attribute), iterable)
  579. return sum(iterable, start)
  580. def do_list(value):
  581. """Convert the value into a list. If it was a string the returned list
  582. will be a list of characters.
  583. """
  584. return list(value)
  585. def do_mark_safe(value):
  586. """Mark the value as safe which means that in an environment with automatic
  587. escaping enabled this variable will not be escaped.
  588. """
  589. return Markup(value)
  590. def do_mark_unsafe(value):
  591. """Mark a value as unsafe. This is the reverse operation for :func:`safe`."""
  592. return text_type(value)
  593. def do_reverse(value):
  594. """Reverse the object or return an iterator the iterates over it the other
  595. way round.
  596. """
  597. if isinstance(value, string_types):
  598. return value[::-1]
  599. try:
  600. return reversed(value)
  601. except TypeError:
  602. try:
  603. rv = list(value)
  604. rv.reverse()
  605. return rv
  606. except TypeError:
  607. raise FilterArgumentError('argument must be iterable')
  608. @environmentfilter
  609. def do_attr(environment, obj, name):
  610. """Get an attribute of an object. ``foo|attr("bar")`` works like
  611. ``foo["bar"]`` just that always an attribute is returned and items are not
  612. looked up.
  613. See :ref:`Notes on subscriptions <notes-on-subscriptions>` for more details.
  614. """
  615. try:
  616. name = str(name)
  617. except UnicodeError:
  618. pass
  619. else:
  620. try:
  621. value = getattr(obj, name)
  622. except AttributeError:
  623. pass
  624. else:
  625. if environment.sandboxed and not \
  626. environment.is_safe_attribute(obj, name, value):
  627. return environment.unsafe_undefined(obj, name)
  628. return value
  629. return environment.undefined(obj=obj, name=name)
  630. @contextfilter
  631. def do_map(*args, **kwargs):
  632. """Applies a filter on a sequence of objects or looks up an attribute.
  633. This is useful when dealing with lists of objects but you are really
  634. only interested in a certain value of it.
  635. The basic usage is mapping on an attribute. Imagine you have a list
  636. of users but you are only interested in a list of usernames:
  637. .. sourcecode:: jinja
  638. Users on this page: {{ users|map(attribute='username')|join(', ') }}
  639. Alternatively you can let it invoke a filter by passing the name of the
  640. filter and the arguments afterwards. A good example would be applying a
  641. text conversion filter on a sequence:
  642. .. sourcecode:: jinja
  643. Users on this page: {{ titles|map('lower')|join(', ') }}
  644. .. versionadded:: 2.7
  645. """
  646. context = args[0]
  647. seq = args[1]
  648. if len(args) == 2 and 'attribute' in kwargs:
  649. attribute = kwargs.pop('attribute')
  650. if kwargs:
  651. raise FilterArgumentError('Unexpected keyword argument %r' %
  652. next(iter(kwargs)))
  653. func = make_attrgetter(context.environment, attribute)
  654. else:
  655. try:
  656. name = args[2]
  657. args = args[3:]
  658. except LookupError:
  659. raise FilterArgumentError('map requires a filter argument')
  660. func = lambda item: context.environment.call_filter(
  661. name, item, args, kwargs, context=context)
  662. if seq:
  663. for item in seq:
  664. yield func(item)
  665. @contextfilter
  666. def do_select(*args, **kwargs):
  667. """Filters a sequence of objects by appying a test to either the object
  668. or the attribute and only selecting the ones with the test succeeding.
  669. Example usage:
  670. .. sourcecode:: jinja
  671. {{ numbers|select("odd") }}
  672. .. versionadded:: 2.7
  673. """
  674. return _select_or_reject(args, kwargs, lambda x: x, False)
  675. @contextfilter
  676. def do_reject(*args, **kwargs):
  677. """Filters a sequence of objects by appying a test to either the object
  678. or the attribute and rejecting the ones with the test succeeding.
  679. Example usage:
  680. .. sourcecode:: jinja
  681. {{ numbers|reject("odd") }}
  682. .. versionadded:: 2.7
  683. """
  684. return _select_or_reject(args, kwargs, lambda x: not x, False)
  685. @contextfilter
  686. def do_selectattr(*args, **kwargs):
  687. """Filters a sequence of objects by appying a test to either the object
  688. or the attribute and only selecting the ones with the test succeeding.
  689. Example usage:
  690. .. sourcecode:: jinja
  691. {{ users|selectattr("is_active") }}
  692. {{ users|selectattr("email", "none") }}
  693. .. versionadded:: 2.7
  694. """
  695. return _select_or_reject(args, kwargs, lambda x: x, True)
  696. @contextfilter
  697. def do_rejectattr(*args, **kwargs):
  698. """Filters a sequence of objects by appying a test to either the object
  699. or the attribute and rejecting the ones with the test succeeding.
  700. .. sourcecode:: jinja
  701. {{ users|rejectattr("is_active") }}
  702. {{ users|rejectattr("email", "none") }}
  703. .. versionadded:: 2.7
  704. """
  705. return _select_or_reject(args, kwargs, lambda x: not x, True)
  706. def _select_or_reject(args, kwargs, modfunc, lookup_attr):
  707. context = args[0]
  708. seq = args[1]
  709. if lookup_attr:
  710. try:
  711. attr = args[2]
  712. except LookupError:
  713. raise FilterArgumentError('Missing parameter for attribute name')
  714. transfunc = make_attrgetter(context.environment, attr)
  715. off = 1
  716. else:
  717. off = 0
  718. transfunc = lambda x: x
  719. try:
  720. name = args[2 + off]
  721. args = args[3 + off:]
  722. func = lambda item: context.environment.call_test(
  723. name, item, args, kwargs)
  724. except LookupError:
  725. func = bool
  726. if seq:
  727. for item in seq:
  728. if modfunc(func(transfunc(item))):
  729. yield item
  730. FILTERS = {
  731. 'attr': do_attr,
  732. 'replace': do_replace,
  733. 'upper': do_upper,
  734. 'lower': do_lower,
  735. 'escape': escape,
  736. 'e': escape,
  737. 'forceescape': do_forceescape,
  738. 'capitalize': do_capitalize,
  739. 'title': do_title,
  740. 'default': do_default,
  741. 'd': do_default,
  742. 'join': do_join,
  743. 'count': len,
  744. 'dictsort': do_dictsort,
  745. 'sort': do_sort,
  746. 'length': len,
  747. 'reverse': do_reverse,
  748. 'center': do_center,
  749. 'indent': do_indent,
  750. 'title': do_title,
  751. 'capitalize': do_capitalize,
  752. 'first': do_first,
  753. 'last': do_last,
  754. 'map': do_map,
  755. 'random': do_random,
  756. 'reject': do_reject,
  757. 'rejectattr': do_rejectattr,
  758. 'filesizeformat': do_filesizeformat,
  759. 'pprint': do_pprint,
  760. 'truncate': do_truncate,
  761. 'wordwrap': do_wordwrap,
  762. 'wordcount': do_wordcount,
  763. 'int': do_int,
  764. 'float': do_float,
  765. 'string': soft_unicode,
  766. 'list': do_list,
  767. 'urlize': do_urlize,
  768. 'format': do_format,
  769. 'trim': do_trim,
  770. 'striptags': do_striptags,
  771. 'select': do_select,
  772. 'selectattr': do_selectattr,
  773. 'slice': do_slice,
  774. 'batch': do_batch,
  775. 'sum': do_sum,
  776. 'abs': abs,
  777. 'round': do_round,
  778. 'groupby': do_groupby,
  779. 'safe': do_mark_safe,
  780. 'xmlattr': do_xmlattr,
  781. 'urlencode': do_urlencode
  782. }