1
"""adds autohandler functionality to Mako templates.
3
requires that the TemplateLookup class is used with templates.
8
from mako.ext.autohandler import autohandler
10
<%inherit file="${autohandler(template, context)}"/>
13
or with custom autohandler filename:
16
from mako.ext.autohandler import autohandler
18
<%inherit file="${autohandler(template, context, name='somefilename')}"/>
22
import posixpath, os, re
24
def autohandler(template, context, name='autohandler'):
25
lookup = context.lookup
26
_template_uri = template.module._template_uri
27
if not lookup.filesystem_checks:
29
return lookup._uri_cache[(autohandler, _template_uri, name)]
33
tokens = re.findall(r'([^/]+)', posixpath.dirname(_template_uri)) + [name]
35
path = '/' + '/'.join(tokens)
36
if path != _template_uri and _file_exists(lookup, path):
37
if not lookup.filesystem_checks:
38
return lookup._uri_cache.setdefault((autohandler, _template_uri, name), path)
45
if not lookup.filesystem_checks:
46
return lookup._uri_cache.setdefault((autohandler, _template_uri, name), None)
50
def _file_exists(lookup, path):
51
psub = re.sub(r'^/', '',path)
52
for d in lookup.directories:
53
if os.access(d + '/' + psub, os.F_OK):
b'\\ No newline at end of file'