Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 3 | jinja2.loaders |
| 4 | ~~~~~~~~~~~~~~ |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 5 | |
| 6 | Jinja loader classes. |
| 7 | |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 8 | :copyright: 2008 by Armin Ronacher. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 9 | :license: BSD, see LICENSE for more details. |
| 10 | """ |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 11 | from os import path |
| 12 | from jinja2.exceptions import TemplateNotFound |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 13 | from jinja2.utils import LRUCache |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 14 | |
| 15 | |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 16 | def split_template_path(template): |
| 17 | """Split a path into segments and perform a sanity check. If it detects |
| 18 | '..' in the path it will raise a `TemplateNotFound` error. |
| 19 | """ |
| 20 | pieces = [] |
| 21 | for piece in template.split('/'): |
| 22 | if path.sep in piece \ |
| 23 | or (path.altsep and path.altsep in piece) or \ |
| 24 | piece == path.pardir: |
| 25 | raise TemplateNotFound(template) |
Armin Ronacher | 58f351d | 2008-05-28 21:30:14 +0200 | [diff] [blame^] | 26 | elif piece and piece != '.': |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 27 | pieces.append(piece) |
| 28 | return pieces |
| 29 | |
| 30 | |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 31 | class BaseLoader(object): |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 32 | """Baseclass for all loaders. Subclass this and override `get_source` to |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 33 | implement a custom loading mechanism. The environment provides a |
| 34 | `get_template` method that calls the loader's `load` method to get the |
| 35 | :class:`Template` object. |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 36 | |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 37 | A very basic example for a loader that looks up templates on the file |
| 38 | system could look like this:: |
| 39 | |
| 40 | from jinja2 import BaseLoader, TemplateNotFound |
| 41 | from os.path import join, exists, getmtime |
| 42 | |
| 43 | class MyLoader(BaseLoader): |
| 44 | |
| 45 | def __init__(self, path, cache_size=50, auto_reload=True): |
| 46 | BaseLoader.__init__(self, cache_size, auto_reload) |
| 47 | self.path = path |
| 48 | |
| 49 | def get_source(self, environment, template): |
| 50 | path = join(self.path, template) |
| 51 | if not exists(path): |
| 52 | raise TemplateNotFound(template) |
| 53 | mtime = getmtime(path) |
| 54 | with file(path) as f: |
| 55 | source = f.read().decode('utf-8') |
Armin Ronacher | 4dc9578 | 2008-05-04 01:11:14 +0200 | [diff] [blame] | 56 | return source, path, lambda: mtime == getmtime(path) |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 57 | """ |
| 58 | |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 59 | def get_source(self, environment, template): |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 60 | """Get the template source, filename and reload helper for a template. |
| 61 | It's passed the environment and template name and has to return a |
| 62 | tuple in the form ``(source, filename, uptodate)`` or raise a |
| 63 | `TemplateNotFound` error if it can't locate the template. |
| 64 | |
| 65 | The source part of the returned tuple must be the source of the |
| 66 | template as unicode string or a ASCII bytestring. The filename should |
| 67 | be the name of the file on the filesystem if it was loaded from there, |
| 68 | otherwise `None`. The filename is used by python for the tracebacks |
| 69 | if no loader extension is used. |
| 70 | |
| 71 | The last item in the tuple is the `uptodate` function. If auto |
| 72 | reloading is enabled it's always called to check if the template |
| 73 | changed. No arguments are passed so the function must store the |
| 74 | old state somewhere (for example in a closure). If it returns `False` |
| 75 | the template will be reloaded. |
| 76 | """ |
| 77 | raise TemplateNotFound(template) |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 78 | |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 79 | def load(self, environment, name, globals=None): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 80 | """Loads a template. This method looks up the template in the cache |
| 81 | or loads one by calling :meth:`get_source`. Subclasses should not |
| 82 | override this method as loaders working on collections of other |
| 83 | loaders (such as :class:`PrefixLoader` or :class:`ChoiceLoader`) |
| 84 | will not call this method but `get_source` directly. |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 85 | """ |
| 86 | if globals is None: |
| 87 | globals = {} |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 88 | source, filename, uptodate = self.get_source(environment, name) |
Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 89 | code = environment.compile(source, name, filename) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 90 | return environment.template_class.from_code(environment, code, |
| 91 | globals, uptodate) |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 92 | |
| 93 | |
| 94 | class FileSystemLoader(BaseLoader): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 95 | """Loads templates from the file system. This loader can find templates |
| 96 | in folders on the file system and is the preferred way to load them. |
| 97 | |
| 98 | The loader takes the path to the templates as string, or if multiple |
| 99 | locations are wanted a list of them which is then looked up in the |
| 100 | given order: |
| 101 | |
| 102 | >>> loader = FileSystemLoader('/path/to/templates') |
| 103 | >>> loader = FileSystemLoader(['/path/to/templates', '/other/path']) |
| 104 | |
| 105 | Per default the template encoding is ``'utf-8'`` which can be changed |
| 106 | by setting the `encoding` parameter to something else. |
| 107 | """ |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 108 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 109 | def __init__(self, searchpath, encoding='utf-8'): |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 110 | if isinstance(searchpath, basestring): |
| 111 | searchpath = [searchpath] |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 112 | self.searchpath = list(searchpath) |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 113 | self.encoding = encoding |
| 114 | |
| 115 | def get_source(self, environment, template): |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 116 | pieces = split_template_path(template) |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 117 | for searchpath in self.searchpath: |
| 118 | filename = path.join(searchpath, *pieces) |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 119 | if not path.isfile(filename): |
| 120 | continue |
| 121 | f = file(filename) |
| 122 | try: |
| 123 | contents = f.read().decode(self.encoding) |
| 124 | finally: |
| 125 | f.close() |
| 126 | old = path.getmtime(filename) |
Armin Ronacher | 4dc9578 | 2008-05-04 01:11:14 +0200 | [diff] [blame] | 127 | return contents, filename, lambda: path.getmtime(filename) == old |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 128 | raise TemplateNotFound(template) |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 129 | |
| 130 | |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 131 | class PackageLoader(BaseLoader): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 132 | """Load templates from python eggs or packages. It is constructed with |
| 133 | the name of the python package and the path to the templates in that |
| 134 | package: |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 135 | |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 136 | >>> loader = PackageLoader('mypackage', 'views') |
| 137 | |
| 138 | If the package path is not given, ``'templates'`` is assumed. |
| 139 | |
| 140 | Per default the template encoding is ``'utf-8'`` which can be changed |
| 141 | by setting the `encoding` parameter to something else. Due to the nature |
| 142 | of eggs it's only possible to reload templates if the package was loaded |
| 143 | from the file system and not a zip file. |
| 144 | """ |
| 145 | |
| 146 | def __init__(self, package_name, package_path='templates', |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 147 | encoding='utf-8'): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 148 | from pkg_resources import DefaultProvider, ResourceManager, get_provider |
| 149 | provider = get_provider(package_name) |
| 150 | self.encoding = encoding |
| 151 | self.manager = ResourceManager() |
| 152 | self.filesystem_bound = isinstance(provider, DefaultProvider) |
| 153 | self.provider = provider |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 154 | self.package_path = package_path |
| 155 | |
| 156 | def get_source(self, environment, template): |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 157 | pieces = split_template_path(template) |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 158 | p = '/'.join((self.package_path,) + tuple(pieces)) |
| 159 | if not self.provider.has_resource(p): |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 160 | raise TemplateNotFound(template) |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 161 | |
| 162 | filename = uptodate = None |
| 163 | if self.filesystem_bound: |
| 164 | filename = self.provider.get_resource_filename(self.manager, p) |
| 165 | mtime = path.getmtime(filename) |
| 166 | def uptodate(): |
Armin Ronacher | 4dc9578 | 2008-05-04 01:11:14 +0200 | [diff] [blame] | 167 | return path.getmtime(filename) == mtime |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 168 | |
| 169 | source = self.provider.get_resource_string(self.manager, p) |
| 170 | return source.decode(self.encoding), filename, uptodate |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 171 | |
| 172 | |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 173 | class DictLoader(BaseLoader): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 174 | """Loads a template from a python dict. It's passed a dict of unicode |
| 175 | strings bound to template names. This loader is useful for unittesting: |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 176 | |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 177 | >>> loader = DictLoader({'index.html': 'source here'}) |
| 178 | |
| 179 | Because auto reloading is rarely useful this is disabled per default. |
| 180 | """ |
| 181 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 182 | def __init__(self, mapping): |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 183 | self.mapping = mapping |
| 184 | |
| 185 | def get_source(self, environment, template): |
| 186 | if template in self.mapping: |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 187 | source = self.mapping[template] |
| 188 | return source, None, lambda: source != self.mapping[template] |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 189 | raise TemplateNotFound(template) |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 190 | |
| 191 | |
| 192 | class FunctionLoader(BaseLoader): |
| 193 | """A loader that is passed a function which does the loading. The |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 194 | function becomes the name of the template passed and has to return either |
| 195 | an unicode string with the template source, a tuple in the form ``(source, |
| 196 | filename, uptodatefunc)`` or `None` if the template does not exist. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 197 | |
| 198 | >>> def load_template(name): |
| 199 | ... if name == 'index.html' |
| 200 | ... return '...' |
| 201 | ... |
| 202 | >>> loader = FunctionLoader(load_template) |
| 203 | |
| 204 | The `uptodatefunc` is a function that is called if autoreload is enabled |
| 205 | and has to return `True` if the template is still up to date. For more |
| 206 | details have a look at :meth:`BaseLoader.get_source` which has the same |
| 207 | return value. |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 208 | """ |
| 209 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 210 | def __init__(self, load_func): |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 211 | self.load_func = load_func |
| 212 | |
| 213 | def get_source(self, environment, template): |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 214 | rv = self.load_func(template) |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 215 | if rv is None: |
| 216 | raise TemplateNotFound(template) |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 217 | elif isinstance(rv, basestring): |
| 218 | return rv, None, None |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 219 | return rv |
| 220 | |
| 221 | |
| 222 | class PrefixLoader(BaseLoader): |
| 223 | """A loader that is passed a dict of loaders where each loader is bound |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 224 | to a prefix. The prefix is delimited from the template by a slash per |
| 225 | default, which can be changed by setting the `delimiter` argument to |
| 226 | something else. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 227 | |
| 228 | >>> loader = PrefixLoader({ |
| 229 | ... 'app1': PackageLoader('mypackage.app1'), |
| 230 | ... 'app2': PackageLoader('mypackage.app2') |
| 231 | ... }) |
| 232 | |
| 233 | By loading ``'app1/index.html'`` the file from the app1 package is loaded, |
| 234 | by loading ``'app2/index.html'`` the file from the second. |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 235 | """ |
| 236 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 237 | def __init__(self, mapping, delimiter='/'): |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 238 | self.mapping = mapping |
| 239 | self.delimiter = delimiter |
| 240 | |
| 241 | def get_source(self, environment, template): |
| 242 | try: |
| 243 | prefix, template = template.split(self.delimiter, 1) |
| 244 | loader = self.mapping[prefix] |
| 245 | except (ValueError, KeyError): |
| 246 | raise TemplateNotFound(template) |
| 247 | return loader.get_source(environment, template) |
| 248 | |
| 249 | |
| 250 | class ChoiceLoader(BaseLoader): |
| 251 | """This loader works like the `PrefixLoader` just that no prefix is |
| 252 | specified. If a template could not be found by one loader the next one |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 253 | is tried. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 254 | |
| 255 | >>> loader = ChoiceLoader([ |
| 256 | ... FileSystemLoader('/path/to/user/templates'), |
| 257 | ... PackageLoader('myapplication') |
Armin Ronacher | 61a5a24 | 2008-05-26 12:07:44 +0200 | [diff] [blame] | 258 | ... ]) |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 259 | |
| 260 | This is useful if you want to allow users to override builtin templates |
| 261 | from a different location. |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 262 | """ |
| 263 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 264 | def __init__(self, loaders): |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 265 | self.loaders = loaders |
| 266 | |
| 267 | def get_source(self, environment, template): |
| 268 | for loader in self.loaders: |
| 269 | try: |
| 270 | return loader.get_source(environment, template) |
| 271 | except TemplateNotFound: |
| 272 | pass |
| 273 | raise TemplateNotFound(template) |