Commit 17d73cdf by Chris Jerdonek

Made context.resolve() a method on the ContextStack class.

parent 64e2fe30
...@@ -60,43 +60,6 @@ def _get_value(item, key): ...@@ -60,43 +60,6 @@ def _get_value(item, key):
return _NOT_FOUND return _NOT_FOUND
# TODO: add some unit tests for this.
def resolve(context_stack, name):
"""
Resolve a name against a context stack.
This function follows the rules outlined in the section of the spec
regarding tag interpolation.
Arguments:
context_stack: a ContextStack instance.
This function does not coerce the return value to a string.
"""
if name == '.':
return context_stack.top()
parts = name.split('.')
value = context_stack.get(parts[0], _NOT_FOUND)
for part in parts[1:]:
# TODO: use EAFP here instead.
# http://docs.python.org/glossary.html#term-eafp
if value is _NOT_FOUND:
break
value = _get_value(value, part)
# The spec says that if name resolution fails at any point, the result
# should be considered falsey, and should interpolate as the empty string.
if value is _NOT_FOUND:
return ''
return value
class ContextStack(object): class ContextStack(object):
""" """
...@@ -204,6 +167,42 @@ class ContextStack(object): ...@@ -204,6 +167,42 @@ class ContextStack(object):
return context return context
# TODO: add some unit tests for this.
def resolve(self, name):
"""
Resolve a name against a context stack.
This function follows the rules outlined in the section of the spec
regarding tag interpolation.
Arguments:
context_stack: a ContextStack instance.
This function does not coerce the return value to a string.
"""
if name == '.':
return self.top()
parts = name.split('.')
value = self.get(parts[0], _NOT_FOUND)
for part in parts[1:]:
# TODO: use EAFP here instead.
# http://docs.python.org/glossary.html#term-eafp
if value is _NOT_FOUND:
break
value = _get_value(value, part)
# The spec says that if name resolution fails at any point, the result
# should be considered falsey, and should interpolate as the empty string.
if value is _NOT_FOUND:
return ''
return value
def get(self, key, default=None): def get(self, key, default=None):
""" """
Query the stack for the given key, and return the resulting value. Query the stack for the given key, and return the resulting value.
......
...@@ -7,7 +7,6 @@ Defines a class responsible for rendering logic. ...@@ -7,7 +7,6 @@ Defines a class responsible for rendering logic.
import re import re
from pystache.context import resolve
from pystache.parser import Parser from pystache.parser import Parser
...@@ -69,7 +68,7 @@ class RenderEngine(object): ...@@ -69,7 +68,7 @@ class RenderEngine(object):
Get a value from the given context as a basestring instance. Get a value from the given context as a basestring instance.
""" """
val = resolve(context, tag_name) val = context.resolve(tag_name)
if callable(val): if callable(val):
# According to the spec: # According to the spec:
......
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