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
340c2991
Commit
340c2991
authored
Apr 25, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed Context to ContextStack.
parent
780cf29c
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
61 additions
and
61 deletions
+61
-61
pystache/context.py
+18
-18
pystache/parsed.py
+1
-1
pystache/renderengine.py
+2
-2
pystache/renderer.py
+3
-3
pystache/tests/test_context.py
+35
-35
pystache/tests/test_renderengine.py
+2
-2
No files found.
pystache/context.py
View file @
340c2991
# coding: utf-8
"""
Exposes a
context
class and functions to retrieve names from context.
Exposes a
ContextStack
class and functions to retrieve names from context.
"""
...
...
@@ -33,7 +33,7 @@ def _get_value(item, key):
Returns _NOT_FOUND if the key does not exist.
The Context.get() docstring documents this function's intended behavior.
The Context
Stack
.get() docstring documents this function's intended behavior.
"""
if
isinstance
(
item
,
dict
):
...
...
@@ -79,7 +79,7 @@ def resolve(context, name):
return
context
.
get
(
name
,
''
)
class
Context
(
object
):
class
Context
Stack
(
object
):
"""
Provides dictionary-like access to a stack of zero or more items.
...
...
@@ -94,7 +94,7 @@ class Context(object):
(last in, first out).
Caution: this class does not currently support recursive nesting in
that items in the stack cannot themselves be Context instances.
that items in the stack cannot themselves be Context
Stack
instances.
See the docstrings of the methods of this class for more details.
...
...
@@ -111,7 +111,7 @@ class Context(object):
stack in order so that, in particular, items at the end of
the argument list are queried first when querying the stack.
Caution: items should not themselves be Context instances, as
Caution: items should not themselves be Context
Stack
instances, as
recursive nesting does not behave as one might expect.
"""
...
...
@@ -123,9 +123,9 @@ class Context(object):
For example--
>>> context = Context({'alpha': 'abc'}, {'numeric': 123})
>>> context = Context
Stack
({'alpha': 'abc'}, {'numeric': 123})
>>> repr(context)
"Context({'alpha': 'abc'}, {'numeric': 123})"
"Context
Stack
({'alpha': 'abc'}, {'numeric': 123})"
"""
return
"
%
s
%
s"
%
(
self
.
__class__
.
__name__
,
tuple
(
self
.
_stack
))
...
...
@@ -133,18 +133,18 @@ class Context(object):
@staticmethod
def
create
(
*
context
,
**
kwargs
):
"""
Build a Context instance from a sequence of context-like items.
Build a Context
Stack
instance from a sequence of context-like items.
This factory-style method is more general than the Context class's
This factory-style method is more general than the Context
Stack
class's
constructor in that, unlike the constructor, the argument list
can itself contain Context instances.
can itself contain Context
Stack
instances.
Here is an example illustrating various aspects of this method:
>>> obj1 = {'animal': 'cat', 'vegetable': 'carrot', 'mineral': 'copper'}
>>> obj2 = Context({'vegetable': 'spinach', 'mineral': 'silver'})
>>> obj2 = Context
Stack
({'vegetable': 'spinach', 'mineral': 'silver'})
>>>
>>> context = Context.create(obj1, None, obj2, mineral='gold')
>>> context = Context
Stack
.create(obj1, None, obj2, mineral='gold')
>>>
>>> context.get('animal')
'cat'
...
...
@@ -155,7 +155,7 @@ class Context(object):
Arguments:
*context: zero or more dictionaries, Context instances, or objects
*context: zero or more dictionaries, Context
Stack
instances, or objects
with which to populate the initial context stack. None
arguments will be skipped. Items in the *context list are
added to the stack in order so that later items in the argument
...
...
@@ -171,12 +171,12 @@ class Context(object):
"""
items
=
context
context
=
Context
()
context
=
Context
Stack
()
for
item
in
items
:
if
item
is
None
:
continue
if
isinstance
(
item
,
Context
):
if
isinstance
(
item
,
Context
Stack
):
context
.
_stack
.
extend
(
item
.
_stack
)
else
:
context
.
push
(
item
)
...
...
@@ -245,9 +245,9 @@ class Context(object):
>>>
>>> dct['greet'] is obj.greet
True
>>> Context(dct).get('greet') #doctest: +ELLIPSIS
>>> Context
Stack
(dct).get('greet') #doctest: +ELLIPSIS
<function greet at 0x...>
>>> Context(obj).get('greet')
>>> Context
Stack
(obj).get('greet')
'Hi Bob!'
TODO: explain the rationale for this difference in treatment.
...
...
@@ -289,4 +289,4 @@ class Context(object):
Return a copy of this instance.
"""
return
Context
(
*
self
.
_stack
)
return
Context
Stack
(
*
self
.
_stack
)
pystache/parsed.py
View file @
340c2991
...
...
@@ -17,7 +17,7 @@ class ParsedTemplate(object):
parse_tree: a list, each element of which is either--
(1) a unicode string, or
(2) a "rendering" callable that accepts a Context instance
(2) a "rendering" callable that accepts a Context
Stack
instance
and returns a unicode string.
The possible rendering callables are the return values of the
...
...
pystache/renderengine.py
View file @
340c2991
...
...
@@ -215,7 +215,7 @@ class RenderEngine(object):
Arguments:
template: a template string of type unicode.
context: a Context instance.
context: a Context
Stack
instance.
"""
# We keep this type-check as an added check because this method is
...
...
@@ -238,7 +238,7 @@ class RenderEngine(object):
template: a template string of type unicode (but not a proper
subclass of unicode).
context: a Context instance.
context: a Context
Stack
instance.
"""
# Be strict but not too strict. In other words, accept str instead
...
...
pystache/renderer.py
View file @
340c2991
...
...
@@ -8,7 +8,7 @@ This module provides a Renderer class to render templates.
import
sys
from
pystache
import
defaults
from
pystache.context
import
Context
from
pystache.context
import
Context
Stack
from
pystache.loader
import
Loader
from
pystache.renderengine
import
RenderEngine
from
pystache.specloader
import
SpecLoader
...
...
@@ -277,7 +277,7 @@ class Renderer(object):
# RenderEngine.render() requires that the template string be unicode.
template
=
self
.
_to_unicode_hard
(
template
)
context
=
Context
.
create
(
*
context
,
**
kwargs
)
context
=
Context
Stack
.
create
(
*
context
,
**
kwargs
)
self
.
_context
=
context
engine
=
self
.
_make_render_engine
()
...
...
@@ -338,7 +338,7 @@ class Renderer(object):
uses the passed object as the first element of the context stack
when rendering.
*context: zero or more dictionaries, Context instances, or objects
*context: zero or more dictionaries, Context
Stack
instances, or objects
with which to populate the initial context stack. None
arguments are skipped. Items in the *context list are added to
the context stack in order so that later items in the argument
...
...
pystache/tests/test_context.py
View file @
340c2991
...
...
@@ -10,7 +10,7 @@ import unittest
from
pystache.context
import
_NOT_FOUND
from
pystache.context
import
_get_value
from
pystache.context
import
Context
from
pystache.context
import
Context
Stack
from
pystache.tests.common
import
AssertIsMixin
class
SimpleObject
(
object
):
...
...
@@ -204,10 +204,10 @@ class GetValueTests(unittest.TestCase, AssertIsMixin):
self
.
assertNotFound
(
item2
,
'pop'
)
class
ContextTests
(
unittest
.
TestCase
,
AssertIsMixin
):
class
Context
Stack
Tests
(
unittest
.
TestCase
,
AssertIsMixin
):
"""
Test the Context class.
Test the Context
Stack
class.
"""
...
...
@@ -216,34 +216,34 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Check that passing nothing to __init__() raises no exception.
"""
context
=
Context
()
context
=
Context
Stack
()
def
test_init__many_elements
(
self
):
"""
Check that passing more than two items to __init__() raises no exception.
"""
context
=
Context
({},
{},
{})
context
=
Context
Stack
({},
{},
{})
def
test__repr
(
self
):
context
=
Context
()
self
.
assertEqual
(
repr
(
context
),
'Context()'
)
context
=
Context
Stack
()
self
.
assertEqual
(
repr
(
context
),
'Context
Stack
()'
)
context
=
Context
({
'foo'
:
'bar'
})
self
.
assertEqual
(
repr
(
context
),
"Context({'foo': 'bar'},)"
)
context
=
Context
Stack
({
'foo'
:
'bar'
})
self
.
assertEqual
(
repr
(
context
),
"Context
Stack
({'foo': 'bar'},)"
)
context
=
Context
({
'foo'
:
'bar'
},
{
'abc'
:
123
})
self
.
assertEqual
(
repr
(
context
),
"Context({'foo': 'bar'}, {'abc': 123})"
)
context
=
Context
Stack
({
'foo'
:
'bar'
},
{
'abc'
:
123
})
self
.
assertEqual
(
repr
(
context
),
"Context
Stack
({'foo': 'bar'}, {'abc': 123})"
)
def
test__str
(
self
):
context
=
Context
()
self
.
assertEqual
(
str
(
context
),
'Context()'
)
context
=
Context
Stack
()
self
.
assertEqual
(
str
(
context
),
'Context
Stack
()'
)
context
=
Context
({
'foo'
:
'bar'
})
self
.
assertEqual
(
str
(
context
),
"Context({'foo': 'bar'},)"
)
context
=
Context
Stack
({
'foo'
:
'bar'
})
self
.
assertEqual
(
str
(
context
),
"Context
Stack
({'foo': 'bar'},)"
)
context
=
Context
({
'foo'
:
'bar'
},
{
'abc'
:
123
})
self
.
assertEqual
(
str
(
context
),
"Context({'foo': 'bar'}, {'abc': 123})"
)
context
=
Context
Stack
({
'foo'
:
'bar'
},
{
'abc'
:
123
})
self
.
assertEqual
(
str
(
context
),
"Context
Stack
({'foo': 'bar'}, {'abc': 123})"
)
## Test the static create() method.
...
...
@@ -252,7 +252,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test passing a dictionary.
"""
context
=
Context
.
create
({
'foo'
:
'bar'
})
context
=
Context
Stack
.
create
({
'foo'
:
'bar'
})
self
.
assertEqual
(
context
.
get
(
'foo'
),
'bar'
)
def
test_create__none
(
self
):
...
...
@@ -260,7 +260,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test passing None.
"""
context
=
Context
.
create
({
'foo'
:
'bar'
},
None
)
context
=
Context
Stack
.
create
({
'foo'
:
'bar'
},
None
)
self
.
assertEqual
(
context
.
get
(
'foo'
),
'bar'
)
def
test_create__object
(
self
):
...
...
@@ -270,16 +270,16 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
"""
class
Foo
(
object
):
foo
=
'bar'
context
=
Context
.
create
(
Foo
())
context
=
Context
Stack
.
create
(
Foo
())
self
.
assertEqual
(
context
.
get
(
'foo'
),
'bar'
)
def
test_create__context
(
self
):
"""
Test passing a Context instance.
Test passing a Context
Stack
instance.
"""
obj
=
Context
({
'foo'
:
'bar'
})
context
=
Context
.
create
(
obj
)
obj
=
Context
Stack
({
'foo'
:
'bar'
})
context
=
Context
Stack
.
create
(
obj
)
self
.
assertEqual
(
context
.
get
(
'foo'
),
'bar'
)
def
test_create__kwarg
(
self
):
...
...
@@ -287,7 +287,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test passing a keyword argument.
"""
context
=
Context
.
create
(
foo
=
'bar'
)
context
=
Context
Stack
.
create
(
foo
=
'bar'
)
self
.
assertEqual
(
context
.
get
(
'foo'
),
'bar'
)
def
test_create__precedence_positional
(
self
):
...
...
@@ -295,7 +295,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test precedence of positional arguments.
"""
context
=
Context
.
create
({
'foo'
:
'bar'
},
{
'foo'
:
'buzz'
})
context
=
Context
Stack
.
create
({
'foo'
:
'bar'
},
{
'foo'
:
'buzz'
})
self
.
assertEqual
(
context
.
get
(
'foo'
),
'buzz'
)
def
test_create__precedence_keyword
(
self
):
...
...
@@ -303,7 +303,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test precedence of keyword arguments.
"""
context
=
Context
.
create
({
'foo'
:
'bar'
},
foo
=
'buzz'
)
context
=
Context
Stack
.
create
({
'foo'
:
'bar'
},
foo
=
'buzz'
)
self
.
assertEqual
(
context
.
get
(
'foo'
),
'buzz'
)
def
test_get__key_present
(
self
):
...
...
@@ -311,7 +311,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test getting a key.
"""
context
=
Context
({
"foo"
:
"bar"
})
context
=
Context
Stack
({
"foo"
:
"bar"
})
self
.
assertEqual
(
context
.
get
(
"foo"
),
"bar"
)
def
test_get__key_missing
(
self
):
...
...
@@ -319,7 +319,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test getting a missing key.
"""
context
=
Context
()
context
=
Context
Stack
()
self
.
assertTrue
(
context
.
get
(
"foo"
)
is
None
)
def
test_get__default
(
self
):
...
...
@@ -327,7 +327,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test that get() respects the default value.
"""
context
=
Context
()
context
=
Context
Stack
()
self
.
assertEqual
(
context
.
get
(
"foo"
,
"bar"
),
"bar"
)
def
test_get__precedence
(
self
):
...
...
@@ -335,7 +335,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Test that get() respects the order of precedence (later items first).
"""
context
=
Context
({
"foo"
:
"bar"
},
{
"foo"
:
"buzz"
})
context
=
Context
Stack
({
"foo"
:
"bar"
},
{
"foo"
:
"buzz"
})
self
.
assertEqual
(
context
.
get
(
"foo"
),
"buzz"
)
def
test_get__fallback
(
self
):
...
...
@@ -343,7 +343,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
Check that first-added stack items are queried on context misses.
"""
context
=
Context
({
"fuzz"
:
"buzz"
},
{
"foo"
:
"bar"
})
context
=
Context
Stack
({
"fuzz"
:
"buzz"
},
{
"foo"
:
"bar"
})
self
.
assertEqual
(
context
.
get
(
"fuzz"
),
"buzz"
)
def
test_push
(
self
):
...
...
@@ -352,7 +352,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
"""
key
=
"foo"
context
=
Context
({
key
:
"bar"
})
context
=
Context
Stack
({
key
:
"bar"
})
self
.
assertEqual
(
context
.
get
(
key
),
"bar"
)
context
.
push
({
key
:
"buzz"
})
...
...
@@ -364,7 +364,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
"""
key
=
"foo"
context
=
Context
({
key
:
"bar"
},
{
key
:
"buzz"
})
context
=
Context
Stack
({
key
:
"bar"
},
{
key
:
"buzz"
})
self
.
assertEqual
(
context
.
get
(
key
),
"buzz"
)
item
=
context
.
pop
()
...
...
@@ -373,7 +373,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
def
test_top
(
self
):
key
=
"foo"
context
=
Context
({
key
:
"bar"
},
{
key
:
"buzz"
})
context
=
Context
Stack
({
key
:
"bar"
},
{
key
:
"buzz"
})
self
.
assertEqual
(
context
.
get
(
key
),
"buzz"
)
top
=
context
.
top
()
...
...
@@ -383,7 +383,7 @@ class ContextTests(unittest.TestCase, AssertIsMixin):
def
test_copy
(
self
):
key
=
"foo"
original
=
Context
({
key
:
"bar"
},
{
key
:
"buzz"
})
original
=
Context
Stack
({
key
:
"bar"
},
{
key
:
"buzz"
})
self
.
assertEqual
(
original
.
get
(
key
),
"buzz"
)
new
=
original
.
copy
()
...
...
pystache/tests/test_renderengine.py
View file @
340c2991
...
...
@@ -7,7 +7,7 @@ Unit tests of renderengine.py.
import
unittest
from
pystache.context
import
Context
from
pystache.context
import
Context
Stack
from
pystache
import
defaults
from
pystache.parser
import
ParsingError
from
pystache.renderengine
import
RenderEngine
...
...
@@ -83,7 +83,7 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
if
partials
is
not
None
:
engine
.
load_partial
=
lambda
key
:
unicode
(
partials
[
key
])
context
=
Context
(
*
context
)
context
=
Context
Stack
(
*
context
)
actual
=
engine
.
render
(
template
,
context
)
...
...
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