Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
eb736a1c
Commit
eb736a1c
authored
Dec 15, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a Context class with support for dictionaries (for issue #49).
parent
117e3530
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
0 deletions
+134
-0
pystache/context.py
+61
-0
tests/test_context.py
+73
-0
No files found.
pystache/context.py
0 → 100644
View file @
eb736a1c
# coding: utf-8
"""
Defines a Context class to represent mustache(5)'s notion of context.
"""
class
Context
(
object
):
"""
Encapsulates a queryable stack of zero or more dictionary-like objects.
Instances of this class are intended to act as the context when
rendering mustache templates in accordance with mustache(5).
"""
# We reserve keyword arguments for future options (e.g. a "strict=True"
# option for enabling a strict mode).
def
__init__
(
self
,
*
obj
):
"""
Construct an instance and initialize the stack.
The variable argument list *obj are the objects with which to
populate the initial stack. Objects in the argument list are added
to the stack in order so that, in particular, items at the end of
the argument list are queried first when querying the stack.
The objects should be dictionary-like in the following sense:
(1) They can be dictionaries or objects.
(2) If they implement __getitem__, a KeyError should be raised
if __getitem__ is called on a missing key.
"""
self
.
stack
=
list
(
obj
)
def
get
(
self
,
key
,
default
=
None
):
"""
Query the stack for the given key, and return the resulting value.
Querying for a key queries objects in the stack in order from
last-added objects to first (last in, first out).
Querying an item in the stack is done as follows:
(1) The __getitem__ method is attempted first, if it exists.
This method returns None if no item in the stack contains the key.
"""
for
obj
in
reversed
(
self
.
stack
):
try
:
return
obj
[
key
]
except
KeyError
:
pass
return
default
tests/test_context.py
0 → 100644
View file @
eb736a1c
# coding: utf-8
"""
Unit tests of context.py.
"""
import
unittest
from
pystache.context
import
Context
class
ContextTestCase
(
unittest
.
TestCase
):
"""
Test the Context class.
"""
def
test_init__no_elements
(
self
):
"""
Check that passing nothing to __init__() raises no exception.
"""
context
=
Context
()
def
test_init__no_elements
(
self
):
"""
Check that passing more than two items to __init__() raises no exception.
"""
context
=
Context
({},
{},
{})
def
test_get__missing_key
(
self
):
"""
Test getting a missing key.
"""
context
=
Context
()
self
.
assertTrue
(
context
.
get
(
"foo"
)
is
None
)
def
test_get__default
(
self
):
"""
Test that get() respects the default value .
"""
context
=
Context
()
self
.
assertEquals
(
context
.
get
(
"foo"
,
"bar"
),
"bar"
)
def
test_get__key_present
(
self
):
"""
Test get() with a key that is present.
"""
context
=
Context
({
"foo"
:
"bar"
})
self
.
assertEquals
(
context
.
get
(
"foo"
),
"bar"
)
def
test_get__precedence
(
self
):
"""
Test that get() respects the order of precedence (later items first).
"""
context
=
Context
({
"foo"
:
"bar"
},
{
"foo"
:
"buzz"
})
self
.
assertEquals
(
context
.
get
(
"foo"
),
"buzz"
)
def
test_get__fallback
(
self
):
"""
Check that first-added stack items are queried on context misses.
"""
context
=
Context
({
"fuzz"
:
"buzz"
},
{
"foo"
:
"bar"
})
self
.
assertEquals
(
context
.
get
(
"fuzz"
),
"buzz"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment