Commit 423db900 by Chris Jerdonek

The Renderer class now passes search_dirs to Loader via the constructor.

parent 00fa136b
...@@ -31,7 +31,8 @@ class Loader(object): ...@@ -31,7 +31,8 @@ class Loader(object):
""" """
def __init__(self, file_encoding=None, extension=None, to_unicode=None): def __init__(self, file_encoding=None, extension=None, to_unicode=None,
search_dirs=None):
""" """
Construct a template loader instance. Construct a template loader instance.
...@@ -44,6 +45,9 @@ class Loader(object): ...@@ -44,6 +45,9 @@ class Loader(object):
file_encoding: the name of the encoding to use when converting file file_encoding: the name of the encoding to use when converting file
contents to unicode. Defaults to the package default. contents to unicode. Defaults to the package default.
search_dirs: the list of directories in which to search when loading
a template by name or file name. Defaults to the package default.
to_unicode: the function to use when converting strings of type to_unicode: the function to use when converting strings of type
str to unicode. The function should have the signature: str to unicode. The function should have the signature:
...@@ -61,11 +65,16 @@ class Loader(object): ...@@ -61,11 +65,16 @@ class Loader(object):
if file_encoding is None: if file_encoding is None:
file_encoding = defaults.FILE_ENCODING file_encoding = defaults.FILE_ENCODING
if search_dirs is None:
search_dirs = defaults.SEARCH_DIRS
if to_unicode is None: if to_unicode is None:
to_unicode = _to_unicode to_unicode = _to_unicode
self.extension = extension self.extension = extension
self.file_encoding = file_encoding self.file_encoding = file_encoding
# TODO: unit test setting this attribute.
self.search_dirs = search_dirs
self.to_unicode = to_unicode self.to_unicode = to_unicode
def _make_locator(self): def _make_locator(self):
...@@ -107,9 +116,8 @@ class Loader(object): ...@@ -107,9 +116,8 @@ class Loader(object):
return self.unicode(text, encoding) return self.unicode(text, encoding)
# TODO: consider passing search_dirs in the constructor.
# TODO: unit-test this method. # TODO: unit-test this method.
def load_name(self, name, search_dirs): def load_name(self, name):
""" """
Find and return the template with the given name. Find and return the template with the given name.
...@@ -122,13 +130,13 @@ class Loader(object): ...@@ -122,13 +130,13 @@ class Loader(object):
""" """
locator = self._make_locator() locator = self._make_locator()
path = locator.find_name(search_dirs, name) # TODO: change the order of these arguments.
path = locator.find_name(self.search_dirs, name)
return self.read(path) return self.read(path)
# TODO: consider passing search_dirs in the constructor.
# TODO: unit-test this method. # TODO: unit-test this method.
def load_object(self, obj, search_dirs): def load_object(self, obj):
""" """
Find and return the template associated to the given object. Find and return the template associated to the given object.
...@@ -141,6 +149,7 @@ class Loader(object): ...@@ -141,6 +149,7 @@ class Loader(object):
""" """
locator = self._make_locator() locator = self._make_locator()
path = locator.find_object(search_dirs, obj) # TODO: change the order of these arguments.
path = locator.find_object(self.search_dirs, obj)
return self.read(path) return self.read(path)
...@@ -76,8 +76,8 @@ class Renderer(object): ...@@ -76,8 +76,8 @@ class Renderer(object):
extension (i.e. to use extensionless template files). extension (i.e. to use extensionless template files).
Defaults to the package default. Defaults to the package default.
search_dirs: the list of directories in which to search for search_dirs: the list of directories in which to search when
templates when loading a template by name. If given a string, loading a template by name or file name. If given a string,
the method interprets the string as a single directory. the method interprets the string as a single directory.
Defaults to the package default. Defaults to the package default.
...@@ -167,7 +167,7 @@ class Renderer(object): ...@@ -167,7 +167,7 @@ class Renderer(object):
""" """
return Loader(file_encoding=self.file_encoding, extension=self.file_extension, return Loader(file_encoding=self.file_encoding, extension=self.file_extension,
to_unicode=self.unicode) to_unicode=self.unicode, search_dirs=self.search_dirs)
def _make_load_template(self): def _make_load_template(self):
""" """
...@@ -177,7 +177,7 @@ class Renderer(object): ...@@ -177,7 +177,7 @@ class Renderer(object):
loader = self._make_loader() loader = self._make_loader()
def load_template(template_name): def load_template(template_name):
return loader.load_name(template_name, self.search_dirs) return loader.load_name(template_name)
return load_template return load_template
...@@ -250,7 +250,7 @@ class Renderer(object): ...@@ -250,7 +250,7 @@ class Renderer(object):
""" """
loader = self._make_loader() loader = self._make_loader()
template = loader.load_object(obj, self.search_dirs) template = loader.load_object(obj)
context = [obj] + list(context) context = [obj] + list(context)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment