[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
Provenance
- Commit
fe0ce52- Tree
599116e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
15fae54
3 files changed+54 added−26 removed
modified:docs/src/loaders.txt+12 added-13 removed
11 unmodified lines12 Developing Loaders13 ==================14 15 Template loaders are just normal Python classes that have to provide some16-functions used to load and translate templates.16+functions used to load and translate templates. Because some of the tasks17+a loader has to do are redundant there are some classes that make loader18+development easier.19 18-Because some of the tasks a loader has to do are redundant there are mixin19-classes that make loader development easier.20-21-Here the implementation of a simple loader based on the `LoaderMixin` from20+Here the implementation of a simple loader based on the `BaseLoader` from21 `jinja.loaders`:22 23 .. sourcecode:: python24 25 import codecs26 from os.path import join28- from jinja.loaders import LoaderMixin27+ from jinja.loaders import BaseLoader28 from jinja.exceptions import TemplateNotFound29 31- class SimpleLoader(LoaderMixin):30+ class SimpleLoader(BaseLoader):31 32 def __init__(self, path):33 self.path = path34 7 unmodified lines42 finally:43 f.close()44 45 The functions `load` and `parse` which are a requirement for a loader are47-added automatically by the `LoaderMixin`.46+added automatically by the `BaseLoader`.47 49-Additionally to the `LoaderMixin` there is a second mixin called48+Additionally to the `BaseLoader` there is a mixin class called49 `CachedLoaderMixin` that implements memory and disk caching of templates.50+Note 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:52 52-It works basically the same, just that there are two more things to implement:53-53 .. sourcecode:: python54 55 import codecs56 from os.path import join, getmtime58- from jinja.loaders import CachedLoaderMixin57+ from jinja.loaders import BaseLoaderCachedLoaderMixin58 from jinja.exceptions import TemplateNotFound59 61- class CachedLoader(CachedLoaderMixin):60+ class CachedLoader(CachedLoaderMixin, BaseLoader):61 62 def __init__(self, path):63 self.path = path64 CachedLoaderMixin.__init__(21 unmodified linesmodified:jinja/__init__.py-1 removed
7 unmodified lines8 """9 from jinja.environment import Environment10 from jinja.loaders import *11 12-12 from_string = Environment().from_stringmodified:jinja/loaders.py+42 added-12 removed
87 unmodified lines88 def __nonzero__(self):89 return self.loader is not None90 91 92-class LoaderMixin(object):92+class BaseLoader(object):93 """94 Use this class to implement loaders.95 96- 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.99 100 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 """103 104 def parse(self, environment, name, parent):105 """9 unmodified lines115 ast = self.parse(environment, name, None)116 return translator.process(environment, ast)117 118 119-class CachedLoaderMixin(LoaderMixin):119+class 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 """123 124 def __init__(self, use_memcache, cache_size, cache_folder, auto_reload):125 if use_memcache:8 unmodified lines134 self.__times = {}135 self.__lock = Lock()136 137 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 translators34 unmodified lines183 f.close()184 185 # 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)189 190 # save the compiled template191 f = file(cache_fn, 'wb')192 try:9 unmodified lines202 return tmpl203 204 # 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()210 211 204-class FileSystemLoader(CachedLoaderMixin):212+class FileSystemLoader(CachedLoaderMixin, BaseLoader):213 """214 Loads templates from the filesystem:215 216 .. sourcecode:: python46 unmodified lines263 def check_source_changed(self, environment, name):264 return path.getmtime(get_template_filename(self.searchpath, name))265 266 259-class PackageLoader(CachedLoaderMixin):267+class PackageLoader(CachedLoaderMixin, BaseLoader):268 """269 Loads templates from python packages using setuptools.270 271 .. sourcecode:: python54 unmodified lines326 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))330+331+332+class DictLoader(BaseLoader):333+ """334+ Load templates from a given dict:335+336+ .. sourcecode:: python337+338+ from jinja import Environment, DictLoader339+ e = Environment(loader=DictLoader(dict(340+ layout='...',341+ index='{% extends layout %}...'342+ )))343+ """344+345+ def __init__(self, templates):346+ self.templates = templates347+348+ def get_source(self, environment, name, parent):349+ if name in self.templates:350+ return self.templates[name]351+ raise TemplateNotFound(name)