Skip to content
Myr

[svn] fixed bug reported by stefan ebner and implemented cache_keys to fix problems with multiple laoders caching in the same folder

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 4c9cfcc · Browse files · 6 files+63 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+63 added−17 removed

  • tests:+9 added−2 removed
  • other:+54 added−15 removed

6 files+63 added−17 removed

modified:docs/src/loaders.txt+6 added-3 removed
59 unmodified lines
60 class CachedLoader(CachedLoaderMixin, BaseLoader):61 62 def __init__(self, path):63 self.path = path64 CachedLoaderMixin.__init__(64 CachedLoaderMixin.__init__(self,65 True, # use memory caching66 40, # for up to 40 templates67 '/tmp', # additionally save the compiled templates in /tmp68 True # and reload cached templates automatically if changed68 True, # and reload cached templates automatically if changed69 'foo' # optional salt used to keep templates with the same70 # name in the same cache folder, but from different71 # loaders. New in Jinja 1.1 and can be omitted.72 )7374 def get_source(self, environment, name, parent):75 filename = join(self.path, name)
14 unmodified lines
90You don't have to provide the `check_source_changed` method. If it doesn't91exist the option `auto_reload` won't have an effect. Also note that the92`check_source_changed` method must not raise an exception if the template93does not exist but return ``-1``. The return value ``-1`` is considered91"always reload" whereas ``0`` means "do not reload".94"always reload" whereas ``0`` means "do not reload".
modified:jinja/datastructure.py+9 added-2 removed
274 unmodified lines
275 if self._translate_func is None:276 translator = self.environment.get_translator(self)277 def translate(s, p=None, n=None, r=None):278 if p is None:279 return translator.gettext(s) % (r or {})280 return translator.ngettext(s, p, r[n]) % (r or {})279 s = translator.gettext(s)280 else:281 s = translator.ngettext(s, p, r[n])282 # apply replacement substitution only if replacements283 # are given. This is the case for {% trans %}...{% endtras %}284 # but for the "_()" syntax and a trans tag without a body.285 if r is not None:286 s %= r287 return s288 self._translate_func = translate289 return self._translate_func290 translate_func = property(translate_func, doc=translate_func.__doc__)291
297 unmodified lines
modified:jinja/loaders.py+31 added-10 removed
36 unmodified lines
37 return path.join(searchpath, path.sep.join([p for p in name.split('/')38 if p and p[0] != '.']))394041def get_cachename(cachepath, name):41def get_cachename(cachepath, name, salt=None):42 """43 Return the filename for a cached file.44 """45 return path.join(cachepath, 'jinja_%s.cache' %46 sha.new('jinja(%s)tmpl' % name).hexdigest())46 sha.new('jinja(%s|%s)tmpl' %47 (name, salt or '')).hexdigest())484950class LoaderWrapper(object):51 """
78 unmodified lines
130 """131 Mixin this class to implement simple memory and disk caching.132 """133133 def __init__(self, use_memcache, cache_size, cache_folder, auto_reload):134 def __init__(self, use_memcache, cache_size, cache_folder, auto_reload,135 cache_salt=None):136 if use_memcache:137 self.__memcache = CacheDict(cache_size)138 else:139 self.__memcache = None
1 unmodified line
141 if not hasattr(self, 'check_source_changed'):142 self.__auto_reload = False143 else:144 self.__auto_reload = auto_reload145 self.__salt = cache_salt146 self.__times = {}147 self.__lock = Lock()148149 def load(self, environment, name, translator):
38 unmodified lines
188189 # mem cache disabled or not cached by now190 # try to load if from the disk cache191 if tmpl is None and self.__cache_folder is not None:189 cache_fn = get_cachename(self.__cache_folder, name)192 cache_fn = get_cachename(self.__cache_folder, name, self.__salt)193 if last_change is not None:194 try:195 cache_time = path.getmtime(cache_fn)196 except OSError:
66 unmodified lines
263 that caching is disabled.264 ``auto_reload`` Set this to `False` for a slightly better265 performance. In that case Jinja won't check for266 template changes on the filesystem.267 ``cache_salt`` Optional unique number to not confuse the268 caching system when caching more than one269 template loader in the same folder. Defaults270 to the searchpath. *New in Jinja 1.1*271 =================== =================================================272 """273274 def __init__(self, searchpath, use_memcache=False, memcache_size=40,268 cache_folder=None, auto_reload=True):275 cache_folder=None, auto_reload=True, cache_salt=None):276 if cache_salt is None:277 cache_salt = searchpath278 self.searchpath = searchpath279 CachedLoaderMixin.__init__(self, use_memcache, memcache_size,271 cache_folder, auto_reload)280 cache_folder, auto_reload, cache_salt)281282 def get_source(self, environment, name, parent):283 filename = get_template_filename(self.searchpath, name)284 if path.exists(filename):
44 unmodified lines
329 performance. In that case Jinja won't check for330 template changes on the filesystem. If the331 templates are inside of an egg file this won't332 have an effect.333 ``cache_salt`` Optional unique number to not confuse the334 caching system when caching more than one335 template loader in the same folder. Defaults336 to ``package_name + '/' + package_path``.337 *New in Jinja 1.1*338 =================== =================================================339 """340341 def __init__(self, package_name, package_path, use_memcache=False,328 memcache_size=40, cache_folder=None, auto_reload=True):342 memcache_size=40, cache_folder=None, auto_reload=True,343 cache_salt=None):344 if resource_filename is None:345 raise ImportError('setuptools not found')346 self.package_name = package_name347 self.package_path = package_path348 if cache_salt is None:349 cache_salt = package_name + '/' + package_path350 # if we have an loader we probably retrieved it from an egg351 # file. In that case don't use the auto_reload!352 if auto_reload and getattr(__import__(package_name, '', '', ['']),353 '__loader__', None) is not None:354 auto_reload = False355 CachedLoaderMixin.__init__(self, use_memcache, memcache_size,339 cache_folder, auto_reload)356 cache_folder, auto_reload, cache_salt)357358 def get_source(self, environment, name, parent):359 name = '/'.join([self.package_path] + [p for p in name.split('/')360 if p != '..'])
56 unmodified lines
417 that caching is disabled.418 ``auto_reload`` Set this to `False` for a slightly better419 performance. In that case of `getmtime_func`420 not being provided this won't have an effect.421 ``cache_salt`` Optional unique number to not confuse the422 caching system when caching more than one423 template loader in the same folder.424 =================== =================================================425 """426427 def __init__(self, loader_func, getmtime_func=None, use_memcache=False,408 memcache_size=40, cache_folder=None, auto_reload=True):428 memcache_size=40, cache_folder=None, auto_reload=True,429 cache_salt=None):430 # when changing the signature also check the jinja.plugin function431 # loader instantiation.432 self.loader_func = loader_func433 self.getmtime_func = getmtime_func434 if auto_reload and getmtime_func is None:435 auto_reload = False436 CachedLoaderMixin.__init__(self, use_memcache, memcache_size,416 cache_folder, auto_reload)437 cache_folder, auto_reload, cache_salt)438439 def get_source(self, environment, name, parent):440 rv = self.loader_func(name)441 if rv is None:
70 unmodified lines
modified:tests/test_i18n.py+9 added-2 removed
12 unmodified lines
13 '{% block body %}{% endblock %}',14 'child.html': '{% extends "master.html" %}{% block body %}'15 '{% trans "watch out" %}{% endblock %}',16 'plural.html': '{% trans user_count %}One user online{% pluralize %}'17 '{{ user_count }} users online{% endtrans %}'17 '{{ user_count }} users online{% endtrans %}',18 'stringformat.html': '{{ _("User: %d")|format(user_count) }}'19}202122languages = {23 'de': {24 'missing': 'fehlend',25 'watch out': 'pass auf',26 'One user online': 'Ein Benutzer online',26 '%(user_count)s users online': '%(user_count)s Benutzer online'27 '%(user_count)s users online': '%(user_count)s Benutzer online',28 'User: %d': 'Benutzer: %d'29 }30}3132
45 unmodified lines
78def test_trans_plural():79 tmpl = i18n_env.get_template('plural.html')80 assert tmpl.render(LANGUAGE='de', user_count=1) == 'Ein Benutzer online'81 assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online'828384def test_trans_stringformatting():85 tmpl = i18n_env.get_template('stringformat.html')86 assert tmpl.render(LANGUAGE='de', user_count=5) == 'Benutzer: 5'
modified:CHANGES+6 added
81 unmodified lines
82- it's not possible to stream templates.8384- fixed a corner case when defining a block inside of a condition8586- the cached loader mixin is now able to cache multiple templates from87 different loaders in the same cache folder.8889- Translatable strings returned by ``_()`` will leave their string formatting90 signs untouched. Thanks to Stefan Ebner for reporting.919293Version 1.094-----------95(released Mar 23, 2007)96
1 unmodified line
modified:THANKS+2 added
2 unmodified lines
34All the people listed here helped improving Jinja a lot, provided5patches, helped working out solutions etc. Thanks to all of you!67- Ronny Pfannschmidt8- Axel Böhm9- Alexey Melchakov10- Stefan Ebner