Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | jinja2.utils |
| 4 | ~~~~~~~~~~~~ |
| 5 | |
| 6 | Utility functions. |
| 7 | |
Armin Ronacher | 55494e4 | 2010-01-22 09:41:48 +0100 | [diff] [blame] | 8 | :copyright: (c) 2010 by the Jinja Team. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 9 | :license: BSD, see LICENSE for more details. |
| 10 | """ |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 11 | import re |
Benjamin Wiegand | 9682855 | 2008-05-03 22:27:29 +0200 | [diff] [blame] | 12 | import sys |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 13 | import errno |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 14 | try: |
Armin Ronacher | 1d4c638 | 2012-01-07 17:46:40 +0100 | [diff] [blame] | 15 | from urllib.parse import quote_from_bytes as url_quote |
| 16 | except ImportError: |
| 17 | from urllib import quote as url_quote |
| 18 | try: |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 19 | from thread import allocate_lock |
| 20 | except ImportError: |
| 21 | from dummy_thread import allocate_lock |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 22 | from collections import deque |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 23 | from itertools import imap |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 24 | |
| 25 | |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 26 | _word_split_re = re.compile(r'(\s+)') |
| 27 | _punctuation_re = re.compile( |
| 28 | '^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % ( |
| 29 | '|'.join(imap(re.escape, ('(', '<', '<'))), |
| 30 | '|'.join(imap(re.escape, ('.', ',', ')', '>', '\n', '>'))) |
| 31 | ) |
| 32 | ) |
| 33 | _simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$') |
Armin Ronacher | 76c280b | 2008-05-04 12:31:48 +0200 | [diff] [blame] | 34 | _striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)') |
| 35 | _entity_re = re.compile(r'&([^;]+);') |
Armin Ronacher | 9a0078d | 2008-08-13 18:24:17 +0200 | [diff] [blame] | 36 | _letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 37 | _digits = '0123456789' |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 38 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 39 | # special singleton representing missing values for the runtime |
| 40 | missing = type('MissingType', (), {'__repr__': lambda x: 'missing'})() |
| 41 | |
Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 42 | # internal code |
| 43 | internal_code = set() |
| 44 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 45 | |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 46 | # concatenate a list of strings and convert them to unicode. |
| 47 | # unfortunately there is a bug in python 2.4 and lower that causes |
| 48 | # unicode.join trash the traceback. |
Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 49 | _concat = u''.join |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 50 | try: |
| 51 | def _test_gen_bug(): |
| 52 | raise TypeError(_test_gen_bug) |
| 53 | yield None |
Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 54 | _concat(_test_gen_bug()) |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 55 | except TypeError, _error: |
Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 56 | if not _error.args or _error.args[0] is not _test_gen_bug: |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 57 | def concat(gen): |
| 58 | try: |
Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 59 | return _concat(list(gen)) |
Ian Lewis | ab014bd | 2010-10-31 20:29:28 +0900 | [diff] [blame] | 60 | except Exception: |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 61 | # this hack is needed so that the current frame |
| 62 | # does not show up in the traceback. |
| 63 | exc_type, exc_value, tb = sys.exc_info() |
| 64 | raise exc_type, exc_value, tb.tb_next |
Armin Ronacher | cda43df | 2008-05-03 17:10:05 +0200 | [diff] [blame] | 65 | else: |
| 66 | concat = _concat |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 67 | del _test_gen_bug, _error |
| 68 | |
| 69 | |
Florent Xicluna | 0ec4f76 | 2012-02-05 13:09:15 +0100 | [diff] [blame^] | 70 | # for python 2.x we create ourselves a next() function that does the |
Armin Ronacher | bd35772 | 2009-08-05 20:25:06 +0200 | [diff] [blame] | 71 | # basics without exception catching. |
| 72 | try: |
| 73 | next = next |
| 74 | except NameError: |
| 75 | def next(x): |
| 76 | return x.next() |
| 77 | |
| 78 | |
Armin Ronacher | 0d242be | 2010-02-10 01:35:13 +0100 | [diff] [blame] | 79 | # if this python version is unable to deal with unicode filenames |
| 80 | # when passed to encode we let this function encode it properly. |
| 81 | # This is used in a couple of places. As far as Jinja is concerned |
| 82 | # filenames are unicode *or* bytestrings in 2.x and unicode only in |
| 83 | # 3.x because compile cannot handle bytes |
| 84 | if sys.version_info < (3, 0): |
| 85 | def _encode_filename(filename): |
| 86 | if isinstance(filename, unicode): |
| 87 | return filename.encode('utf-8') |
| 88 | return filename |
| 89 | else: |
| 90 | def _encode_filename(filename): |
| 91 | assert filename is None or isinstance(filename, str), \ |
| 92 | 'filenames must be strings' |
| 93 | return filename |
| 94 | |
| 95 | from keyword import iskeyword as is_python_keyword |
Armin Ronacher | 9a0078d | 2008-08-13 18:24:17 +0200 | [diff] [blame] | 96 | |
| 97 | |
| 98 | # common types. These do exist in the special types module too which however |
Armin Ronacher | 0d242be | 2010-02-10 01:35:13 +0100 | [diff] [blame] | 99 | # does not exist in IronPython out of the box. Also that way we don't have |
| 100 | # to deal with implementation specific stuff here |
Armin Ronacher | 9a0078d | 2008-08-13 18:24:17 +0200 | [diff] [blame] | 101 | class _C(object): |
| 102 | def method(self): pass |
| 103 | def _func(): |
| 104 | yield None |
| 105 | FunctionType = type(_func) |
| 106 | GeneratorType = type(_func()) |
| 107 | MethodType = type(_C.method) |
| 108 | CodeType = type(_C.method.func_code) |
| 109 | try: |
| 110 | raise TypeError() |
| 111 | except TypeError: |
| 112 | _tb = sys.exc_info()[2] |
| 113 | TracebackType = type(_tb) |
| 114 | FrameType = type(_tb.tb_frame) |
| 115 | del _C, _tb, _func |
| 116 | |
| 117 | |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 118 | def contextfunction(f): |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 119 | """This decorator can be used to mark a function or method context callable. |
| 120 | A context callable is passed the active :class:`Context` as first argument when |
| 121 | called from the template. This is useful if a function wants to get access |
| 122 | to the context or functions provided on the context object. For example |
| 123 | a function that returns a sorted list of template variables the current |
| 124 | template exports could look like this:: |
| 125 | |
Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 126 | @contextfunction |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 127 | def get_exported_names(context): |
| 128 | return sorted(context.exported_vars) |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 129 | """ |
| 130 | f.contextfunction = True |
| 131 | return f |
| 132 | |
| 133 | |
Armin Ronacher | 8346bd7 | 2010-03-14 19:43:47 +0100 | [diff] [blame] | 134 | def evalcontextfunction(f): |
Florent Xicluna | 0ec4f76 | 2012-02-05 13:09:15 +0100 | [diff] [blame^] | 135 | """This decorator can be used to mark a function or method as an eval |
Armin Ronacher | 8346bd7 | 2010-03-14 19:43:47 +0100 | [diff] [blame] | 136 | context callable. This is similar to the :func:`contextfunction` |
| 137 | but instead of passing the context, an evaluation context object is |
Armin Ronacher | fe150f3 | 2010-03-15 02:42:41 +0100 | [diff] [blame] | 138 | passed. For more information about the eval context, see |
| 139 | :ref:`eval-context`. |
Armin Ronacher | 8346bd7 | 2010-03-14 19:43:47 +0100 | [diff] [blame] | 140 | |
| 141 | .. versionadded:: 2.4 |
| 142 | """ |
| 143 | f.evalcontextfunction = True |
| 144 | return f |
| 145 | |
| 146 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 147 | def environmentfunction(f): |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 148 | """This decorator can be used to mark a function or method as environment |
| 149 | callable. This decorator works exactly like the :func:`contextfunction` |
| 150 | decorator just that the first argument is the active :class:`Environment` |
| 151 | and not context. |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 152 | """ |
| 153 | f.environmentfunction = True |
| 154 | return f |
| 155 | |
| 156 | |
Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 157 | def internalcode(f): |
| 158 | """Marks the function as internally used""" |
| 159 | internal_code.add(f.func_code) |
| 160 | return f |
| 161 | |
| 162 | |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 163 | def is_undefined(obj): |
| 164 | """Check if the object passed is undefined. This does nothing more than |
| 165 | performing an instance check against :class:`Undefined` but looks nicer. |
| 166 | This can be used for custom filters or tests that want to react to |
| 167 | undefined variables. For example a custom default filter can look like |
| 168 | this:: |
| 169 | |
| 170 | def default(var, default=''): |
| 171 | if is_undefined(var): |
| 172 | return default |
| 173 | return var |
| 174 | """ |
| 175 | from jinja2.runtime import Undefined |
| 176 | return isinstance(obj, Undefined) |
| 177 | |
| 178 | |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 179 | def consume(iterable): |
| 180 | """Consumes an iterable without doing anything with it.""" |
| 181 | for event in iterable: |
| 182 | pass |
| 183 | |
| 184 | |
Armin Ronacher | 187bde1 | 2008-05-01 18:19:16 +0200 | [diff] [blame] | 185 | def clear_caches(): |
| 186 | """Jinja2 keeps internal caches for environments and lexers. These are |
| 187 | used so that Jinja2 doesn't have to recreate environments and lexers all |
| 188 | the time. Normally you don't have to care about that but if you are |
| 189 | messuring memory consumption you may want to clean the caches. |
| 190 | """ |
| 191 | from jinja2.environment import _spontaneous_environments |
| 192 | from jinja2.lexer import _lexer_cache |
| 193 | _spontaneous_environments.clear() |
| 194 | _lexer_cache.clear() |
| 195 | |
| 196 | |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 197 | def import_string(import_name, silent=False): |
Florent Xicluna | 0ec4f76 | 2012-02-05 13:09:15 +0100 | [diff] [blame^] | 198 | """Imports an object based on a string. This is useful if you want to |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 199 | use import paths as endpoints or something similar. An import path can |
| 200 | be specified either in dotted notation (``xml.sax.saxutils.escape``) |
| 201 | or with a colon as object delimiter (``xml.sax.saxutils:escape``). |
| 202 | |
| 203 | If the `silent` is True the return value will be `None` if the import |
| 204 | fails. |
| 205 | |
| 206 | :return: imported object |
Armin Ronacher | 9a027f4 | 2008-04-17 11:13:40 +0200 | [diff] [blame] | 207 | """ |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 208 | try: |
| 209 | if ':' in import_name: |
| 210 | module, obj = import_name.split(':', 1) |
| 211 | elif '.' in import_name: |
| 212 | items = import_name.split('.') |
| 213 | module = '.'.join(items[:-1]) |
| 214 | obj = items[-1] |
| 215 | else: |
| 216 | return __import__(import_name) |
| 217 | return getattr(__import__(module, None, None, [obj]), obj) |
| 218 | except (ImportError, AttributeError): |
| 219 | if not silent: |
| 220 | raise |
Armin Ronacher | 9a027f4 | 2008-04-17 11:13:40 +0200 | [diff] [blame] | 221 | |
| 222 | |
Armin Ronacher | 0faa861 | 2010-02-09 15:04:51 +0100 | [diff] [blame] | 223 | def open_if_exists(filename, mode='rb'): |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 224 | """Returns a file descriptor for the filename if that file exists, |
| 225 | otherwise `None`. |
| 226 | """ |
| 227 | try: |
Armin Ronacher | 790b8a8 | 2010-02-10 00:05:46 +0100 | [diff] [blame] | 228 | return open(filename, mode) |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 229 | except IOError, e: |
| 230 | if e.errno not in (errno.ENOENT, errno.EISDIR): |
| 231 | raise |
| 232 | |
| 233 | |
Armin Ronacher | 98dbf5f | 2010-04-12 15:49:59 +0200 | [diff] [blame] | 234 | def object_type_repr(obj): |
| 235 | """Returns the name of the object's type. For some recognized |
| 236 | singletons the name of the object is returned instead. (For |
| 237 | example for `None` and `Ellipsis`). |
| 238 | """ |
| 239 | if obj is None: |
| 240 | return 'None' |
| 241 | elif obj is Ellipsis: |
| 242 | return 'Ellipsis' |
Armin Ronacher | 802f472 | 2010-04-20 19:48:46 +0200 | [diff] [blame] | 243 | # __builtin__ in 2.x, builtins in 3.x |
| 244 | if obj.__class__.__module__ in ('__builtin__', 'builtins'): |
Armin Ronacher | 800ac7f | 2010-04-20 13:45:11 +0200 | [diff] [blame] | 245 | name = obj.__class__.__name__ |
Armin Ronacher | 98dbf5f | 2010-04-12 15:49:59 +0200 | [diff] [blame] | 246 | else: |
Armin Ronacher | 800ac7f | 2010-04-20 13:45:11 +0200 | [diff] [blame] | 247 | name = obj.__class__.__module__ + '.' + obj.__class__.__name__ |
Armin Ronacher | 98dbf5f | 2010-04-12 15:49:59 +0200 | [diff] [blame] | 248 | return '%s object' % name |
| 249 | |
| 250 | |
Christoph Hack | e9e43bb | 2008-04-13 23:35:48 +0200 | [diff] [blame] | 251 | def pformat(obj, verbose=False): |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 252 | """Prettyprint an object. Either use the `pretty` library or the |
Christoph Hack | e9e43bb | 2008-04-13 23:35:48 +0200 | [diff] [blame] | 253 | builtin `pprint`. |
| 254 | """ |
| 255 | try: |
| 256 | from pretty import pretty |
| 257 | return pretty(obj, verbose=verbose) |
| 258 | except ImportError: |
| 259 | from pprint import pformat |
| 260 | return pformat(obj) |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 261 | |
| 262 | |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 263 | def urlize(text, trim_url_limit=None, nofollow=False): |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 264 | """Converts any URLs in text into clickable links. Works on http://, |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 265 | https:// and www. links. Links can have trailing punctuation (periods, |
| 266 | commas, close-parens) and leading punctuation (opening parens) and |
| 267 | it'll still do the right thing. |
| 268 | |
| 269 | If trim_url_limit is not None, the URLs in link text will be limited |
| 270 | to trim_url_limit characters. |
| 271 | |
| 272 | If nofollow is True, the URLs in link text will get a rel="nofollow" |
| 273 | attribute. |
| 274 | """ |
| 275 | trim_url = lambda x, limit=trim_url_limit: limit is not None \ |
| 276 | and (x[:limit] + (len(x) >=limit and '...' |
| 277 | or '')) or x |
Armin Ronacher | d9342dc | 2008-11-17 00:35:30 +0100 | [diff] [blame] | 278 | words = _word_split_re.split(unicode(escape(text))) |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 279 | nofollow_attr = nofollow and ' rel="nofollow"' or '' |
| 280 | for i, word in enumerate(words): |
| 281 | match = _punctuation_re.match(word) |
| 282 | if match: |
| 283 | lead, middle, trail = match.groups() |
| 284 | if middle.startswith('www.') or ( |
| 285 | '@' not in middle and |
| 286 | not middle.startswith('http://') and |
| 287 | len(middle) > 0 and |
Armin Ronacher | 9a0078d | 2008-08-13 18:24:17 +0200 | [diff] [blame] | 288 | middle[0] in _letters + _digits and ( |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 289 | middle.endswith('.org') or |
| 290 | middle.endswith('.net') or |
| 291 | middle.endswith('.com') |
| 292 | )): |
| 293 | middle = '<a href="http://%s"%s>%s</a>' % (middle, |
| 294 | nofollow_attr, trim_url(middle)) |
| 295 | if middle.startswith('http://') or \ |
| 296 | middle.startswith('https://'): |
| 297 | middle = '<a href="%s"%s>%s</a>' % (middle, |
| 298 | nofollow_attr, trim_url(middle)) |
| 299 | if '@' in middle and not middle.startswith('www.') and \ |
| 300 | not ':' in middle and _simple_email_re.match(middle): |
| 301 | middle = '<a href="mailto:%s">%s</a>' % (middle, middle) |
| 302 | if lead + middle + trail != word: |
| 303 | words[i] = lead + middle + trail |
| 304 | return u''.join(words) |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 305 | |
| 306 | |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 307 | def generate_lorem_ipsum(n=5, html=True, min=20, max=100): |
| 308 | """Generate some lorem impsum for the template.""" |
| 309 | from jinja2.constants import LOREM_IPSUM_WORDS |
Georg Brandl | 95632c4 | 2009-11-22 18:35:18 +0100 | [diff] [blame] | 310 | from random import choice, randrange |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 311 | words = LOREM_IPSUM_WORDS.split() |
| 312 | result = [] |
| 313 | |
| 314 | for _ in xrange(n): |
| 315 | next_capitalized = True |
| 316 | last_comma = last_fullstop = 0 |
| 317 | word = None |
| 318 | last = None |
| 319 | p = [] |
| 320 | |
| 321 | # each paragraph contains out of 20 to 100 words. |
| 322 | for idx, _ in enumerate(xrange(randrange(min, max))): |
| 323 | while True: |
| 324 | word = choice(words) |
| 325 | if word != last: |
| 326 | last = word |
| 327 | break |
| 328 | if next_capitalized: |
| 329 | word = word.capitalize() |
| 330 | next_capitalized = False |
| 331 | # add commas |
| 332 | if idx - randrange(3, 8) > last_comma: |
| 333 | last_comma = idx |
| 334 | last_fullstop += 2 |
| 335 | word += ',' |
| 336 | # add end of sentences |
| 337 | if idx - randrange(10, 20) > last_fullstop: |
| 338 | last_comma = last_fullstop = idx |
| 339 | word += '.' |
| 340 | next_capitalized = True |
| 341 | p.append(word) |
| 342 | |
| 343 | # ensure that the paragraph ends with a dot. |
| 344 | p = u' '.join(p) |
| 345 | if p.endswith(','): |
| 346 | p = p[:-1] + '.' |
| 347 | elif not p.endswith('.'): |
| 348 | p += '.' |
| 349 | result.append(p) |
| 350 | |
| 351 | if not html: |
| 352 | return u'\n\n'.join(result) |
| 353 | return Markup(u'\n'.join(u'<p>%s</p>' % escape(x) for x in result)) |
| 354 | |
| 355 | |
Armin Ronacher | 5145401 | 2012-01-07 17:47:56 +0100 | [diff] [blame] | 356 | def unicode_urlencode(obj, charset='utf-8'): |
Armin Ronacher | 1d4c638 | 2012-01-07 17:46:40 +0100 | [diff] [blame] | 357 | """URL escapes a single bytestring or unicode string with the |
| 358 | given charset if applicable to URL safe quoting under all rules |
| 359 | that need to be considered under all supported Python versions. |
| 360 | |
| 361 | If non strings are provided they are converted to their unicode |
| 362 | representation first. |
| 363 | """ |
| 364 | if not isinstance(obj, basestring): |
| 365 | obj = unicode(obj) |
| 366 | if isinstance(obj, unicode): |
| 367 | obj = obj.encode(charset) |
| 368 | return unicode(url_quote(obj)) |
| 369 | |
| 370 | |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 371 | class LRUCache(object): |
| 372 | """A simple LRU Cache implementation.""" |
Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame] | 373 | |
| 374 | # this is fast for small capacities (something below 1000) but doesn't |
| 375 | # scale. But as long as it's only used as storage for templates this |
| 376 | # won't do any harm. |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 377 | |
| 378 | def __init__(self, capacity): |
| 379 | self.capacity = capacity |
| 380 | self._mapping = {} |
| 381 | self._queue = deque() |
Armin Ronacher | 7962ce7 | 2008-05-20 17:52:52 +0200 | [diff] [blame] | 382 | self._postinit() |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 383 | |
Armin Ronacher | 7962ce7 | 2008-05-20 17:52:52 +0200 | [diff] [blame] | 384 | def _postinit(self): |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 385 | # alias all queue methods for faster lookup |
| 386 | self._popleft = self._queue.popleft |
| 387 | self._pop = self._queue.pop |
| 388 | if hasattr(self._queue, 'remove'): |
| 389 | self._remove = self._queue.remove |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 390 | self._wlock = allocate_lock() |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 391 | self._append = self._queue.append |
| 392 | |
| 393 | def _remove(self, obj): |
| 394 | """Python 2.4 compatibility.""" |
| 395 | for idx, item in enumerate(self._queue): |
| 396 | if item == obj: |
| 397 | del self._queue[idx] |
| 398 | break |
| 399 | |
Armin Ronacher | 7962ce7 | 2008-05-20 17:52:52 +0200 | [diff] [blame] | 400 | def __getstate__(self): |
| 401 | return { |
| 402 | 'capacity': self.capacity, |
| 403 | '_mapping': self._mapping, |
| 404 | '_queue': self._queue |
| 405 | } |
| 406 | |
| 407 | def __setstate__(self, d): |
| 408 | self.__dict__.update(d) |
| 409 | self._postinit() |
| 410 | |
| 411 | def __getnewargs__(self): |
| 412 | return (self.capacity,) |
| 413 | |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 414 | def copy(self): |
Florent Xicluna | 0ec4f76 | 2012-02-05 13:09:15 +0100 | [diff] [blame^] | 415 | """Return a shallow copy of the instance.""" |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 416 | rv = self.__class__(self.capacity) |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 417 | rv._mapping.update(self._mapping) |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 418 | rv._queue = deque(self._queue) |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 419 | return rv |
| 420 | |
| 421 | def get(self, key, default=None): |
| 422 | """Return an item from the cache dict or `default`""" |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 423 | try: |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 424 | return self[key] |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 425 | except KeyError: |
| 426 | return default |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 427 | |
| 428 | def setdefault(self, key, default=None): |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame] | 429 | """Set `default` if the key is not in the cache otherwise |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 430 | leave unchanged. Return the value of this key. |
| 431 | """ |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 432 | try: |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 433 | return self[key] |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 434 | except KeyError: |
| 435 | self[key] = default |
| 436 | return default |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 437 | |
| 438 | def clear(self): |
| 439 | """Clear the cache.""" |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 440 | self._wlock.acquire() |
| 441 | try: |
| 442 | self._mapping.clear() |
| 443 | self._queue.clear() |
| 444 | finally: |
| 445 | self._wlock.release() |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 446 | |
| 447 | def __contains__(self, key): |
| 448 | """Check if a key exists in this cache.""" |
| 449 | return key in self._mapping |
| 450 | |
| 451 | def __len__(self): |
| 452 | """Return the current size of the cache.""" |
| 453 | return len(self._mapping) |
| 454 | |
| 455 | def __repr__(self): |
| 456 | return '<%s %r>' % ( |
| 457 | self.__class__.__name__, |
| 458 | self._mapping |
| 459 | ) |
| 460 | |
| 461 | def __getitem__(self, key): |
| 462 | """Get an item from the cache. Moves the item up so that it has the |
| 463 | highest priority then. |
| 464 | |
Florent Xicluna | 0ec4f76 | 2012-02-05 13:09:15 +0100 | [diff] [blame^] | 465 | Raise a `KeyError` if it does not exist. |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 466 | """ |
| 467 | rv = self._mapping[key] |
| 468 | if self._queue[-1] != key: |
Armin Ronacher | 8de6f18 | 2009-01-12 11:08:26 +0100 | [diff] [blame] | 469 | try: |
| 470 | self._remove(key) |
Armin Ronacher | e7c72bc | 2009-09-14 12:20:33 -0700 | [diff] [blame] | 471 | except ValueError: |
Armin Ronacher | 8de6f18 | 2009-01-12 11:08:26 +0100 | [diff] [blame] | 472 | # if something removed the key from the container |
| 473 | # when we read, ignore the ValueError that we would |
| 474 | # get otherwise. |
| 475 | pass |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 476 | self._append(key) |
| 477 | return rv |
| 478 | |
| 479 | def __setitem__(self, key, value): |
| 480 | """Sets the value for an item. Moves the item up so that it |
| 481 | has the highest priority then. |
| 482 | """ |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 483 | self._wlock.acquire() |
| 484 | try: |
| 485 | if key in self._mapping: |
Armin Ronacher | 74230e6 | 2009-10-25 12:46:31 +0100 | [diff] [blame] | 486 | try: |
| 487 | self._remove(key) |
| 488 | except ValueError: |
| 489 | # __getitem__ is not locked, it might happen |
| 490 | pass |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 491 | elif len(self._mapping) == self.capacity: |
| 492 | del self._mapping[self._popleft()] |
| 493 | self._append(key) |
| 494 | self._mapping[key] = value |
| 495 | finally: |
| 496 | self._wlock.release() |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 497 | |
| 498 | def __delitem__(self, key): |
| 499 | """Remove an item from the cache dict. |
Florent Xicluna | 0ec4f76 | 2012-02-05 13:09:15 +0100 | [diff] [blame^] | 500 | Raise a `KeyError` if it does not exist. |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 501 | """ |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 502 | self._wlock.acquire() |
| 503 | try: |
| 504 | del self._mapping[key] |
Armin Ronacher | e7c72bc | 2009-09-14 12:20:33 -0700 | [diff] [blame] | 505 | try: |
| 506 | self._remove(key) |
| 507 | except ValueError: |
| 508 | # __getitem__ is not locked, it might happen |
| 509 | pass |
Armin Ronacher | 000b491 | 2008-05-01 18:40:15 +0200 | [diff] [blame] | 510 | finally: |
| 511 | self._wlock.release() |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 512 | |
Armin Ronacher | e25f24d | 2008-05-19 11:20:41 +0200 | [diff] [blame] | 513 | def items(self): |
| 514 | """Return a list of items.""" |
| 515 | result = [(key, self._mapping[key]) for key in list(self._queue)] |
| 516 | result.reverse() |
| 517 | return result |
| 518 | |
| 519 | def iteritems(self): |
| 520 | """Iterate over all items.""" |
| 521 | return iter(self.items()) |
| 522 | |
| 523 | def values(self): |
| 524 | """Return a list of all values.""" |
| 525 | return [x[1] for x in self.items()] |
| 526 | |
| 527 | def itervalue(self): |
| 528 | """Iterate over all values.""" |
| 529 | return iter(self.values()) |
| 530 | |
| 531 | def keys(self): |
| 532 | """Return a list of all keys ordered by most recent usage.""" |
| 533 | return list(self) |
| 534 | |
| 535 | def iterkeys(self): |
| 536 | """Iterate over all keys in the cache dict, ordered by |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 537 | the most recent usage. |
| 538 | """ |
Armin Ronacher | e224488 | 2008-05-19 09:25:57 +0200 | [diff] [blame] | 539 | return reversed(tuple(self._queue)) |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 540 | |
Armin Ronacher | e25f24d | 2008-05-19 11:20:41 +0200 | [diff] [blame] | 541 | __iter__ = iterkeys |
| 542 | |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 543 | def __reversed__(self): |
| 544 | """Iterate over the values in the cache dict, oldest items |
| 545 | coming first. |
| 546 | """ |
Armin Ronacher | e224488 | 2008-05-19 09:25:57 +0200 | [diff] [blame] | 547 | return iter(tuple(self._queue)) |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 548 | |
| 549 | __copy__ = copy |
| 550 | |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 551 | |
Armin Ronacher | 9bb7e47 | 2008-05-28 11:26:59 +0200 | [diff] [blame] | 552 | # register the LRU cache as mutable mapping if possible |
| 553 | try: |
| 554 | from collections import MutableMapping |
| 555 | MutableMapping.register(LRUCache) |
| 556 | except ImportError: |
| 557 | pass |
| 558 | |
| 559 | |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 560 | class Cycler(object): |
| 561 | """A cycle helper for templates.""" |
| 562 | |
| 563 | def __init__(self, *items): |
| 564 | if not items: |
| 565 | raise RuntimeError('at least one item has to be provided') |
| 566 | self.items = items |
| 567 | self.reset() |
| 568 | |
| 569 | def reset(self): |
| 570 | """Resets the cycle.""" |
| 571 | self.pos = 0 |
| 572 | |
| 573 | @property |
| 574 | def current(self): |
| 575 | """Returns the current item.""" |
| 576 | return self.items[self.pos] |
| 577 | |
| 578 | def next(self): |
| 579 | """Goes one item ahead and returns it.""" |
| 580 | rv = self.current |
| 581 | self.pos = (self.pos + 1) % len(self.items) |
| 582 | return rv |
| 583 | |
| 584 | |
Armin Ronacher | d34eb12 | 2008-10-13 23:47:51 +0200 | [diff] [blame] | 585 | class Joiner(object): |
| 586 | """A joining helper for templates.""" |
| 587 | |
| 588 | def __init__(self, sep=u', '): |
| 589 | self.sep = sep |
| 590 | self.used = False |
| 591 | |
| 592 | def __call__(self): |
| 593 | if not self.used: |
| 594 | self.used = True |
| 595 | return u'' |
| 596 | return self.sep |
| 597 | |
| 598 | |
Armin Ronacher | f9f5f26 | 2010-08-17 11:57:07 +0200 | [diff] [blame] | 599 | # try markupsafe first, if that fails go with Jinja2's bundled version |
| 600 | # of markupsafe. Markupsafe was previously Jinja2's implementation of |
Florent Xicluna | 0ec4f76 | 2012-02-05 13:09:15 +0100 | [diff] [blame^] | 601 | # the Markup object but was moved into a separate package in a patchlevel |
Armin Ronacher | f9f5f26 | 2010-08-17 11:57:07 +0200 | [diff] [blame] | 602 | # release |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 603 | try: |
Armin Ronacher | f9f5f26 | 2010-08-17 11:57:07 +0200 | [diff] [blame] | 604 | from markupsafe import Markup, escape, soft_unicode |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 605 | except ImportError: |
Armin Ronacher | f9f5f26 | 2010-08-17 11:57:07 +0200 | [diff] [blame] | 606 | from jinja2._markupsafe import Markup, escape, soft_unicode |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 607 | |
| 608 | |
| 609 | # partials |
| 610 | try: |
| 611 | from functools import partial |
| 612 | except ImportError: |
| 613 | class partial(object): |
| 614 | def __init__(self, _func, *args, **kwargs): |
Benjamin Wiegand | 228c183 | 2008-04-28 18:09:27 +0200 | [diff] [blame] | 615 | self._func = _func |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 616 | self._args = args |
| 617 | self._kwargs = kwargs |
| 618 | def __call__(self, *args, **kwargs): |
| 619 | kwargs.update(self._kwargs) |
| 620 | return self._func(*(self._args + args), **kwargs) |