Commit bcd4afe9 by Chris Jerdonek

View class now uses new Context class.

parent c689ce93
...@@ -158,12 +158,12 @@ class Template(object): ...@@ -158,12 +158,12 @@ class Template(object):
return template return template
def _render_dictionary(self, template, context): def _render_dictionary(self, template, context):
self.view.context_list.insert(0, context) self.view.context.push(context)
template = Template(template, self.view) template = Template(template, self.view)
out = template.render() out = template.render()
self.view.context_list.pop(0) self.view.context.pop()
return out return out
...@@ -186,7 +186,7 @@ class Template(object): ...@@ -186,7 +186,7 @@ class Template(object):
# See issue #34: https://github.com/defunkt/pystache/issues/34 # See issue #34: https://github.com/defunkt/pystache/issues/34
if not raw and raw != 0: if not raw and raw != 0:
if tag_name == '.': if tag_name == '.':
raw = self.view.context_list[0] raw = self.view.context.top()
else: else:
return '' return ''
......
...@@ -8,32 +8,11 @@ This module provides a View class. ...@@ -8,32 +8,11 @@ This module provides a View class.
import re import re
from types import UnboundMethodType from types import UnboundMethodType
from .context import Context
from .loader import Loader from .loader import Loader
from .template import Template from .template import Template
def get_or_attr(context_list, name, default=None):
"""
Find and return an attribute from the given context.
"""
if not context_list:
return default
for obj in context_list:
try:
return obj[name]
except KeyError:
pass
except:
try:
return getattr(obj, name)
except AttributeError:
pass
return default
class View(object): class View(object):
template_name = None template_name = None
...@@ -50,31 +29,19 @@ class View(object): ...@@ -50,31 +29,19 @@ class View(object):
Construct a View instance. Construct a View instance.
""" """
if context is None:
context = {}
if load_template is not None: if load_template is not None:
self._load_template = load_template self._load_template = load_template
if template is not None: if template is not None:
self.template = template self.template = template
context = context or {} _context = Context(self)
context.update(**kwargs) if context:
_context.push(context)
self.context_list = [context] if kwargs:
_context.push(kwargs)
def get(self, attr, default=None): self.context = _context
"""
Return the value for the given attribute.
"""
attr = get_or_attr(self.context_list, attr, getattr(self, attr, default))
if hasattr(attr, '__call__') and type(attr) is UnboundMethodType:
return attr()
else:
return attr
def load_template(self, template_name): def load_template(self, template_name):
if self._load_template is None: if self._load_template is None:
...@@ -121,13 +88,6 @@ class View(object): ...@@ -121,13 +88,6 @@ class View(object):
return re.sub('[A-Z]', repl, template_name)[1:] return re.sub('[A-Z]', repl, template_name)[1:]
def _get_context(self):
context = {}
for item in self.context_list:
if hasattr(item, 'keys') and hasattr(item, '__getitem__'):
context.update(item)
return context
def render(self, encoding=None): def render(self, encoding=None):
""" """
Return the view rendered using the current context. Return the view rendered using the current context.
...@@ -136,14 +96,8 @@ class View(object): ...@@ -136,14 +96,8 @@ class View(object):
template = Template(self.get_template(), self) template = Template(self.get_template(), self)
return template.render(encoding=encoding) return template.render(encoding=encoding)
def __contains__(self, needle): def get(self, key, default=None):
return needle in self.context or hasattr(self, needle) return self.context.get(key, default)
def __getattr__(self, attr):
if attr == 'context':
return self._get_context()
raise AttributeError("Attribute '%s' does not exist in View" % attr)
def __str__(self): def __str__(self):
return self.render() return self.render()
...@@ -173,12 +173,6 @@ class ViewTestCase(unittest.TestCase): ...@@ -173,12 +173,6 @@ class ViewTestCase(unittest.TestCase):
self.assertEquals(view.render(), 'derp') self.assertEquals(view.render(), 'derp')
def test_context_returns_a_flattened_dict(self):
view = Simple()
view.context_list = [{'one':'1'}, {'two':'2'}, object()]
self.assertEqual(view.context, {'one': '1', 'two': '2'})
def test_inverted_lists(self): def test_inverted_lists(self):
view = InvertedLists() view = InvertedLists()
self.assertEquals(view.render(), """one, two, three, empty list""") self.assertEquals(view.render(), """one, two, three, empty list""")
......
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