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
383acd31
Commit
383acd31
authored
Dec 29, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Addressed issue #70: "Support Renderer.render(object, context)"
parent
cf4d98ef
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
60 additions
and
20 deletions
+60
-20
pystache/locator.py
+14
-7
pystache/renderer.py
+10
-9
tests/data/__init__.py
+0
-0
tests/data/say_hello.mustache
+2
-2
tests/data/templates.py
+7
-0
tests/test_locator.py
+10
-0
tests/test_renderer.py
+17
-2
No files found.
pystache/locator.py
View file @
383acd31
...
...
@@ -30,7 +30,6 @@ class Locator(object):
self
.
template_extension
=
extension
def
_find_path
(
self
,
file_name
,
search_dirs
):
"""
Search for the given file, and return the path.
...
...
@@ -45,6 +44,20 @@ class Locator(object):
return
None
def
get_object_directory
(
self
,
obj
):
"""
Return the directory containing an object's defining class.
"""
module
=
sys
.
modules
[
obj
.
__module__
]
# TODO: should we handle the case of __file__ not existing, for
# example when using the interpreter or using a module in the
# standard library)?
path
=
module
.
__file__
return
os
.
path
.
dirname
(
path
)
def
make_file_name
(
self
,
template_name
):
file_name
=
template_name
if
self
.
template_extension
is
not
False
:
...
...
@@ -80,12 +93,6 @@ class Locator(object):
"""
Find and return the path to the template with the given name.
Raises an IOError if the template cannot be found.
Arguments:
search_dirs: the list of directories in which to search for templates.
"""
file_name
=
self
.
make_file_name
(
template_name
)
path
=
self
.
_find_path
(
file_name
,
search_dirs
)
...
...
pystache/renderer.py
View file @
383acd31
...
...
@@ -259,17 +259,18 @@ class Renderer(object):
"""
Find and return the template associated with an object.
TODO: document this.
The function first searches the directory containing the object's
class definition.
"""
# TODO: implement this as follows:
#
# (1) call self.
locator.make_template_name(obj)
# (2) call self.locator.get_director
(obj)
# (3) call self.locator.locate_path() with template_name argument
# and enlarged search_dirs.
# (4) call self.read(), and return the result.
r
aise
NotImplementedError
(
)
locator
=
self
.
make_locator
()
template_name
=
locator
.
make_template_name
(
obj
)
directory
=
locator
.
get_object_directory
(
obj
)
search_dirs
=
[
directory
]
+
self
.
search_dirs
path
=
locator
.
locate_path
(
template_name
=
template_name
,
search_dirs
=
search_dirs
)
r
eturn
self
.
read
(
path
)
def
_render_string
(
self
,
template
,
*
context
,
**
kwargs
):
"""
...
...
tests/data/__init__.py
0 → 100644
View file @
383acd31
tests/data/say_hello.mustache
View file @
383acd31
Hello
{{
to
}}
\ No newline at end of file
Hello,
{{
to
}}
\ No newline at end of file
tests/data/templates.py
0 → 100644
View file @
383acd31
# coding: utf-8
class
SayHello
(
object
):
def
to
(
self
):
return
"World"
tests/test_locator.py
View file @
383acd31
...
...
@@ -33,6 +33,16 @@ class LocatorTests(unittest.TestCase):
locator
=
Locator
(
extension
=
False
)
self
.
assertTrue
(
locator
.
template_extension
is
False
)
def
test_get_object_directory
(
self
):
locator
=
Locator
()
reader
=
Reader
()
actual
=
locator
.
get_object_directory
(
reader
)
expected
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
os
.
pardir
,
'pystache'
)
self
.
assertEquals
(
os
.
path
.
normpath
(
actual
),
os
.
path
.
normpath
(
expected
))
def
test_make_file_name
(
self
):
locator
=
Locator
()
...
...
tests/test_renderer.py
View file @
383acd31
...
...
@@ -15,6 +15,8 @@ from pystache.renderer import Renderer
from
pystache.locator
import
Locator
from
.common
import
get_data_path
from
.data.templates
import
SayHello
class
RendererInitTestCase
(
unittest
.
TestCase
):
...
...
@@ -375,9 +377,22 @@ class RendererTestCase(unittest.TestCase):
"""
renderer
=
Renderer
()
path
=
get_data_path
(
'say_hello.mustache'
)
actual
=
renderer
.
render_path
(
path
,
to
=
'world'
)
self
.
assertEquals
(
actual
,
"Hello world"
)
actual
=
renderer
.
render_path
(
path
,
to
=
'foo'
)
self
.
assertEquals
(
actual
,
"Hello, foo"
)
def
test_render__object
(
self
):
"""
Test rendering an object instance.
"""
renderer
=
Renderer
()
say_hello
=
SayHello
()
actual
=
renderer
.
render
(
say_hello
)
self
.
assertEquals
(
'Hello, World'
,
actual
)
actual
=
renderer
.
render
(
say_hello
,
to
=
'Mars'
)
self
.
assertEquals
(
'Hello, Mars'
,
actual
)
# By testing that Renderer.render() constructs the right RenderEngine,
# we no longer need to exercise all rendering code paths through
...
...
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