Commit 383acd31 by Chris Jerdonek

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

parent cf4d98ef
...@@ -30,7 +30,6 @@ class Locator(object): ...@@ -30,7 +30,6 @@ class Locator(object):
self.template_extension = extension self.template_extension = extension
def _find_path(self, file_name, search_dirs): def _find_path(self, file_name, search_dirs):
""" """
Search for the given file, and return the path. Search for the given file, and return the path.
...@@ -45,6 +44,20 @@ class Locator(object): ...@@ -45,6 +44,20 @@ class Locator(object):
return None 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): def make_file_name(self, template_name):
file_name = template_name file_name = template_name
if self.template_extension is not False: if self.template_extension is not False:
...@@ -80,12 +93,6 @@ class Locator(object): ...@@ -80,12 +93,6 @@ class Locator(object):
""" """
Find and return the path to the template with the given name. 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) file_name = self.make_file_name(template_name)
path = self._find_path(file_name, search_dirs) path = self._find_path(file_name, search_dirs)
......
...@@ -259,17 +259,18 @@ class Renderer(object): ...@@ -259,17 +259,18 @@ class Renderer(object):
""" """
Find and return the template associated with an 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: locator = self.make_locator()
#
# (1) call self.locator.make_template_name(obj) template_name = locator.make_template_name(obj)
# (2) call self.locator.get_director(obj) directory = locator.get_object_directory(obj)
# (3) call self.locator.locate_path() with template_name argument search_dirs = [directory] + self.search_dirs
# and enlarged search_dirs. path = locator.locate_path(template_name=template_name, search_dirs=search_dirs)
# (4) call self.read(), and return the result.
raise NotImplementedError() return self.read(path)
def _render_string(self, template, *context, **kwargs): def _render_string(self, template, *context, **kwargs):
""" """
......
Hello {{to}} Hello, {{to}}
\ No newline at end of file \ 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): ...@@ -33,6 +33,16 @@ class LocatorTests(unittest.TestCase):
locator = Locator(extension=False) locator = Locator(extension=False)
self.assertTrue(locator.template_extension is 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): def test_make_file_name(self):
locator = Locator() locator = Locator()
......
...@@ -15,6 +15,8 @@ from pystache.renderer import Renderer ...@@ -15,6 +15,8 @@ from pystache.renderer import Renderer
from pystache.locator import Locator from pystache.locator import Locator
from .common import get_data_path from .common import get_data_path
from .data.templates import SayHello
class RendererInitTestCase(unittest.TestCase): class RendererInitTestCase(unittest.TestCase):
...@@ -375,9 +377,22 @@ class RendererTestCase(unittest.TestCase): ...@@ -375,9 +377,22 @@ class RendererTestCase(unittest.TestCase):
""" """
renderer = Renderer() renderer = Renderer()
path = get_data_path('say_hello.mustache') path = get_data_path('say_hello.mustache')
actual = renderer.render_path(path, to='world') actual = renderer.render_path(path, to='foo')
self.assertEquals(actual, "Hello world") 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, # By testing that Renderer.render() constructs the right RenderEngine,
# we no longer need to exercise all rendering code paths through # 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