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. ...@@ -7,10 +7,9 @@ This module contains the initialization logic called by __init__.py.
from .renderer import Renderer from .renderer import Renderer
from .template_spec import TemplateSpec from .template_spec import TemplateSpec
from .view import View
__all__ = ['render', 'Renderer', 'View', 'TemplateSpec'] __all__ = ['render', 'Renderer', 'TemplateSpec']
def render(template, context=None, **kwargs): def render(template, context=None, **kwargs):
......
...@@ -52,8 +52,7 @@ class TemplateSpec(object): ...@@ -52,8 +52,7 @@ class TemplateSpec(object):
template_encoding = None template_encoding = None
# TODO: add test cases for this class, and then refactor while replacing the # TODO: add test cases for this class.
# View class.
class SpecLoader(object): 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 ...@@ -14,9 +14,8 @@ from examples.simple import Simple
from examples.complex import Complex from examples.complex import Complex
from examples.lambdas import Lambdas from examples.lambdas import Lambdas
from examples.inverted import Inverted, InvertedLists from examples.inverted import Inverted, InvertedLists
from pystache import TemplateSpec as Template from pystache import TemplateSpec
from pystache import Renderer from pystache import Renderer
from pystache import View
from pystache.template_spec import SpecLoader from pystache.template_spec import SpecLoader
from pystache.locator import Locator from pystache.locator import Locator
from pystache.loader import Loader from pystache.loader import Loader
...@@ -34,46 +33,38 @@ class Thing(object): ...@@ -34,46 +33,38 @@ class Thing(object):
class ViewTestCase(unittest.TestCase, AssertStringMixin): 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): class Tagless(TemplateSpec):
template = "foo"
view = TestView()
self.assertEquals(view.template, "foo")
def test_template_path(self):
"""
Test that View.template_path is respected.
"""
class Tagless(View):
pass pass
view = Tagless() view = Tagless()
self.assertRaises(IOError, view.render) renderer = Renderer()
view = Tagless() self.assertRaises(IOError, renderer.render, view)
view.template_path = "examples"
self.assertEquals(view.render(), "No tags...") view.template_rel_directory = "../examples"
actual = renderer.render(view)
self.assertEquals(actual, "No tags...")
def test_template_path_for_partials(self): def test_template_path_for_partials(self):
""" """
Test that View.template_rel_path is respected for partials. Test that View.template_rel_path is respected for partials.
""" """
class TestView(View): spec = TemplateSpec()
template = "Partial: {{>tagless}}" spec.template = "Partial: {{>tagless}}"
renderer1 = Renderer()
renderer2 = Renderer(search_dirs=EXAMPLES_DIR)
view = TestView() self.assertRaises(IOError, renderer1.render, spec)
self.assertRaises(IOError, view.render)
view = TestView() actual = renderer2.render(spec)
view.template_path = "examples" self.assertEquals(actual, "Partial: No tags...")
self.assertEquals(view.render(), "Partial: No tags...")
def test_basic_method_calls(self): def test_basic_method_calls(self):
renderer = Renderer() renderer = Renderer()
...@@ -192,7 +183,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): ...@@ -192,7 +183,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: str string. Test the template attribute: str string.
""" """
custom = Template() custom = TemplateSpec()
custom.template = "abc" custom.template = "abc"
self._assert_template(SpecLoader(), custom, u"abc") self._assert_template(SpecLoader(), custom, u"abc")
...@@ -202,7 +193,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): ...@@ -202,7 +193,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: unicode string. Test the template attribute: unicode string.
""" """
custom = Template() custom = TemplateSpec()
custom.template = u"abc" custom.template = u"abc"
self._assert_template(SpecLoader(), custom, u"abc") self._assert_template(SpecLoader(), custom, u"abc")
...@@ -212,7 +203,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): ...@@ -212,7 +203,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: non-ascii unicode string. Test the template attribute: non-ascii unicode string.
""" """
custom = Template() custom = TemplateSpec()
custom.template = u"é" custom.template = u"é"
self._assert_template(SpecLoader(), custom, u"é") self._assert_template(SpecLoader(), custom, u"é")
...@@ -222,7 +213,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): ...@@ -222,7 +213,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
Test the template attribute: with template encoding attribute. Test the template attribute: with template encoding attribute.
""" """
custom = Template() custom = TemplateSpec()
custom.template = u'é'.encode('utf-8') custom.template = u'é'.encode('utf-8')
self.assertRaises(UnicodeDecodeError, self._assert_template, SpecLoader(), custom, u'é') self.assertRaises(UnicodeDecodeError, self._assert_template, SpecLoader(), custom, u'é')
...@@ -257,7 +248,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): ...@@ -257,7 +248,7 @@ class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
custom_loader = SpecLoader() custom_loader = SpecLoader()
custom_loader.loader = loader custom_loader.loader = loader
view = Template() view = TemplateSpec()
view.template = "template-foo" view.template = "template-foo"
view.template_encoding = "encoding-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