Dot notation support, thereby making pystache compliant with Mustache spec…

Dot notation support, thereby making pystache compliant with Mustache spec v1.1.2 (and updating the spec submodule accordingly)
parent 59995926
Subproject commit 48c933b0bb780875acbfd15816297e263c53d6f7 Subproject commit bf6288ed6bd0ce8ccea6f1dac070b3d779132c3b
...@@ -27,13 +27,17 @@ def _get_value(item, key): ...@@ -27,13 +27,17 @@ def _get_value(item, key):
The Context.get() docstring documents this function's intended behavior. The Context.get() docstring documents this function's intended behavior.
""" """
parts = key.split('.')
key = parts[0]
value = _NOT_FOUND
if isinstance(item, dict): if isinstance(item, dict):
# Then we consider the argument a "hash" for the purposes of the spec. # Then we consider the argument a "hash" for the purposes of the spec.
# #
# We do a membership test to avoid using exceptions for flow control # We do a membership test to avoid using exceptions for flow control
# (e.g. catching KeyError). # (e.g. catching KeyError).
if key in item: if key in item:
return item[key] value = item[key]
elif type(item).__module__ != '__builtin__': elif type(item).__module__ != '__builtin__':
# Then we consider the argument an "object" for the purposes of # Then we consider the argument an "object" for the purposes of
# the spec. # the spec.
...@@ -45,10 +49,15 @@ def _get_value(item, key): ...@@ -45,10 +49,15 @@ def _get_value(item, key):
if hasattr(item, key): if hasattr(item, key):
attr = getattr(item, key) attr = getattr(item, key)
if _is_callable(attr): if _is_callable(attr):
return attr() value = attr()
return attr else:
value = attr
for part in parts[1:]:
if value is not _NOT_FOUND:
value = _get_value(value, part)
return _NOT_FOUND return value
class Context(object): class Context(object):
......
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