Commit e87e859f by Chris Jerdonek

Removed View class (finally!).

parent 49c1d7a9
......@@ -7,10 +7,9 @@ This module contains the initialization logic called by __init__.py.
from .renderer import Renderer
from .template_spec import TemplateSpec
from .view import View
__all__ = ['render', 'Renderer', 'View', 'TemplateSpec']
__all__ = ['render', 'Renderer', 'TemplateSpec']
def render(template, context=None, **kwargs):
......
......@@ -52,8 +52,7 @@ class TemplateSpec(object):
template_encoding = None
# TODO: add test cases for this class, and then refactor while replacing the
# View class.
# TODO: add test cases for this class.
class SpecLoader(object):
"""
......
# coding: utf-8
"""
This module exposes the deprecated View class.
TODO: remove this module.
"""
from .context import Context
from .locator import Locator
from .renderer import Renderer
from .template_spec import TemplateSpec
# TODO: remove this class.
class View(TemplateSpec):
_renderer = None
locator = Locator()
def __init__(self, context=None):
"""
Construct a View instance.
"""
context = Context.create(self, context)
self.context = context
def _get_renderer(self):
if self._renderer is None:
# We delay setting self._renderer until now (instead of, say,
# setting it in the constructor) in case the user changes after
# instantiation some of the attributes on which the Renderer
# depends. This lets users set the template_extension attribute,
# etc. after View.__init__() has already been called.
renderer = Renderer(file_encoding=self.template_encoding,
search_dirs=self.template_path,
file_extension=self.template_extension)
self._renderer = renderer
return self._renderer
def get_template(self):
"""
Return the current template after setting it, if necessary.
"""
if not self.template:
template_name = self._get_template_name()
renderer = self._get_renderer()
self.template = renderer.load_template(template_name)
return self.template
def _get_template_name(self):
"""
Return the name of the template to load.
If the template_name attribute is not set, then this method constructs
the template name from the class name as follows, for example:
TemplatePartial => template_partial
Otherwise, this method returns the template_name.
"""
if self.template_name:
return self.template_name
return self.locator.make_template_name(self)
def render(self):
"""
Return the view rendered using the current context.
"""
template = self.get_template()
renderer = self._get_renderer()
return renderer.render(template, self.context)
......@@ -14,9 +14,8 @@ from examples.simple import Simple
from examples.complex import Complex
from examples.lambdas import Lambdas
from examples.inverted import Inverted, InvertedLists
from pystache import TemplateSpec as Template
from pystache import TemplateSpec
from pystache import Renderer
from pystache import View
from pystache.template_spec import SpecLoader
from pystache.locator import Locator
from pystache.loader import Loader
......@@ -34,46 +33,38 @@ class Thing(object):
class ViewTestCase(unittest.TestCase, AssertStringMixin):
def test_init(self):
def test_template_rel_directory(self):
"""
Test the constructor.
Test that View.template_rel_directory is respected.
"""
class TestView(View):
template = "foo"
view = TestView()
self.assertEquals(view.template, "foo")
def test_template_path(self):
"""
Test that View.template_path is respected.
"""
class Tagless(View):
class Tagless(TemplateSpec):
pass
view = Tagless()
self.assertRaises(IOError, view.render)
renderer = Renderer()
view = Tagless()
view.template_path = "examples"
self.assertEquals(view.render(), "No tags...")
self.assertRaises(IOError, renderer.render, view)
view.template_rel_directory = "../examples"
actual = renderer.render(view)
self.assertEquals(actual, "No tags...")
def test_template_path_for_partials(self):
"""
Test that View.template_rel_path is respected for partials.
"""
class TestView(View):
template = "Partial: {{>tagless}}"
spec = TemplateSpec()
spec.template = "Partial: {{>tagless}}"
renderer1 = Renderer()
renderer2 = Renderer(search_dirs=EXAMPLES_DIR)
view = TestView()
self.assertRaises(IOError, view.render)
self.assertRaises(IOError, renderer1.render, spec)
view = TestView()
view.template_path = "examples"
self.assertEquals(view.render(), "Partial: No tags...")
actual = renderer2.render(spec)
self.assertEquals(actual, "Partial: No tags...")
def test_basic_method_calls(self):
renderer = Renderer()
......@@ -192,7 +183,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: str string.
"""
custom = Template()
custom = TemplateSpec()
custom.template = "abc"
self._assert_template(SpecLoader(), custom, u"abc")
......@@ -202,7 +193,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: unicode string.
"""
custom = Template()
custom = TemplateSpec()
custom.template = u"abc"
self._assert_template(SpecLoader(), custom, u"abc")
......@@ -212,7 +203,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: non-ascii unicode string.
"""
custom = Template()
custom = TemplateSpec()
custom.template = u"é"
self._assert_template(SpecLoader(), custom, u"é")
......@@ -222,7 +213,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: with template encoding attribute.
"""
custom = Template()
custom = TemplateSpec()
custom.template = u'é'.encode('utf-8')
self.assertRaises(UnicodeDecodeError, self._assert_template, SpecLoader(), custom, u'é')
......@@ -257,7 +248,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
custom_loader = SpecLoader()
custom_loader.loader = loader
view = Template()
view = TemplateSpec()
view.template = "template-foo"
view.template_encoding = "encoding-foo"
......
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