This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
jinja.py
47 lines (38 loc) · 1.57 KB
/
jinja.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import glob
import os
from jinja2.loaders import FileSystemLoader, TemplateNotFound
from jinja2.loaders import split_template_path
class AmbiguousTemplate(Exception):
pass
class GlobFileLoader(FileSystemLoader):
"""
As ``jinja2.loaders.FileSystemLoader`` except allow support for globbing.
The loader takes the path to the templates as string, or if multiple
locations are wanted a list of them which is then looked up in the
given order:
>>> loader = GlobFileLoader('/path/to/templates')
>>> loader = GlobFileLoader(['/path/to/templates', '/other/path'])
Per default the template encoding is ``'utf-8'`` which can be changed
by setting the `encoding` parameter to something else.
"""
def get_source(self, environment, template):
pieces = split_template_path(template)
for searchpath in self.searchpath:
globbed_filename = os.path.join(searchpath, *pieces)
filenames = glob.glob(globbed_filename)
if len(filenames) > 1:
raise AmbiguousTemplate(template)
elif len(filenames) < 1:
continue
filename = filenames[0]
with open(filename) as f:
contents = f.read().decode(self.encoding)
mtime = os.path.getmtime(filename)
def uptodate():
try:
return os.path.getmtime(filename) == mtime
except OSError:
return False
return contents, filename, uptodate
else:
raise TemplateNotFound(template)