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
17d73cdf
Commit
17d73cdf
authored
Apr 25, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made context.resolve() a method on the ContextStack class.
parent
64e2fe30
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
39 deletions
+37
-39
pystache/context.py
+36
-37
pystache/renderengine.py
+1
-2
No files found.
pystache/context.py
View file @
17d73cdf
...
...
@@ -60,43 +60,6 @@ def _get_value(item, key):
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
):
"""
...
...
@@ -204,6 +167,42 @@ class ContextStack(object):
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
):
"""
Query the stack for the given key, and return the resulting value.
...
...
pystache/renderengine.py
View file @
17d73cdf
...
...
@@ -7,7 +7,6 @@ Defines a class responsible for rendering logic.
import
re
from
pystache.context
import
resolve
from
pystache.parser
import
Parser
...
...
@@ -69,7 +68,7 @@ class RenderEngine(object):
Get a value from the given context as a basestring instance.
"""
val
=
resolve
(
context
,
tag_name
)
val
=
context
.
resolve
(
tag_name
)
if
callable
(
val
):
# According to the spec:
...
...
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