Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: third_party/jinja2/bccache.py

Issue 23506004: Update Jinja2 (Python template library) to 2.7.1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/jinja2/_stringdefs.py ('k') | third_party/jinja2/compiler.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 """ 2 """
3 jinja2.bccache 3 jinja2.bccache
4 ~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~
5 5
6 This module implements the bytecode cache system Jinja is optionally 6 This module implements the bytecode cache system Jinja is optionally
7 using. This is useful if you have very complex template situations and 7 using. This is useful if you have very complex template situations and
8 the compiliation of all those templates slow down your application too 8 the compiliation of all those templates slow down your application too
9 much. 9 much.
10 10
11 Situations where this is useful are often forking web applications that 11 Situations where this is useful are often forking web applications that
12 are initialized on the first request. 12 are initialized on the first request.
13 13
14 :copyright: (c) 2010 by the Jinja Team. 14 :copyright: (c) 2010 by the Jinja Team.
15 :license: BSD. 15 :license: BSD.
16 """ 16 """
17 from os import path, listdir 17 from os import path, listdir
18 import sys 18 import sys
19 import marshal 19 import marshal
20 import tempfile 20 import tempfile
21 import cPickle as pickle
22 import fnmatch 21 import fnmatch
23 try: 22 from hashlib import sha1
24 from hashlib import sha1
25 except ImportError:
26 from sha import new as sha1
27 from jinja2.utils import open_if_exists 23 from jinja2.utils import open_if_exists
24 from jinja2._compat import BytesIO, pickle, PY2, text_type
28 25
29 26
30 # marshal works better on 3.x, one hack less required 27 # marshal works better on 3.x, one hack less required
31 if sys.version_info > (3, 0): 28 if not PY2:
32 from io import BytesIO
33 marshal_dump = marshal.dump 29 marshal_dump = marshal.dump
34 marshal_load = marshal.load 30 marshal_load = marshal.load
35 else: 31 else:
36 from cStringIO import StringIO as BytesIO
37 32
38 def marshal_dump(code, f): 33 def marshal_dump(code, f):
39 if isinstance(f, file): 34 if isinstance(f, file):
40 marshal.dump(code, f) 35 marshal.dump(code, f)
41 else: 36 else:
42 f.write(marshal.dumps(code)) 37 f.write(marshal.dumps(code))
43 38
44 def marshal_load(f): 39 def marshal_load(f):
45 if isinstance(f, file): 40 if isinstance(f, file):
46 return marshal.load(f) 41 return marshal.load(f)
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 """Clears the cache. This method is not used by Jinja2 but should be 153 """Clears the cache. This method is not used by Jinja2 but should be
159 implemented to allow applications to clear the bytecode cache used 154 implemented to allow applications to clear the bytecode cache used
160 by a particular environment. 155 by a particular environment.
161 """ 156 """
162 157
163 def get_cache_key(self, name, filename=None): 158 def get_cache_key(self, name, filename=None):
164 """Returns the unique hash key for this template name.""" 159 """Returns the unique hash key for this template name."""
165 hash = sha1(name.encode('utf-8')) 160 hash = sha1(name.encode('utf-8'))
166 if filename is not None: 161 if filename is not None:
167 filename = '|' + filename 162 filename = '|' + filename
168 if isinstance(filename, unicode): 163 if isinstance(filename, text_type):
169 filename = filename.encode('utf-8') 164 filename = filename.encode('utf-8')
170 hash.update(filename) 165 hash.update(filename)
171 return hash.hexdigest() 166 return hash.hexdigest()
172 167
173 def get_source_checksum(self, source): 168 def get_source_checksum(self, source):
174 """Returns a checksum for the source.""" 169 """Returns a checksum for the source."""
175 return sha1(source.encode('utf-8')).hexdigest() 170 return sha1(source.encode('utf-8')).hexdigest()
176 171
177 def get_bucket(self, environment, name, filename, source): 172 def get_bucket(self, environment, name, filename, source):
178 """Return a cache bucket for the given template. All arguments are 173 """Return a cache bucket for the given template. All arguments are
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 270
276 Returns the value for the cache key. If the item does not 271 Returns the value for the cache key. If the item does not
277 exist in the cache the return value must be `None`. 272 exist in the cache the return value must be `None`.
278 273
279 The other arguments to the constructor are the prefix for all keys that 274 The other arguments to the constructor are the prefix for all keys that
280 is added before the actual cache key and the timeout for the bytecode in 275 is added before the actual cache key and the timeout for the bytecode in
281 the cache system. We recommend a high (or no) timeout. 276 the cache system. We recommend a high (or no) timeout.
282 277
283 This bytecode cache does not support clearing of used items in the cache. 278 This bytecode cache does not support clearing of used items in the cache.
284 The clear method is a no-operation function. 279 The clear method is a no-operation function.
280
281 .. versionadded:: 2.7
282 Added support for ignoring memcache errors through the
283 `ignore_memcache_errors` parameter.
285 """ 284 """
286 285
287 def __init__(self, client, prefix='jinja2/bytecode/', timeout=None): 286 def __init__(self, client, prefix='jinja2/bytecode/', timeout=None,
287 ignore_memcache_errors=True):
288 self.client = client 288 self.client = client
289 self.prefix = prefix 289 self.prefix = prefix
290 self.timeout = timeout 290 self.timeout = timeout
291 self.ignore_memcache_errors = ignore_memcache_errors
291 292
292 def load_bytecode(self, bucket): 293 def load_bytecode(self, bucket):
293 code = self.client.get(self.prefix + bucket.key) 294 try:
295 code = self.client.get(self.prefix + bucket.key)
296 except Exception:
297 if not self.ignore_memcache_errors:
298 raise
299 code = None
294 if code is not None: 300 if code is not None:
295 bucket.bytecode_from_string(code) 301 bucket.bytecode_from_string(code)
296 302
297 def dump_bytecode(self, bucket): 303 def dump_bytecode(self, bucket):
298 args = (self.prefix + bucket.key, bucket.bytecode_to_string()) 304 args = (self.prefix + bucket.key, bucket.bytecode_to_string())
299 if self.timeout is not None: 305 if self.timeout is not None:
300 args += (self.timeout,) 306 args += (self.timeout,)
301 self.client.set(*args) 307 try:
308 self.client.set(*args)
309 except Exception:
310 if not self.ignore_memcache_errors:
311 raise
OLDNEW
« no previous file with comments | « third_party/jinja2/_stringdefs.py ('k') | third_party/jinja2/compiler.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698