2 # Copyright (C) 2006, 2007, 2008 Michael Bayer mike_mp@zzzcomputing.com
4 # This module is part of Mako and is released under
5 # the MIT License: http://www.opensource.org/licenses/mit-license.php
7 import os, stat, posixpath, re
8 from mako import exceptions, util
9 from mako.template import Template
14 import dummy_threading as threading
16 class TemplateCollection(object):
17 def has_template(self, uri):
19 self.get_template(uri)
21 except exceptions.TemplateLookupException, e:
23 def get_template(self, uri, relativeto=None):
24 raise NotImplementedError()
25 def filename_to_uri(self, uri, filename):
26 """convert the given filename to a uri relative to this TemplateCollection."""
29 def adjust_uri(self, uri, filename):
30 """adjust the given uri based on the calling filename.
32 when this method is called from the runtime, the 'filename' parameter
33 is taken directly to the 'filename' attribute of the calling
34 template. Therefore a custom TemplateCollection subclass can place any string
35 identifier desired in the "filename" parameter of the Template objects it constructs
36 and have them come back here."""
39 class TemplateLookup(TemplateCollection):
40 def __init__(self, directories=None, module_directory=None, filesystem_checks=True, collection_size=-1, format_exceptions=False,
41 error_handler=None, disable_unicode=False, output_encoding=None, encoding_errors='strict', cache_type=None, cache_dir=None, cache_url=None,
42 cache_enabled=True, modulename_callable=None, default_filters=None, buffer_filters=[], imports=None, input_encoding=None, preprocessor=None):
43 if isinstance(directories, basestring):
44 directories = [directories]
45 self.directories = [posixpath.normpath(d) for d in directories or []]
46 self.module_directory = module_directory
47 self.modulename_callable = modulename_callable
48 self.filesystem_checks = filesystem_checks
49 self.collection_size = collection_size
50 self.template_args = {
51 'format_exceptions':format_exceptions,
52 'error_handler':error_handler,
53 'disable_unicode':disable_unicode,
54 'output_encoding':output_encoding,
55 'encoding_errors':encoding_errors,
56 'input_encoding':input_encoding,
57 'module_directory':module_directory,
58 'cache_type':cache_type,
59 'cache_dir':cache_dir or module_directory,
60 'cache_url':cache_url,
61 'cache_enabled':cache_enabled,
62 'default_filters':default_filters,
63 'buffer_filters':buffer_filters,
65 'preprocessor':preprocessor}
66 if collection_size == -1:
67 self.__collection = {}
70 self.__collection = util.LRUCache(collection_size)
71 self._uri_cache = util.LRUCache(collection_size)
72 self._mutex = threading.Lock()
74 def get_template(self, uri):
76 if self.filesystem_checks:
77 return self.__check(uri, self.__collection[uri])
79 return self.__collection[uri]
81 u = re.sub(r'^\/+', '', uri)
82 for dir in self.directories:
83 srcfile = posixpath.normpath(posixpath.join(dir, u))
84 if os.path.exists(srcfile):
85 return self.__load(srcfile, uri)
87 raise exceptions.TopLevelLookupException("Cant locate template for uri '%s'" % uri)
89 def adjust_uri(self, uri, relativeto):
90 """adjust the given uri based on the calling filename."""
93 if relativeto is not None:
94 return posixpath.join(posixpath.dirname(relativeto), uri)
101 def filename_to_uri(self, filename):
103 return self._uri_cache[filename]
105 value = self.__relativeize(filename)
106 self._uri_cache[filename] = value
109 def __relativeize(self, filename):
110 """return the portion of a filename that is 'relative' to the directories in this lookup."""
111 filename = posixpath.normpath(filename)
112 for dir in self.directories:
113 if filename[0:len(dir)] == dir:
114 return filename[len(dir):]
118 def __load(self, filename, uri):
119 self._mutex.acquire()
122 # try returning from collection one more time in case concurrent thread already loaded
123 return self.__collection[uri]
127 self.__collection[uri] = Template(uri=uri, filename=posixpath.normpath(filename), lookup=self, module_filename=(self.modulename_callable is not None and self.modulename_callable(filename, uri) or None), **self.template_args)
128 return self.__collection[uri]
130 self.__collection.pop(uri, None)
133 self._mutex.release()
135 def __check(self, uri, template):
136 if template.filename is None:
138 if not os.path.exists(template.filename):
139 self.__collection.pop(uri, None)
140 raise exceptions.TemplateLookupException("Cant locate template for uri '%s'" % uri)
141 elif template.module._modified_time < os.stat(template.filename)[stat.ST_MTIME]:
142 self.__collection.pop(uri, None)
143 return self.__load(template.filename, uri)
147 def put_string(self, uri, text):
148 self.__collection[uri] = Template(text, lookup=self, uri=uri, **self.template_args)
149 def put_template(self, uri, template):
150 self.__collection[uri] = template