Commit ed2c5521 by Chris Jerdonek

Divided Loader.load() into load_name() and load_object().

parent 80dd7698
...@@ -50,6 +50,7 @@ class Loader(object): ...@@ -50,6 +50,7 @@ class Loader(object):
self.encoding = encoding self.encoding = encoding
self.extension = extension self.extension = extension
# TODO: eliminate redundancy with the Renderer class's unicode code.
def unicode(self, s, encoding=None): def unicode(self, s, encoding=None):
""" """
Call Python's built-in function unicode(), and return the result. Call Python's built-in function unicode(), and return the result.
...@@ -80,25 +81,38 @@ class Loader(object): ...@@ -80,25 +81,38 @@ class Loader(object):
return self.unicode(text, encoding) 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. Find and return the template associated to the given object.
Arguments: Arguments:
obj: a string or object instance. If obj is a string, then obj obj: an instance of a user-defined class.
will be interpreted as the template name. Otherwise, obj will
be interpreted as an instance of a user-defined class.
search_dirs: the list of directories in which to search for search_dirs: the list of directories in which to search.
templates when loading a template by name.
""" """
locator = Locator(extension=self.extension) locator = Locator(extension=self.extension)
if isinstance(obj, basestring): path = locator.find_path_by_object(search_dirs, obj)
path = locator.find_path_by_name(search_dirs, obj)
else:
path = locator.find_path_by_object(search_dirs, obj)
return self.read(path) return self.read(path)
...@@ -185,7 +185,7 @@ class Renderer(object): ...@@ -185,7 +185,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(template_name, self.search_dirs) return loader.load_name(template_name, self.search_dirs)
return load_template return load_template
...@@ -258,7 +258,7 @@ class Renderer(object): ...@@ -258,7 +258,7 @@ class Renderer(object):
""" """
loader = self._make_loader() loader = self._make_loader()
template = loader.load(obj, self.search_dirs) template = loader.load_object(obj, self.search_dirs)
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