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
d22cc3b0
Commit
d22cc3b0
authored
Mar 29, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed the reader argument from CustomLoader's constructor.
parent
cab8528f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
48 deletions
+43
-48
pystache/custom_template.py
+18
-16
tests/test_custom_template.py
+25
-32
No files found.
pystache/custom_template.py
View file @
d22cc3b0
...
@@ -9,7 +9,7 @@ import os.path
...
@@ -9,7 +9,7 @@ import os.path
from
.context
import
Context
from
.context
import
Context
from
.loader
import
Loader
from
.loader
import
Loader
from
.locator
import
Locator
as
TemplateLocator
from
.locator
import
Locator
from
.renderer
import
Renderer
from
.renderer
import
Renderer
...
@@ -56,7 +56,7 @@ class View(CustomizedTemplate):
...
@@ -56,7 +56,7 @@ class View(CustomizedTemplate):
_renderer
=
None
_renderer
=
None
locator
=
Template
Locator
()
locator
=
Locator
()
def
__init__
(
self
,
context
=
None
):
def
__init__
(
self
,
context
=
None
):
"""
"""
...
@@ -127,18 +127,14 @@ class CustomLoader(object):
...
@@ -127,18 +127,14 @@ class CustomLoader(object):
"""
"""
def
__init__
(
self
,
search_dirs
=
None
,
locator
=
None
,
reader
=
None
):
def
__init__
(
self
,
search_dirs
=
None
,
loader
=
None
):
if
locator
is
None
:
if
loader
is
None
:
locator
=
TemplateLocator
()
loader
=
Loader
()
if
reader
is
None
:
reader
=
Loader
()
if
search_dirs
is
None
:
if
search_dirs
is
None
:
search_dirs
=
[]
search_dirs
=
[]
self
.
locator
=
locator
self
.
loader
=
loader
self
.
reader
=
reader
self
.
search_dirs
=
search_dirs
self
.
search_dirs
=
search_dirs
# TODO: make this private.
# TODO: make this private.
...
@@ -154,10 +150,13 @@ class CustomLoader(object):
...
@@ -154,10 +150,13 @@ class CustomLoader(object):
# Otherwise, we don't know the directory.
# Otherwise, we don't know the directory.
# TODO: share code with the loader attribute here.
locator
=
Locator
(
extension
=
self
.
loader
.
extension
)
template_name
=
(
view
.
template_name
if
view
.
template_name
is
not
None
else
template_name
=
(
view
.
template_name
if
view
.
template_name
is
not
None
else
self
.
locator
.
make_template_name
(
view
))
locator
.
make_template_name
(
view
))
file_name
=
self
.
locator
.
make_file_name
(
template_name
,
view
.
template_extension
)
file_name
=
locator
.
make_file_name
(
template_name
,
view
.
template_extension
)
return
(
template_dir
,
file_name
)
return
(
template_dir
,
file_name
)
...
@@ -169,11 +168,14 @@ class CustomLoader(object):
...
@@ -169,11 +168,14 @@ class CustomLoader(object):
"""
"""
dir_path
,
file_name
=
self
.
get_relative_template_location
(
view
)
dir_path
,
file_name
=
self
.
get_relative_template_location
(
view
)
# TODO: share code with the loader attribute here.
locator
=
Locator
(
extension
=
self
.
loader
.
extension
)
if
dir_path
is
None
:
if
dir_path
is
None
:
# Then we need to search for the path.
# Then we need to search for the path.
path
=
self
.
locator
.
find_path_by_object
(
self
.
search_dirs
,
view
,
file_name
=
file_name
)
path
=
locator
.
find_path_by_object
(
self
.
search_dirs
,
view
,
file_name
=
file_name
)
else
:
else
:
obj_dir
=
self
.
locator
.
get_object_directory
(
view
)
obj_dir
=
locator
.
get_object_directory
(
view
)
path
=
os
.
path
.
join
(
obj_dir
,
dir_path
,
file_name
)
path
=
os
.
path
.
join
(
obj_dir
,
dir_path
,
file_name
)
return
path
return
path
...
@@ -190,8 +192,8 @@ class CustomLoader(object):
...
@@ -190,8 +192,8 @@ class CustomLoader(object):
"""
"""
if
custom
.
template
is
not
None
:
if
custom
.
template
is
not
None
:
return
self
.
re
ader
.
unicode
(
custom
.
template
,
custom
.
template_encoding
)
return
self
.
lo
ader
.
unicode
(
custom
.
template
,
custom
.
template_encoding
)
path
=
self
.
get_template_path
(
custom
)
path
=
self
.
get_template_path
(
custom
)
return
self
.
re
ader
.
read
(
path
,
custom
.
template_encoding
)
return
self
.
lo
ader
.
read
(
path
,
custom
.
template_encoding
)
tests/test_custom_template.py
View file @
d22cc3b0
...
@@ -18,8 +18,7 @@ from pystache import Renderer
...
@@ -18,8 +18,7 @@ from pystache import Renderer
from
pystache
import
View
from
pystache
import
View
from
pystache.custom_template
import
CustomLoader
from
pystache.custom_template
import
CustomLoader
from
pystache.locator
import
Locator
from
pystache.locator
import
Locator
# TODO: remove this alias.
from
pystache.loader
import
Loader
from
pystache.loader
import
Loader
as
Reader
from
.common
import
AssertIsMixin
from
.common
import
AssertIsMixin
from
.common
import
AssertStringMixin
from
.common
import
AssertStringMixin
from
.common
import
DATA_DIR
from
.common
import
DATA_DIR
...
@@ -145,19 +144,15 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
...
@@ -145,19 +144,15 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
"""
"""
def
test_init__defaults
(
self
):
def
test_init__defaults
(
self
):
loader
=
CustomLoader
()
custom
=
CustomLoader
()
# Check the locator attribute.
locator
=
loader
.
locator
self
.
assertEquals
(
locator
.
template_extension
,
'mustache'
)
# Check the reader attribute.
# Check the reader attribute.
reader
=
loader
.
re
ader
loader
=
custom
.
lo
ader
self
.
assertEquals
(
re
ader
.
decode_errors
,
'strict'
)
self
.
assertEquals
(
lo
ader
.
decode_errors
,
'strict'
)
self
.
assertEquals
(
re
ader
.
encoding
,
sys
.
getdefaultencoding
())
self
.
assertEquals
(
lo
ader
.
encoding
,
sys
.
getdefaultencoding
())
# Check search_dirs.
# Check search_dirs.
self
.
assertEquals
(
loader
.
search_dirs
,
[])
self
.
assertEquals
(
custom
.
search_dirs
,
[])
def
test_init__search_dirs
(
self
):
def
test_init__search_dirs
(
self
):
search_dirs
=
[
'a'
,
'b'
]
search_dirs
=
[
'a'
,
'b'
]
...
@@ -165,18 +160,13 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
...
@@ -165,18 +160,13 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
self
.
assertEquals
(
loader
.
search_dirs
,
[
'a'
,
'b'
])
self
.
assertEquals
(
loader
.
search_dirs
,
[
'a'
,
'b'
])
def
test_init__reader
(
self
):
def
test_init__loader
(
self
):
reader
=
Reader
()
loader
=
Loader
()
loader
=
CustomLoader
([],
reader
=
reader
)
custom
=
CustomLoader
([],
loader
=
loader
)
self
.
assertIs
(
loader
.
reader
,
reader
)
def
test_init__locator
(
self
):
self
.
assertIs
(
custom
.
loader
,
loader
)
locator
=
Locator
()
loader
=
CustomLoader
([],
locator
=
locator
)
self
.
assertIs
(
loader
.
locator
,
locator
)
# TODO: rename to something like _assert_load().
def
_assert_template
(
self
,
loader
,
custom
,
expected
):
def
_assert_template
(
self
,
loader
,
custom
,
expected
):
self
.
assertString
(
loader
.
load
(
custom
),
expected
)
self
.
assertString
(
loader
.
load
(
custom
),
expected
)
...
@@ -223,7 +213,8 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
...
@@ -223,7 +213,8 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
custom
.
template_encoding
=
'utf-8'
custom
.
template_encoding
=
'utf-8'
self
.
_assert_template
(
CustomLoader
(),
custom
,
u'é'
)
self
.
_assert_template
(
CustomLoader
(),
custom
,
u'é'
)
def
test_load__template__correct_reader
(
self
):
# TODO: make this test complete.
def
test_load__template__correct_loader
(
self
):
"""
"""
Test that reader.unicode() is called correctly.
Test that reader.unicode() is called correctly.
...
@@ -233,28 +224,30 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
...
@@ -233,28 +224,30 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
being implemented correctly (and tested).
being implemented correctly (and tested).
"""
"""
class
TestReader
(
Re
ader
):
class
MockLoader
(
Lo
ader
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
s
=
None
self
.
s
=
None
self
.
encoding
=
None
self
.
encoding
=
None
# Overrides the existing method.
def
unicode
(
self
,
s
,
encoding
=
None
):
def
unicode
(
self
,
s
,
encoding
=
None
):
self
.
s
=
s
self
.
s
=
s
self
.
encoding
=
encoding
self
.
encoding
=
encoding
return
u"foo"
return
u"foo"
reader
=
TestRe
ader
()
loader
=
MockLo
ader
()
loader
=
CustomLoader
()
custom_
loader
=
CustomLoader
()
loader
.
reader
=
re
ader
custom_loader
.
loader
=
lo
ader
custom
=
Template
()
view
=
Template
()
custom
.
template
=
"template-foo"
view
.
template
=
"template-foo"
custom
.
template_encoding
=
"encoding-foo"
view
.
template_encoding
=
"encoding-foo"
self
.
_assert_template
(
loader
,
custom
,
u'foo'
)
# Check that our unicode() above was called.
self
.
assertEquals
(
reader
.
s
,
"template-foo"
)
self
.
_assert_template
(
custom_loader
,
view
,
u'foo'
)
self
.
assertEquals
(
reader
.
encoding
,
"encoding-foo"
)
self
.
assertEquals
(
loader
.
s
,
"template-foo"
)
self
.
assertEquals
(
loader
.
encoding
,
"encoding-foo"
)
# TODO: migrate these tests into the CustomLoaderTests class.
# TODO: migrate these tests into the CustomLoaderTests class.
...
...
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