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
b6c4b4af
Commit
b6c4b4af
authored
May 04, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
RenderEngine constructor now accepts resolve_partial() instead of load_partial().
parent
0085bd9d
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
85 additions
and
57 deletions
+85
-57
pystache/renderengine.py
+5
-13
pystache/renderer.py
+20
-11
pystache/tests/test_renderengine.py
+6
-6
pystache/tests/test_renderer.py
+54
-27
No files found.
pystache/renderengine.py
View file @
b6c4b4af
...
...
@@ -7,7 +7,6 @@ Defines a class responsible for rendering logic.
import
re
from
pystache.common
import
TemplateNotFoundError
from
pystache.context
import
KeyNotFoundError
from
pystache.parser
import
Parser
...
...
@@ -31,14 +30,13 @@ class RenderEngine(object):
"""
def
__init__
(
self
,
load
_partial
=
None
,
literal
=
None
,
escape
=
None
):
def
__init__
(
self
,
resolve
_partial
=
None
,
literal
=
None
,
escape
=
None
):
"""
Arguments:
load_partial: the function to call when loading a partial. The
function should accept a string template name and return a
template string of type unicode (not a subclass). If the
template is not found, it should raise a TemplateNotFoundError.
resolve_partial: the function to call when loading a partial.
The function should accept a string template name and return a
template string of type unicode (not a subclass).
literal: the function used to convert unescaped variable tag
values to unicode, e.g. the value corresponding to a tag
...
...
@@ -64,7 +62,7 @@ class RenderEngine(object):
"""
self
.
escape
=
escape
self
.
literal
=
literal
self
.
load_partial
=
load
_partial
self
.
resolve_partial
=
resolve
_partial
def
resolve_context
(
self
,
stack
,
name
):
try
:
...
...
@@ -72,12 +70,6 @@ class RenderEngine(object):
except
KeyNotFoundError
:
return
u''
def
resolve_partial
(
self
,
key
):
try
:
return
self
.
load_partial
(
key
)
except
TemplateNotFoundError
:
return
u''
def
_get_string_value
(
self
,
context
,
tag_name
):
"""
Get a value from the given context as a basestring instance.
...
...
pystache/renderer.py
View file @
b6c4b4af
...
...
@@ -222,40 +222,49 @@ class Renderer(object):
return
load_template
def
_make_load_partial
(
self
):
# TODO: rename this to _make_resolve_partial().
def
_make_resolve_partial
(
self
):
"""
Return the
load
_partial function to pass to RenderEngine.__init__().
Return the
resolve
_partial function to pass to RenderEngine.__init__().
"""
if
self
.
partials
is
None
:
load_template
=
self
.
_make_load_template
()
return
load_template
def
resolve_partial
(
name
):
try
:
return
load_template
(
name
)
except
TemplateNotFoundError
:
return
u''
return
resolve_partial
# Otherwise, create a
load
_partial function from the custom partial
# Otherwise, create a
resolve
_partial function from the custom partial
# loader that satisfies RenderEngine requirements (and that provides
# a nicer exception, etc).
partials
=
self
.
partials
def
load
_partial
(
name
):
def
resolve
_partial
(
name
):
template
=
partials
.
get
(
name
)
if
template
is
None
:
raise
TemplateNotFoundError
(
"Name
%
s not found in partials:
%
s"
%
(
repr
(
name
),
type
(
partials
)))
return
u''
# if template is None:
# raise TemplateNotFoundError("Name %s not found in partials: %s" %
# (repr(name), type(partials)))
# RenderEngine requires that the return value be unicode.
return
self
.
_to_unicode_hard
(
template
)
return
load
_partial
return
resolve
_partial
def
_make_render_engine
(
self
):
"""
Return a RenderEngine instance for rendering.
"""
load_partial
=
self
.
_make_load
_partial
()
resolve_partial
=
self
.
_make_resolve
_partial
()
engine
=
RenderEngine
(
load_partial
=
load
_partial
,
engine
=
RenderEngine
(
resolve_partial
=
resolve
_partial
,
literal
=
self
.
_to_unicode_hard
,
escape
=
self
.
_escape_to_unicode
)
return
engine
...
...
pystache/tests/test_renderengine.py
View file @
b6c4b4af
...
...
@@ -45,11 +45,11 @@ class RenderEngineTestCase(unittest.TestCase):
"""
# In real-life, these arguments would be functions
engine
=
RenderEngine
(
load
_partial
=
"foo"
,
literal
=
"literal"
,
escape
=
"escape"
)
engine
=
RenderEngine
(
resolve
_partial
=
"foo"
,
literal
=
"literal"
,
escape
=
"escape"
)
self
.
assertEqual
(
engine
.
escape
,
"escape"
)
self
.
assertEqual
(
engine
.
literal
,
"literal"
)
self
.
assertEqual
(
engine
.
load
_partial
,
"foo"
)
self
.
assertEqual
(
engine
.
resolve
_partial
,
"foo"
)
class
RenderTests
(
unittest
.
TestCase
,
AssertStringMixin
):
...
...
@@ -69,7 +69,7 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
"""
escape
=
defaults
.
TAG_ESCAPE
engine
=
RenderEngine
(
literal
=
unicode
,
escape
=
escape
,
load
_partial
=
None
)
engine
=
RenderEngine
(
literal
=
unicode
,
escape
=
escape
,
resolve
_partial
=
None
)
return
engine
def
_assert_render
(
self
,
expected
,
template
,
*
context
,
**
kwargs
):
...
...
@@ -81,7 +81,7 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
engine
=
kwargs
.
get
(
'engine'
,
self
.
_engine
())
if
partials
is
not
None
:
engine
.
load
_partial
=
lambda
key
:
unicode
(
partials
[
key
])
engine
.
resolve
_partial
=
lambda
key
:
unicode
(
partials
[
key
])
context
=
ContextStack
(
*
context
)
...
...
@@ -92,14 +92,14 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
def
test_render
(
self
):
self
.
_assert_render
(
u'Hi Mom'
,
'Hi {{person}}'
,
{
'person'
:
'Mom'
})
def
test__
load
_partial
(
self
):
def
test__
resolve
_partial
(
self
):
"""
Test that render() uses the load_template attribute.
"""
engine
=
self
.
_engine
()
partials
=
{
'partial'
:
u"{{person}}"
}
engine
.
load
_partial
=
lambda
key
:
partials
[
key
]
engine
.
resolve
_partial
=
lambda
key
:
partials
[
key
]
self
.
_assert_render
(
u'Hi Mom'
,
'Hi {{>partial}}'
,
{
'person'
:
'Mom'
},
engine
=
engine
)
...
...
pystache/tests/test_renderer.py
View file @
b6c4b4af
...
...
@@ -319,37 +319,37 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
renderer
.
string_encoding
=
'utf_8'
self
.
assertEqual
(
renderer
.
render
(
template
),
u"déf"
)
def
test_make_
load
_partial
(
self
):
def
test_make_
resolve
_partial
(
self
):
"""
Test the _make_
load
_partial() method.
Test the _make_
resolve
_partial() method.
"""
renderer
=
Renderer
()
renderer
.
partials
=
{
'foo'
:
'bar'
}
load_partial
=
renderer
.
_make_load
_partial
()
resolve_partial
=
renderer
.
_make_resolve
_partial
()
actual
=
load
_partial
(
'foo'
)
actual
=
resolve
_partial
(
'foo'
)
self
.
assertEqual
(
actual
,
'bar'
)
self
.
assertEqual
(
type
(
actual
),
unicode
,
"RenderEngine requires that "
"
load
_partial return unicode strings."
)
"
resolve
_partial return unicode strings."
)
def
test_make_
load
_partial__unicode
(
self
):
def
test_make_
resolve
_partial__unicode
(
self
):
"""
Test _make_
load_partial(): that load
_partial doesn't "double-decode" Unicode.
Test _make_
resolve_partial(): that resolve
_partial doesn't "double-decode" Unicode.
"""
renderer
=
Renderer
()
renderer
.
partials
=
{
'partial'
:
'foo'
}
load_partial
=
renderer
.
_make_load
_partial
()
self
.
assertEqual
(
load
_partial
(
"partial"
),
"foo"
)
resolve_partial
=
renderer
.
_make_resolve
_partial
()
self
.
assertEqual
(
resolve
_partial
(
"partial"
),
"foo"
)
# Now with a value that is already unicode.
renderer
.
partials
=
{
'partial'
:
u'foo'
}
load_partial
=
renderer
.
_make_load
_partial
()
resolve_partial
=
renderer
.
_make_resolve
_partial
()
# If the next line failed, we would get the following error:
# TypeError: decoding Unicode is not supported
self
.
assertEqual
(
load
_partial
(
"partial"
),
"foo"
)
self
.
assertEqual
(
resolve
_partial
(
"partial"
),
"foo"
)
def
test_render_path
(
self
):
"""
...
...
@@ -406,7 +406,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
# we no longer need to exercise all rendering code paths through
# the Renderer. It suffices to test rendering paths through the
# RenderEngine for the same amount of code coverage.
class
Renderer_MakeRenderEngineTests
(
unittest
.
TestCase
,
AssertExceptionMixin
):
class
Renderer_MakeRenderEngineTests
(
unittest
.
TestCase
,
Assert
StringMixin
,
Assert
ExceptionMixin
):
"""
Check the RenderEngine returned by Renderer._make_render_engine().
...
...
@@ -420,11 +420,11 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertExceptionMixin):
"""
return
_make_renderer
()
## Test the engine's
load
_partial attribute.
## Test the engine's
resolve
_partial attribute.
def
test__
load
_partial__returns_unicode
(
self
):
def
test__
resolve
_partial__returns_unicode
(
self
):
"""
Check that
load
_partial returns unicode (and not a subclass).
Check that
resolve
_partial returns unicode (and not a subclass).
"""
class
MyUnicode
(
unicode
):
...
...
@@ -436,43 +436,70 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertExceptionMixin):
engine
=
renderer
.
_make_render_engine
()
actual
=
engine
.
load
_partial
(
'str'
)
actual
=
engine
.
resolve
_partial
(
'str'
)
self
.
assertEqual
(
actual
,
"foo"
)
self
.
assertEqual
(
type
(
actual
),
unicode
)
# Check that unicode subclasses are not preserved.
actual
=
engine
.
load
_partial
(
'subclass'
)
actual
=
engine
.
resolve
_partial
(
'subclass'
)
self
.
assertEqual
(
actual
,
"abc"
)
self
.
assertEqual
(
type
(
actual
),
unicode
)
def
test__
load
_partial__not_found__default
(
self
):
def
test__
resolve
_partial__not_found__default
(
self
):
"""
Check that
load_partial provides a nice message
when a template is not found.
Check that
resolve_partial returns the empty string
when a template is not found.
"""
renderer
=
Renderer
()
engine
=
renderer
.
_make_render_engine
()
load_partial
=
engine
.
load
_partial
resolve_partial
=
engine
.
resolve
_partial
self
.
assertException
(
TemplateNotFoundError
,
"File 'foo.mustache' not found in dirs: ['.']"
,
load_partial
,
"foo"
)
self
.
assertString
(
resolve_partial
(
'foo'
),
u''
)
def
test__load_partial__not_found__dict
(
self
):
# TODO: add this test case back when we add support to Renderer for strict mode.
# def test__resolve_partial__not_found__default(self):
# """
# Check that resolve_partial provides a nice message when a template is not found.
# """
# renderer = Renderer()
# engine = renderer._make_render_engine()
# resolve_partial = engine.resolve_partial
# self.assertException(TemplateNotFoundError, "File 'foo.mustache' not found in dirs: ['.']",
# resolve_partial, "foo")
def
test__resolve_partial__not_found__dict
(
self
):
"""
Check that
load_partial provides a nice message
when a template is not found.
Check that
resolve_partial returns the empty string
when a template is not found.
"""
renderer
=
Renderer
()
renderer
.
partials
=
{}
engine
=
renderer
.
_make_render_engine
()
load_partial
=
engine
.
load_partial
resolve_partial
=
engine
.
resolve_partial
self
.
assertString
(
resolve_partial
(
'foo'
),
u''
)
# TODO: add this test case back when we add support to Renderer for strict mode.
# def test__resolve_partial__not_found__dict(self):
# """
# Check that resolve_partial provides a nice message when a template is not found.
# """
# renderer = Renderer()
# renderer.partials = {}
# engine = renderer._make_render_engine()
# resolve_partial = engine.resolve_partial
# Include dict directly since str(dict) is different in Python 2 and 3:
# <type 'dict'> versus <class 'dict'>, respectively.
self
.
assertException
(
TemplateNotFoundError
,
"Name 'foo' not found in partials:
%
s"
%
dict
,
load
_partial
,
"foo"
)
#
self.assertException(TemplateNotFoundError, "Name 'foo' not found in partials: %s" % dict,
# resolve
_partial, "foo")
## Test the engine's literal attribute.
...
...
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