summaryrefslogtreecommitdiff
path: root/mako/lookup.py
diff options
context:
space:
mode:
Diffstat (limited to 'mako/lookup.py')
-rw-r--r--mako/lookup.py163
1 files changed, 83 insertions, 80 deletions
diff --git a/mako/lookup.py b/mako/lookup.py
index 0d3f304..a3010d5 100644
--- a/mako/lookup.py
+++ b/mako/lookup.py
@@ -5,10 +5,12 @@
# the MIT License: http://www.opensource.org/licenses/mit-license.php
import os
-import stat
import posixpath
import re
-from mako import exceptions, util
+import stat
+
+from mako import exceptions
+from mako import util
from mako.template import Template
try:
@@ -151,41 +153,41 @@ class TemplateLookup(TemplateCollection):
"""
- def __init__(self,
- directories=None,
- module_directory=None,
- filesystem_checks=True,
- collection_size=-1,
- format_exceptions=False,
- error_handler=None,
- disable_unicode=False,
- bytestring_passthrough=False,
- output_encoding=None,
- encoding_errors='strict',
-
- cache_args=None,
- cache_impl='beaker',
- cache_enabled=True,
- cache_type=None,
- cache_dir=None,
- cache_url=None,
-
- modulename_callable=None,
- module_writer=None,
- default_filters=None,
- buffer_filters=(),
- strict_undefined=False,
- imports=None,
- future_imports=None,
- enable_loop=True,
- input_encoding=None,
- preprocessor=None,
- lexer_cls=None,
- include_error_handler=None):
-
- self.directories = [posixpath.normpath(d) for d in
- util.to_list(directories, ())
- ]
+ def __init__(
+ self,
+ directories=None,
+ module_directory=None,
+ filesystem_checks=True,
+ collection_size=-1,
+ format_exceptions=False,
+ error_handler=None,
+ disable_unicode=False,
+ bytestring_passthrough=False,
+ output_encoding=None,
+ encoding_errors="strict",
+ cache_args=None,
+ cache_impl="beaker",
+ cache_enabled=True,
+ cache_type=None,
+ cache_dir=None,
+ cache_url=None,
+ modulename_callable=None,
+ module_writer=None,
+ default_filters=None,
+ buffer_filters=(),
+ strict_undefined=False,
+ imports=None,
+ future_imports=None,
+ enable_loop=True,
+ input_encoding=None,
+ preprocessor=None,
+ lexer_cls=None,
+ include_error_handler=None,
+ ):
+
+ self.directories = [
+ posixpath.normpath(d) for d in util.to_list(directories, ())
+ ]
self.module_directory = module_directory
self.modulename_callable = modulename_callable
self.filesystem_checks = filesystem_checks
@@ -195,34 +197,34 @@ class TemplateLookup(TemplateCollection):
cache_args = {}
# transfer deprecated cache_* args
if cache_dir:
- cache_args.setdefault('dir', cache_dir)
+ cache_args.setdefault("dir", cache_dir)
if cache_url:
- cache_args.setdefault('url', cache_url)
+ cache_args.setdefault("url", cache_url)
if cache_type:
- cache_args.setdefault('type', cache_type)
+ cache_args.setdefault("type", cache_type)
self.template_args = {
- 'format_exceptions': format_exceptions,
- 'error_handler': error_handler,
- 'include_error_handler': include_error_handler,
- 'disable_unicode': disable_unicode,
- 'bytestring_passthrough': bytestring_passthrough,
- 'output_encoding': output_encoding,
- 'cache_impl': cache_impl,
- 'encoding_errors': encoding_errors,
- 'input_encoding': input_encoding,
- 'module_directory': module_directory,
- 'module_writer': module_writer,
- 'cache_args': cache_args,
- 'cache_enabled': cache_enabled,
- 'default_filters': default_filters,
- 'buffer_filters': buffer_filters,
- 'strict_undefined': strict_undefined,
- 'imports': imports,
- 'future_imports': future_imports,
- 'enable_loop': enable_loop,
- 'preprocessor': preprocessor,
- 'lexer_cls': lexer_cls
+ "format_exceptions": format_exceptions,
+ "error_handler": error_handler,
+ "include_error_handler": include_error_handler,
+ "disable_unicode": disable_unicode,
+ "bytestring_passthrough": bytestring_passthrough,
+ "output_encoding": output_encoding,
+ "cache_impl": cache_impl,
+ "encoding_errors": encoding_errors,
+ "input_encoding": input_encoding,
+ "module_directory": module_directory,
+ "module_writer": module_writer,
+ "cache_args": cache_args,
+ "cache_enabled": cache_enabled,
+ "default_filters": default_filters,
+ "buffer_filters": buffer_filters,
+ "strict_undefined": strict_undefined,
+ "imports": imports,
+ "future_imports": future_imports,
+ "enable_loop": enable_loop,
+ "preprocessor": preprocessor,
+ "lexer_cls": lexer_cls,
}
if collection_size == -1:
@@ -248,17 +250,18 @@ class TemplateLookup(TemplateCollection):
else:
return self._collection[uri]
except KeyError:
- u = re.sub(r'^\/+', '', uri)
- for dir in self.directories:
+ u = re.sub(r"^\/+", "", uri)
+ for dir_ in self.directories:
# make sure the path seperators are posix - os.altsep is empty
# on POSIX and cannot be used.
- dir = dir.replace(os.path.sep, posixpath.sep)
- srcfile = posixpath.normpath(posixpath.join(dir, u))
+ dir_ = dir_.replace(os.path.sep, posixpath.sep)
+ srcfile = posixpath.normpath(posixpath.join(dir_, u))
if os.path.isfile(srcfile):
return self._load(srcfile, uri)
else:
raise exceptions.TopLevelLookupException(
- "Cant locate template for uri %r" % uri)
+ "Cant locate template for uri %r" % uri
+ )
def adjust_uri(self, uri, relativeto):
"""Adjust the given ``uri`` based on the given relative URI."""
@@ -267,12 +270,13 @@ class TemplateLookup(TemplateCollection):
if key in self._uri_cache:
return self._uri_cache[key]
- if uri[0] != '/':
+ if uri[0] != "/":
if relativeto is not None:
v = self._uri_cache[key] = posixpath.join(
- posixpath.dirname(relativeto), uri)
+ posixpath.dirname(relativeto), uri
+ )
else:
- v = self._uri_cache[key] = '/' + uri
+ v = self._uri_cache[key] = "/" + uri
else:
v = self._uri_cache[key] = uri
return v
@@ -295,9 +299,9 @@ class TemplateLookup(TemplateCollection):
"""
filename = posixpath.normpath(filename)
- for dir in self.directories:
- if filename[0:len(dir)] == dir:
- return filename[len(dir):]
+ for dir_ in self.directories:
+ if filename[0 : len(dir_)] == dir_:
+ return filename[len(dir_) :]
else:
return None
@@ -320,7 +324,8 @@ class TemplateLookup(TemplateCollection):
filename=posixpath.normpath(filename),
lookup=self,
module_filename=module_filename,
- **self.template_args)
+ **self.template_args
+ )
return template
except:
# if compilation fails etc, ensure
@@ -337,8 +342,7 @@ class TemplateLookup(TemplateCollection):
try:
template_stat = os.stat(template.filename)
- if template.module._modified_time < \
- template_stat[stat.ST_MTIME]:
+ if template.module._modified_time < template_stat[stat.ST_MTIME]:
self._collection.pop(uri, None)
return self._load(template.filename, uri)
else:
@@ -346,7 +350,8 @@ class TemplateLookup(TemplateCollection):
except OSError:
self._collection.pop(uri, None)
raise exceptions.TemplateLookupException(
- "Cant locate template for uri %r" % uri)
+ "Cant locate template for uri %r" % uri
+ )
def put_string(self, uri, text):
"""Place a new :class:`.Template` object into this
@@ -355,10 +360,8 @@ class TemplateLookup(TemplateCollection):
"""
self._collection[uri] = Template(
- text,
- lookup=self,
- uri=uri,
- **self.template_args)
+ text, lookup=self, uri=uri, **self.template_args
+ )
def put_template(self, uri, template):
"""Place a new :class:`.Template` object into this