Skip to content
Myr

[svn] some api changes in the loader system regarding subclassing.

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 15fae54 · Browse files · 3 files+54 added−26 removed
This commit

State

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

Diff stats

3 files+54 added−26 removed

  • other:+54 added−26 removed

3 files+54 added−26 removed

modified:docs/src/loaders.txt+12 added-13 removed
11 unmodified lines
12Developing Loaders13==================1415Template loaders are just normal Python classes that have to provide some16functions used to load and translate templates.16functions used to load and translate templates. Because some of the tasks17a loader has to do are redundant there are some classes that make loader18development easier.1918Because some of the tasks a loader has to do are redundant there are mixin19classes that make loader development easier.2021Here the implementation of a simple loader based on the `LoaderMixin` from20Here the implementation of a simple loader based on the `BaseLoader` from21`jinja.loaders`:2223.. sourcecode:: python2425 import codecs26 from os.path import join28 from jinja.loaders import LoaderMixin27 from jinja.loaders import BaseLoader28 from jinja.exceptions import TemplateNotFound2931 class SimpleLoader(LoaderMixin):30 class SimpleLoader(BaseLoader):31 32 def __init__(self, path):33 self.path = path34
7 unmodified lines
42 finally:43 f.close()4445The functions `load` and `parse` which are a requirement for a loader are47added automatically by the `LoaderMixin`.46added automatically by the `BaseLoader`.4749Additionally to the `LoaderMixin` there is a second mixin called48Additionally to the `BaseLoader` there is a mixin class called49`CachedLoaderMixin` that implements memory and disk caching of templates.50Note that you have to give it a higher priority in the MRO than the51`BaseLoader` which means that's the first base class when inheriting from it:5252It works basically the same, just that there are two more things to implement:5353.. sourcecode:: python5455 import codecs56 from os.path import join, getmtime58 from jinja.loaders import CachedLoaderMixin57 from jinja.loaders import BaseLoaderCachedLoaderMixin58 from jinja.exceptions import TemplateNotFound5961 class CachedLoader(CachedLoaderMixin):60 class CachedLoader(CachedLoaderMixin, BaseLoader):61 62 def __init__(self, path):63 self.path = path64 CachedLoaderMixin.__init__(
21 unmodified lines
modified:jinja/__init__.py-1 removed
7 unmodified lines
8"""9from jinja.environment import Environment10from jinja.loaders import *111212from_string = Environment().from_string
modified:jinja/loaders.py+42 added-12 removed
87 unmodified lines
88 def __nonzero__(self):89 return self.loader is not None909192class LoaderMixin(object):92class BaseLoader(object):93 """94 Use this class to implement loaders.9596 Just mixin this class and implement a method called `get_source`97 with the signature (`environment`, `name`, `parent`) that returns98 sourcecode for the template.96 Just inherit from this class and implement a method called97 `get_source` with the signature (`environment`, `name`, `parent`)98 that returns sourcecode for the template.99100 For more complex loaders you probably want to override `load` to101 or not use the `LoaderMixin` at all.101 or not use the `BaseLoader` at all.102 """103104 def parse(self, environment, name, parent):105 """
9 unmodified lines
115 ast = self.parse(environment, name, None)116 return translator.process(environment, ast)117118119class CachedLoaderMixin(LoaderMixin):119class CachedLoaderMixin(object):120 """121 Works like the loader mixin just that it supports caching.121 Mixin this class to implement simple memory and disk caching.122 """123124 def __init__(self, use_memcache, cache_size, cache_folder, auto_reload):125 if use_memcache:
8 unmodified lines
134 self.__times = {}135 self.__lock = Lock()136137 def load(self, environment, name, translator):138 """139 Load and translate a template. First we check if there is a140 cached version of this template in the memory cache. If this is141 not the cache check for a compiled template in the disk cache142 folder. And if none of this is the case we translate the temlate143 using the `LoaderMixin.load` function, cache and return it.144 """145 self.__lock.acquire()146 try:147 # caching is only possible for the python translator. skip148 # all other translators
34 unmodified lines
183 f.close()184185 # no template so far, parse, translate and compile it186 elif tmpl is None:180 tmpl = LoaderMixin.load(self, environment,181 name, translator)187 tmpl = super(CachedLoaderMixin, self).load(188 environment, name, translator)189190 # save the compiled template191 f = file(cache_fn, 'wb')192 try:
9 unmodified lines
202 return tmpl203204 # if we reach this point we don't have caching enabled or translate205 # to something else than python199 return LoaderMixin.load(self, environment, name, translator)206 return super(CachedLoaderMixin, self).load(207 environment, name, translator)208 finally:209 self.__lock.release()210211204class FileSystemLoader(CachedLoaderMixin):212class FileSystemLoader(CachedLoaderMixin, BaseLoader):213 """214 Loads templates from the filesystem:215216 .. sourcecode:: python
46 unmodified lines
263 def check_source_changed(self, environment, name):264 return path.getmtime(get_template_filename(self.searchpath, name))265266259class PackageLoader(CachedLoaderMixin):267class PackageLoader(CachedLoaderMixin, BaseLoader):268 """269 Loads templates from python packages using setuptools.270271 .. sourcecode:: python
54 unmodified lines
326 def check_source_changed(self, environment, name):327 name = '/'.join([self.package_path] + [p for p in name.split('/')328 if p and p[0] != '.'])329 return path.getmtime(resource_filename(name))330331332class DictLoader(BaseLoader):333 """334 Load templates from a given dict:335336 .. sourcecode:: python337338 from jinja import Environment, DictLoader339 e = Environment(loader=DictLoader(dict(340 layout='...',341 index='{% extends layout %}...'342 )))343 """344345 def __init__(self, templates):346 self.templates = templates347348 def get_source(self, environment, name, parent):349 if name in self.templates:350 return self.templates[name]351 raise TemplateNotFound(name)