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
|
# coding: utf-8
"""
This module provides a Loader class.
"""
import os
DEFAULT_EXTENSION = 'mustache'
class Loader(object):
def __init__(self, search_dirs=None, encoding=None, extension=None):
"""
Construct a template loader.
Arguments:
search_dirs: the directories in which to search for templates.
Defaults to the current working directory.
"""
if extension is None:
extension = DEFAULT_EXTENSION
if search_dirs is None:
search_dirs = os.curdir # i.e. "."
if isinstance(search_dirs, basestring):
search_dirs = [search_dirs]
self.search_dirs = search_dirs
self.template_encoding = encoding
self.template_extension = extension
def load_template(self, template_name):
"""
Find and load the given template, and return it as a string.
Raises an IOError if the template cannot be found.
"""
search_dirs = self.search_dirs
file_name = template_name + '.' + self.template_extension
for dir_path in search_dirs:
file_path = os.path.join(dir_path, file_name)
if os.path.exists(file_path):
return self._load_template_file(file_path)
# TODO: we should probably raise an exception of our own type.
raise IOError('"%s" not found in "%s"' % (template_name, ':'.join(search_dirs),))
def _load_template_file(self, file_path):
"""
Read a template file, and return it as a string.
"""
f = open(file_path, 'r')
try:
template = f.read()
if self.template_encoding:
template = unicode(template, self.template_encoding)
finally:
f.close()
return template
|