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
0085bd9d
Commit
0085bd9d
authored
May 04, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Context.get() now raises KeyNotFoundError instead of returning default value.
parent
3f30290b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
30 deletions
+22
-30
pystache/context.py
+12
-16
pystache/renderengine.py
+5
-1
pystache/tests/test_context.py
+5
-13
No files found.
pystache/context.py
View file @
0085bd9d
...
...
@@ -200,7 +200,7 @@ class ContextStack(object):
# TODO: add more unit tests for this.
# TODO: update the docstring for dotted names.
def
get
(
self
,
name
,
default
=
u''
):
def
get
(
self
,
name
):
"""
Resolve a dotted name against the current context stack.
...
...
@@ -277,13 +277,12 @@ class ContextStack(object):
parts
=
name
.
split
(
'.'
)
result
=
self
.
_get_simple
(
parts
[
0
])
try
:
result
=
self
.
_get_simple
(
parts
[
0
])
except
KeyNotFoundError
:
raise
KeyNotFoundError
(
name
,
"first part"
)
for
part
in
parts
[
1
:]:
# TODO: consider using EAFP here instead.
# http://docs.python.org/glossary.html#term-eafp
if
result
is
_NOT_FOUND
:
break
# The full context stack is not used to resolve the remaining parts.
# From the spec--
#
...
...
@@ -295,9 +294,10 @@ class ContextStack(object):
#
# TODO: make sure we have a test case for the above point.
result
=
_get_value
(
result
,
part
)
if
result
is
_NOT_FOUND
:
return
default
# TODO: consider using EAFP here instead.
# http://docs.python.org/glossary.html#term-eafp
if
result
is
_NOT_FOUND
:
raise
KeyNotFoundError
(
name
,
"missing
%
s"
%
repr
(
part
))
return
result
...
...
@@ -306,16 +306,12 @@ class ContextStack(object):
Query the stack for a non-dotted name.
"""
result
=
_NOT_FOUND
for
item
in
reversed
(
self
.
_stack
):
result
=
_get_value
(
item
,
name
)
if
result
is
_NOT_FOUND
:
continue
# Otherwise, the key was found.
break
if
result
is
not
_NOT_FOUND
:
return
result
r
eturn
result
r
aise
KeyNotFoundError
(
name
,
"part missing"
)
def
push
(
self
,
item
):
"""
...
...
pystache/renderengine.py
View file @
0085bd9d
...
...
@@ -8,6 +8,7 @@ Defines a class responsible for rendering logic.
import
re
from
pystache.common
import
TemplateNotFoundError
from
pystache.context
import
KeyNotFoundError
from
pystache.parser
import
Parser
...
...
@@ -66,7 +67,10 @@ class RenderEngine(object):
self
.
load_partial
=
load_partial
def
resolve_context
(
self
,
stack
,
name
):
return
stack
.
get
(
name
)
try
:
return
stack
.
get
(
name
)
except
KeyNotFoundError
:
return
u''
def
resolve_partial
(
self
,
key
):
try
:
...
...
pystache/tests/test_context.py
View file @
0085bd9d
...
...
@@ -341,7 +341,7 @@ class ContextStackTestCase(unittest.TestCase, AssertIsMixin, AssertStringMixin,
"""
context
=
ContextStack
()
self
.
assertException
(
KeyNotFoundError
,
"Key '.' not found: empty context stack"
,
context
.
get
,
"."
,
"b"
)
self
.
assertException
(
KeyNotFoundError
,
"Key '.' not found: empty context stack"
,
context
.
get
,
"."
)
def
test_get__key_present
(
self
):
"""
...
...
@@ -357,15 +357,7 @@ class ContextStackTestCase(unittest.TestCase, AssertIsMixin, AssertStringMixin,
"""
context
=
ContextStack
()
self
.
assertString
(
context
.
get
(
"foo"
),
u''
)
def
test_get__default
(
self
):
"""
Test that get() respects the default value.
"""
context
=
ContextStack
()
self
.
assertEqual
(
context
.
get
(
"foo"
,
"bar"
),
"bar"
)
self
.
assertException
(
KeyNotFoundError
,
"Key 'foo' not found: first part"
,
context
.
get
,
"foo"
)
def
test_get__precedence
(
self
):
"""
...
...
@@ -461,10 +453,10 @@ class ContextStackTestCase(unittest.TestCase, AssertIsMixin, AssertStringMixin,
def
test_dot_notation__missing_attr_or_key
(
self
):
name
=
"foo.bar.baz.bak"
stack
=
ContextStack
({
"foo"
:
{
"bar"
:
{}}})
self
.
assert
String
(
stack
.
get
(
name
),
u''
)
self
.
assert
Exception
(
KeyNotFoundError
,
"Key 'foo.bar.baz.bak' not found: missing 'baz'"
,
stack
.
get
,
name
)
stack
=
ContextStack
({
"foo"
:
Attachable
(
bar
=
Attachable
())})
self
.
assert
String
(
stack
.
get
(
name
),
u''
)
self
.
assert
Exception
(
KeyNotFoundError
,
"Key 'foo.bar.baz.bak' not found: missing 'baz'"
,
stack
.
get
,
name
)
def
test_dot_notation__missing_part_terminates_search
(
self
):
"""
...
...
@@ -488,7 +480,7 @@ class ContextStackTestCase(unittest.TestCase, AssertIsMixin, AssertStringMixin,
"""
stack
=
ContextStack
({
'a'
:
{
'b'
:
'A.B'
}},
{
'a'
:
'A'
})
self
.
assertEqual
(
stack
.
get
(
'a'
),
'A'
)
self
.
assert
String
(
stack
.
get
(
'a.b'
),
u''
)
self
.
assert
Exception
(
KeyNotFoundError
,
"Key 'a.b' not found: missing 'b'"
,
stack
.
get
,
"a.b"
)
stack
.
pop
()
self
.
assertEqual
(
stack
.
get
(
'a.b'
),
'A.B'
)
...
...
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