-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathloaders.py
More file actions
104 lines (73 loc) · 3.22 KB
/
Copy pathloaders.py
File metadata and controls
104 lines (73 loc) · 3.22 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# --------------------------------------------------------------------------
# Default template loaders.
# --------------------------------------------------------------------------
import os
from .templates import Template
from .errors import LoadError, TemplateNotFound
class FileLoader:
""" Loads templates from the file system. Assumes files are utf-8 encoded.
Compiled templates are cached in memory, so they only need to be
compiled once. Templates are *not* automatically recompiled if the
underlying template file changes.
A FileLoader instance should be initialized with a path to a base
template directory.
loader = FileLoader('/path/to/base/directory')
The loader instance can then be called with one or more path strings.
The loader will return the template object corresponding to the first
existing template file or raise a TemplateNotFound exception if no file
can be located.
Note that the path strings may include subdirectory paths:
template = loader('foo.txt')
template = loader('subdir/foo.txt')
"""
def __init__(self, root):
self.root = root
self.cache = {}
def __call__(self, *paths):
for path in paths:
if path in self.cache:
return self.cache[path]
fullpath = os.path.join(self.root, path)
if os.path.isfile(fullpath):
try:
with open(fullpath, encoding='utf-8') as file:
return self.cache.setdefault(path, Template(file.read()))
except OSError:
raise LoadError("error loading template file [%s]" % path)
raise TemplateNotFound()
class FileReloader:
""" Loads templates from the file system. Assumes files are utf-8 encoded.
Compiled templates are cached in memory, so they only need to be
compiled once. Templates are automatically recompiled if the
underlying template file changes.
"""
def __init__(self, root):
self.root = root
self.cache = {}
def __call__(self, *paths):
for path in paths:
fullpath = os.path.join(self.root, path)
if os.path.isfile(fullpath):
try:
mtime = os.path.getmtime(fullpath)
if path in self.cache:
if mtime == self.cache[path][0]:
return self.cache[path][1]
with open(fullpath, encoding='utf-8') as file:
self.cache[path] = (mtime, Template(file.read()))
return self.cache[path][1]
except OSError:
raise LoadError("error loading template file [%s]" % path)
raise TemplateNotFound()
class DictLoader:
""" Loads templates from a dictionary of template strings. """
def __init__(self, strings):
self.templates = {}
self.strings = strings
def __call__(self, *names):
for name in names:
if name in self.templates:
return self.templates[name]
elif name in self.strings:
return self.templates.setdefault(name, Template(self.strings[name]))
raise TemplateNotFound()