summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-03-28 22:40:16 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-03-28 22:40:16 -0700
commited2c5521ec33bdad720d53dcaf41c1a19a276490 (patch)
tree01eb336b5282fc62fa99911305e1d6e28a201244
parent80dd7698985949c0bb9e7995bfb5389f0328531d (diff)
downloadpystache-ed2c5521ec33bdad720d53dcaf41c1a19a276490.tar.gz
Divided Loader.load() into load_name() and load_object().
-rw-r--r--pystache/loader.py34
-rw-r--r--pystache/renderer.py4
2 files changed, 26 insertions, 12 deletions
diff --git a/pystache/loader.py b/pystache/loader.py
index 706a2f7..fb30857 100644
--- a/pystache/loader.py
+++ b/pystache/loader.py
@@ -50,6 +50,7 @@ class Loader(object):
self.encoding = encoding
self.extension = extension
+ # TODO: eliminate redundancy with the Renderer class's unicode code.
def unicode(self, s, encoding=None):
"""
Call Python's built-in function unicode(), and return the result.
@@ -80,25 +81,38 @@ class Loader(object):
return self.unicode(text, encoding)
- def load(self, obj, search_dirs):
+ # TODO: unit-test this method.
+ def load_name(self, name, search_dirs):
+ """
+ Find and return the template with the given name.
+
+ Arguments:
+
+ name: the name of the template.
+
+ search_dirs: the list of directories in which to search.
+
+ """
+ locator = Locator(extension=self.extension)
+
+ path = locator.find_path_by_name(search_dirs, name)
+
+ return self.read(path)
+
+ # TODO: unit-test this method.
+ def load_object(self, obj, search_dirs):
"""
Find and return the template associated to the given object.
Arguments:
- obj: a string or object instance. If obj is a string, then obj
- will be interpreted as the template name. Otherwise, obj will
- be interpreted as an instance of a user-defined class.
+ obj: an instance of a user-defined class.
- search_dirs: the list of directories in which to search for
- templates when loading a template by name.
+ search_dirs: the list of directories in which to search.
"""
locator = Locator(extension=self.extension)
- if isinstance(obj, basestring):
- path = locator.find_path_by_name(search_dirs, obj)
- else:
- path = locator.find_path_by_object(search_dirs, obj)
+ path = locator.find_path_by_object(search_dirs, obj)
return self.read(path)
diff --git a/pystache/renderer.py b/pystache/renderer.py
index adbac3e..ffc3194 100644
--- a/pystache/renderer.py
+++ b/pystache/renderer.py
@@ -185,7 +185,7 @@ class Renderer(object):
loader = self._make_loader()
def load_template(template_name):
- return loader.load(template_name, self.search_dirs)
+ return loader.load_name(template_name, self.search_dirs)
return load_template
@@ -258,7 +258,7 @@ class Renderer(object):
"""
loader = self._make_loader()
- template = loader.load(obj, self.search_dirs)
+ template = loader.load_object(obj, self.search_dirs)
context = [obj] + list(context)