autohandler extension
diff --git a/lib/mako/ext/autohandler.py b/lib/mako/ext/autohandler.py
index 843808f..ee5233a 100644
--- a/lib/mako/ext/autohandler.py
+++ b/lib/mako/ext/autohandler.py
@@ -4,19 +4,36 @@
def autohandler(context, _template_filename, name='autohandler'):
lookup = context.lookup
+
+ if not lookup.filesystem_checks:
+ try:
+ return lookup._uri_cache[(autohandler, _template_filename, name)]
+ except KeyError:
+ pass
+
uri = lookup.filename_to_uri(_template_filename)
- tokens = list(posixpath.split(posixpath.dirname(uri) + "/" + name))
- if posixpath.basename(uri) == name:
- tokens[-2:] = [name]
+ tokens = re.findall(r'([^/]+)', posixpath.dirname(uri)) + [name]
while len(tokens):
- path = posixpath.join(*tokens)
- psub = re.sub(r'^/', '',path)
- print "TOKENS", tokens
- for d in lookup.directories:
- print "FILE", posixpath.join(d, psub)
- if os.access(posixpath.join(d, psub), os.F_OK):
+ path = '/' + '/'.join(tokens)
+ if path != uri and _file_exists(lookup, path):
+ if not lookup.filesystem_checks:
+ return lookup._uri_cache.setdefault((autohandler, _template_filename, name), path)
+ else:
return path
if len(tokens) == 1:
break
tokens[-2:] = [name]
- print "NOW THE TOKENS ARE:", tokens
\ No newline at end of file
+
+ if not lookup.filesystem_checks:
+ return lookup._uri_cache.setdefault((autohandler, _template_filename, name), None)
+ else:
+ return None
+
+def _file_exists(lookup, path):
+ psub = re.sub(r'^/', '',path)
+ for d in lookup.directories:
+ if os.access(d + '/' + psub, os.F_OK):
+ return True
+ else:
+ return False
+
\ No newline at end of file
diff --git a/lib/mako/lookup.py b/lib/mako/lookup.py
index 8ef1e3b..cb286f6 100644
--- a/lib/mako/lookup.py
+++ b/lib/mako/lookup.py
@@ -45,10 +45,10 @@
self.template_args = {'format_exceptions':format_exceptions, 'error_handler':error_handler, 'output_encoding':output_encoding, 'module_directory':module_directory}
if collection_size == -1:
self.__collection = {}
- self.__uri_convert = {}
+ self._uri_cache = {}
else:
self.__collection = util.LRUCache(collection_size)
- self.__uri_convert = util.LRUCache(collection_size)
+ self._uri_cache = util.LRUCache(collection_size)
self._mutex = threading.Lock()
def get_template(self, uri):
@@ -69,7 +69,7 @@
def adjust_uri(self, uri, filename):
"""adjust the given uri based on the calling filename."""
try:
- return self.__uri_convert[(uri, filename)]
+ return self._uri_cache[(uri, filename)]
except KeyError:
if uri[0] != '/':
u = posixpath.normpath(uri)
@@ -79,15 +79,15 @@
u = posixpath.join(posixpath.dirname(rr), u)
else:
u = posixpath.join('/', u)
- return self.__uri_convert.setdefault((uri, filename), u)
+ return self._uri_cache.setdefault((uri, filename), u)
else:
- return self.__uri_convert.setdefault((uri, filename), uri)
+ return self._uri_cache.setdefault((uri, filename), uri)
def filename_to_uri(self, filename):
try:
- return self.__uri_convert[filename]
+ return self._uri_cache[filename]
except KeyError:
- return self.__uri_convert.setdefault(filename, self.__relativeize(filename))
+ return self._uri_cache.setdefault(filename, self.__relativeize(filename))
def __relativeize(self, filename):
"""return the portion of a filename that is 'relative' to the directories in this lookup."""
diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py
index 78dce44..0a15760 100644
--- a/lib/mako/runtime.py
+++ b/lib/mako/runtime.py
@@ -174,13 +174,15 @@
ih.inherits = Namespace("self:%s" % template.description, lclcontext, template = template, populate_self=False)
context._data['parent'] = lclcontext._data['local'] = ih.inherits
callable_ = getattr(template.module, '_mako_inherit', None)
- if callable_ is not None:
- return callable_(lclcontext)
- else:
- gen_ns = getattr(template.module, '_mako_generate_namespaces', None)
- if gen_ns is not None:
- gen_ns(context)
- return (template.callable_, lclcontext)
+ if callable_ is not None:
+ ret = callable_(lclcontext)
+ if ret:
+ return ret
+
+ gen_ns = getattr(template.module, '_mako_generate_namespaces', None)
+ if gen_ns is not None:
+ gen_ns(context)
+ return (template.callable_, lclcontext)
def _lookup_template(context, uri, relativeto):
lookup = context._with_template.lookup
@@ -195,9 +197,10 @@
self_ns = Namespace('self:%s' % template.description, context, template=template, populate_self=False)
context._data['self'] = context._data['local'] = self_ns
if hasattr(template.module, '_mako_inherit'):
- return template.module._mako_inherit(context) or (template.callable_, context)
- else:
- return (template.callable_, context)
+ ret = template.module._mako_inherit(context)
+ if ret:
+ return ret
+ return (template.callable_, context)
def _render(template, callable_, args, data, as_unicode=False):
"""create a Context and return the string output of the given template and template callable."""
diff --git a/test/lookup.py b/test/lookup.py
index 4d8b0ee..25d54e3 100644
--- a/test/lookup.py
+++ b/test/lookup.py
@@ -40,6 +40,11 @@
tl = lookup.TemplateLookup(directories=['./foo/bar'])
assert tl.filename_to_uri('./foo/bar/etc/index.html') == '/etc/index.html'
+
+ def test_uri_cache(self):
+ """test that the _uri_cache dictionary is available"""
+ tl._uri_cache[('foo', 'bar')] = '/some/path'
+ assert tl._uri_cache[('foo', 'bar')] == '/some/path'
if __name__ == '__main__':
unittest.main()
\ No newline at end of file