Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/jinja2/tests.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
| author | bcclaywell |
|---|---|
| date | Mon, 12 Oct 2015 17:43:33 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d67268158946 |
|---|---|
| 1 # -*- coding: utf-8 -*- | |
| 2 """ | |
| 3 jinja2.tests | |
| 4 ~~~~~~~~~~~~ | |
| 5 | |
| 6 Jinja test functions. Used with the "is" operator. | |
| 7 | |
| 8 :copyright: (c) 2010 by the Jinja Team. | |
| 9 :license: BSD, see LICENSE for more details. | |
| 10 """ | |
| 11 import re | |
| 12 from collections import Mapping | |
| 13 from jinja2.runtime import Undefined | |
| 14 from jinja2._compat import text_type, string_types, integer_types | |
| 15 import decimal | |
| 16 | |
| 17 number_re = re.compile(r'^-?\d+(\.\d+)?$') | |
| 18 regex_type = type(number_re) | |
| 19 | |
| 20 | |
| 21 test_callable = callable | |
| 22 | |
| 23 | |
| 24 def test_odd(value): | |
| 25 """Return true if the variable is odd.""" | |
| 26 return value % 2 == 1 | |
| 27 | |
| 28 | |
| 29 def test_even(value): | |
| 30 """Return true if the variable is even.""" | |
| 31 return value % 2 == 0 | |
| 32 | |
| 33 | |
| 34 def test_divisibleby(value, num): | |
| 35 """Check if a variable is divisible by a number.""" | |
| 36 return value % num == 0 | |
| 37 | |
| 38 | |
| 39 def test_defined(value): | |
| 40 """Return true if the variable is defined: | |
| 41 | |
| 42 .. sourcecode:: jinja | |
| 43 | |
| 44 {% if variable is defined %} | |
| 45 value of variable: {{ variable }} | |
| 46 {% else %} | |
| 47 variable is not defined | |
| 48 {% endif %} | |
| 49 | |
| 50 See the :func:`default` filter for a simple way to set undefined | |
| 51 variables. | |
| 52 """ | |
| 53 return not isinstance(value, Undefined) | |
| 54 | |
| 55 | |
| 56 def test_undefined(value): | |
| 57 """Like :func:`defined` but the other way round.""" | |
| 58 return isinstance(value, Undefined) | |
| 59 | |
| 60 | |
| 61 def test_none(value): | |
| 62 """Return true if the variable is none.""" | |
| 63 return value is None | |
| 64 | |
| 65 | |
| 66 def test_lower(value): | |
| 67 """Return true if the variable is lowercased.""" | |
| 68 return text_type(value).islower() | |
| 69 | |
| 70 | |
| 71 def test_upper(value): | |
| 72 """Return true if the variable is uppercased.""" | |
| 73 return text_type(value).isupper() | |
| 74 | |
| 75 | |
| 76 def test_string(value): | |
| 77 """Return true if the object is a string.""" | |
| 78 return isinstance(value, string_types) | |
| 79 | |
| 80 | |
| 81 def test_mapping(value): | |
| 82 """Return true if the object is a mapping (dict etc.). | |
| 83 | |
| 84 .. versionadded:: 2.6 | |
| 85 """ | |
| 86 return isinstance(value, Mapping) | |
| 87 | |
| 88 | |
| 89 def test_number(value): | |
| 90 """Return true if the variable is a number.""" | |
| 91 return isinstance(value, integer_types + (float, complex, decimal.Decimal)) | |
| 92 | |
| 93 | |
| 94 def test_sequence(value): | |
| 95 """Return true if the variable is a sequence. Sequences are variables | |
| 96 that are iterable. | |
| 97 """ | |
| 98 try: | |
| 99 len(value) | |
| 100 value.__getitem__ | |
| 101 except: | |
| 102 return False | |
| 103 return True | |
| 104 | |
| 105 | |
| 106 def test_equalto(value, other): | |
| 107 """Check if an object has the same value as another object: | |
| 108 | |
| 109 .. sourcecode:: jinja | |
| 110 | |
| 111 {% if foo.expression is equalto 42 %} | |
| 112 the foo attribute evaluates to the constant 42 | |
| 113 {% endif %} | |
| 114 | |
| 115 This appears to be a useless test as it does exactly the same as the | |
| 116 ``==`` operator, but it can be useful when used together with the | |
| 117 `selectattr` function: | |
| 118 | |
| 119 .. sourcecode:: jinja | |
| 120 | |
| 121 {{ users|selectattr("email", "equalto", "foo@bar.invalid") }} | |
| 122 | |
| 123 .. versionadded:: 2.8 | |
| 124 """ | |
| 125 return value == other | |
| 126 | |
| 127 | |
| 128 def test_sameas(value, other): | |
| 129 """Check if an object points to the same memory address than another | |
| 130 object: | |
| 131 | |
| 132 .. sourcecode:: jinja | |
| 133 | |
| 134 {% if foo.attribute is sameas false %} | |
| 135 the foo attribute really is the `False` singleton | |
| 136 {% endif %} | |
| 137 """ | |
| 138 return value is other | |
| 139 | |
| 140 | |
| 141 def test_iterable(value): | |
| 142 """Check if it's possible to iterate over an object.""" | |
| 143 try: | |
| 144 iter(value) | |
| 145 except TypeError: | |
| 146 return False | |
| 147 return True | |
| 148 | |
| 149 | |
| 150 def test_escaped(value): | |
| 151 """Check if the value is escaped.""" | |
| 152 return hasattr(value, '__html__') | |
| 153 | |
| 154 | |
| 155 TESTS = { | |
| 156 'odd': test_odd, | |
| 157 'even': test_even, | |
| 158 'divisibleby': test_divisibleby, | |
| 159 'defined': test_defined, | |
| 160 'undefined': test_undefined, | |
| 161 'none': test_none, | |
| 162 'lower': test_lower, | |
| 163 'upper': test_upper, | |
| 164 'string': test_string, | |
| 165 'mapping': test_mapping, | |
| 166 'number': test_number, | |
| 167 'sequence': test_sequence, | |
| 168 'iterable': test_iterable, | |
| 169 'callable': test_callable, | |
| 170 'sameas': test_sameas, | |
| 171 'equalto': test_equalto, | |
| 172 'escaped': test_escaped | |
| 173 } |
