Commit 383acd31 by Chris Jerdonek

Addressed issue #70: "Support Renderer.render(object, context)"

parent cf4d98ef
......@@ -30,7 +30,6 @@ class Locator(object):
self.template_extension = extension
def _find_path(self, file_name, search_dirs):
"""
Search for the given file, and return the path.
......@@ -45,6 +44,20 @@ class Locator(object):
return None
def get_object_directory(self, obj):
"""
Return the directory containing an object's defining class.
"""
module = sys.modules[obj.__module__]
# TODO: should we handle the case of __file__ not existing, for
# example when using the interpreter or using a module in the
# standard library)?
path = module.__file__
return os.path.dirname(path)
def make_file_name(self, template_name):
file_name = template_name
if self.template_extension is not False:
......@@ -80,12 +93,6 @@ class Locator(object):
"""
Find and return the path to the template with the given name.
Raises an IOError if the template cannot be found.
Arguments:
search_dirs: the list of directories in which to search for templates.
"""
file_name = self.make_file_name(template_name)
path = self._find_path(file_name, search_dirs)
......
......@@ -259,17 +259,18 @@ class Renderer(object):
"""
Find and return the template associated with an object.
TODO: document this.
The function first searches the directory containing the object's
class definition.
"""
# TODO: implement this as follows:
#
# (1) call self.locator.make_template_name(obj)
# (2) call self.locator.get_director(obj)
# (3) call self.locator.locate_path() with template_name argument
# and enlarged search_dirs.
# (4) call self.read(), and return the result.
raise NotImplementedError()
locator = self.make_locator()
template_name = locator.make_template_name(obj)
directory = locator.get_object_directory(obj)
search_dirs = [directory] + self.search_dirs
path = locator.locate_path(template_name=template_name, search_dirs=search_dirs)
return self.read(path)
def _render_string(self, template, *context, **kwargs):
"""
......
Hello {{to}}
\ No newline at end of file
Hello, {{to}}
\ No newline at end of file
# coding: utf-8
class SayHello(object):
def to(self):
return "World"
......@@ -33,6 +33,16 @@ class LocatorTests(unittest.TestCase):
locator = Locator(extension=False)
self.assertTrue(locator.template_extension is False)
def test_get_object_directory(self):
locator = Locator()
reader = Reader()
actual = locator.get_object_directory(reader)
expected = os.path.join(os.path.dirname(__file__), os.pardir, 'pystache')
self.assertEquals(os.path.normpath(actual), os.path.normpath(expected))
def test_make_file_name(self):
locator = Locator()
......
......@@ -15,6 +15,8 @@ from pystache.renderer import Renderer
from pystache.locator import Locator
from .common import get_data_path
from .data.templates import SayHello
class RendererInitTestCase(unittest.TestCase):
......@@ -375,9 +377,22 @@ class RendererTestCase(unittest.TestCase):
"""
renderer = Renderer()
path = get_data_path('say_hello.mustache')
actual = renderer.render_path(path, to='world')
self.assertEquals(actual, "Hello world")
actual = renderer.render_path(path, to='foo')
self.assertEquals(actual, "Hello, foo")
def test_render__object(self):
"""
Test rendering an object instance.
"""
renderer = Renderer()
say_hello = SayHello()
actual = renderer.render(say_hello)
self.assertEquals('Hello, World', actual)
actual = renderer.render(say_hello, to='Mars')
self.assertEquals('Hello, Mars', actual)
# By testing that Renderer.render() constructs the right RenderEngine,
# we no longer need to exercise all rendering code paths through
......
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