diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-04-01 11:38:59 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-04-01 11:38:59 -0700 |
commit | 423db900a6b3b447c15f292c14b6fb794af18061 (patch) | |
tree | 7a16fd6183bf7f3c88f4778f63db261b7d35f509 | |
parent | 00fa136b0c2c5725148535fef58b481583ba7a5e (diff) | |
download | pystache-423db900a6b3b447c15f292c14b6fb794af18061.tar.gz |
The Renderer class now passes search_dirs to Loader via the constructor.
-rw-r--r-- | pystache/loader.py | 23 | ||||
-rw-r--r-- | pystache/renderer.py | 10 |
2 files changed, 21 insertions, 12 deletions
diff --git a/pystache/loader.py b/pystache/loader.py index e2a9029..670fcb7 100644 --- a/pystache/loader.py +++ b/pystache/loader.py @@ -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. @@ -44,6 +45,9 @@ class Loader(object): file_encoding: the name of the encoding to use when converting file 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 str to unicode. The function should have the signature: @@ -61,11 +65,16 @@ class Loader(object): if file_encoding is None: file_encoding = defaults.FILE_ENCODING + if search_dirs is None: + search_dirs = defaults.SEARCH_DIRS + if to_unicode is None: to_unicode = _to_unicode self.extension = extension self.file_encoding = file_encoding + # TODO: unit test setting this attribute. + self.search_dirs = search_dirs self.to_unicode = to_unicode def _make_locator(self): @@ -107,9 +116,8 @@ class Loader(object): return self.unicode(text, encoding) - # TODO: consider passing search_dirs in the constructor. # 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. @@ -122,13 +130,13 @@ class Loader(object): """ 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) - # TODO: consider passing search_dirs in the constructor. # 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. @@ -141,6 +149,7 @@ class Loader(object): """ 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) diff --git a/pystache/renderer.py b/pystache/renderer.py index b67415e..7b72803 100644 --- a/pystache/renderer.py +++ b/pystache/renderer.py @@ -76,8 +76,8 @@ class Renderer(object): extension (i.e. to use extensionless template files). Defaults to the package default. - search_dirs: the list of directories in which to search for - templates when loading a template by name. If given a string, + search_dirs: the list of directories in which to search when + loading a template by name or file name. If given a string, the method interprets the string as a single directory. Defaults to the package default. @@ -167,7 +167,7 @@ class Renderer(object): """ 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): """ @@ -177,7 +177,7 @@ class Renderer(object): loader = self._make_loader() def load_template(template_name): - return loader.load_name(template_name, self.search_dirs) + return loader.load_name(template_name) return load_template @@ -250,7 +250,7 @@ class Renderer(object): """ loader = self._make_loader() - template = loader.load_object(obj, self.search_dirs) + template = loader.load_object(obj) context = [obj] + list(context) |