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
Show 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
from
.context
import
Context
from
.loader
import
Loader
from
.locator
import
Locator
as
TemplateLocator
from
.locator
import
Locator
from
.renderer
import
Renderer
...
...
@@ -56,7 +56,7 @@ class View(CustomizedTemplate):
_renderer
=
None
locator
=
Template
Locator
()
locator
=
Locator
()
def
__init__
(
self
,
context
=
None
):
"""
...
...
@@ -127,18 +127,14 @@ class CustomLoader(object):
"""
def
__init__
(
self
,
search_dirs
=
None
,
locator
=
None
,
reader
=
None
):
if
locator
is
None
:
locator
=
TemplateLocator
()
if
reader
is
None
:
reader
=
Loader
()
def
__init__
(
self
,
search_dirs
=
None
,
loader
=
None
):
if
loader
is
None
:
loader
=
Loader
()
if
search_dirs
is
None
:
search_dirs
=
[]
self
.
locator
=
locator
self
.
reader
=
reader
self
.
loader
=
loader
self
.
search_dirs
=
search_dirs
# TODO: make this private.
...
...
@@ -154,10 +150,13 @@ class CustomLoader(object):
# 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
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
)
...
...
@@ -169,11 +168,14 @@ class CustomLoader(object):
"""
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
:
# 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
:
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
)
return
path
...
...
@@ -190,8 +192,8 @@ class CustomLoader(object):
"""
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
)
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
from
pystache
import
View
from
pystache.custom_template
import
CustomLoader
from
pystache.locator
import
Locator
# TODO: remove this alias.
from
pystache.loader
import
Loader
as
Reader
from
pystache.loader
import
Loader
from
.common
import
AssertIsMixin
from
.common
import
AssertStringMixin
from
.common
import
DATA_DIR
...
...
@@ -145,19 +144,15 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
"""
def
test_init__defaults
(
self
):
loader
=
CustomLoader
()
# Check the locator attribute.
locator
=
loader
.
locator
self
.
assertEquals
(
locator
.
template_extension
,
'mustache'
)
custom
=
CustomLoader
()
# Check the reader attribute.
reader
=
loader
.
re
ader
self
.
assertEquals
(
re
ader
.
decode_errors
,
'strict'
)
self
.
assertEquals
(
re
ader
.
encoding
,
sys
.
getdefaultencoding
())
loader
=
custom
.
lo
ader
self
.
assertEquals
(
lo
ader
.
decode_errors
,
'strict'
)
self
.
assertEquals
(
lo
ader
.
encoding
,
sys
.
getdefaultencoding
())
# Check search_dirs.
self
.
assertEquals
(
loader
.
search_dirs
,
[])
self
.
assertEquals
(
custom
.
search_dirs
,
[])
def
test_init__search_dirs
(
self
):
search_dirs
=
[
'a'
,
'b'
]
...
...
@@ -165,18 +160,13 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
self
.
assertEquals
(
loader
.
search_dirs
,
[
'a'
,
'b'
])
def
test_init__reader
(
self
):
reader
=
Reader
()
loader
=
CustomLoader
([],
reader
=
reader
)
self
.
assertIs
(
loader
.
reader
,
reader
)
def
test_init__loader
(
self
):
loader
=
Loader
()
custom
=
CustomLoader
([],
loader
=
loader
)
def
test_init__locator
(
self
):
locator
=
Locator
()
loader
=
CustomLoader
([],
locator
=
locator
)
self
.
assertIs
(
loader
.
locator
,
locator
)
self
.
assertIs
(
custom
.
loader
,
loader
)
# TODO: rename to something like _assert_load().
def
_assert_template
(
self
,
loader
,
custom
,
expected
):
self
.
assertString
(
loader
.
load
(
custom
),
expected
)
...
...
@@ -223,7 +213,8 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
custom
.
template_encoding
=
'utf-8'
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.
...
...
@@ -233,28 +224,30 @@ class CustomLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
being implemented correctly (and tested).
"""
class
TestReader
(
Re
ader
):
class
MockLoader
(
Lo
ader
):
def
__init__
(
self
):
self
.
s
=
None
self
.
encoding
=
None
# Overrides the existing method.
def
unicode
(
self
,
s
,
encoding
=
None
):
self
.
s
=
s
self
.
encoding
=
encoding
return
u"foo"
reader
=
TestRe
ader
()
loader
=
CustomLoader
()
loader
.
reader
=
re
ader
loader
=
MockLo
ader
()
custom_
loader
=
CustomLoader
()
custom_loader
.
loader
=
lo
ader
custom
=
Template
()
custom
.
template
=
"template-foo"
custom
.
template_encoding
=
"encoding-foo"
view
=
Template
()
view
.
template
=
"template-foo"
view
.
template_encoding
=
"encoding-foo"
self
.
_assert_template
(
loader
,
custom
,
u'foo'
)
self
.
assertEquals
(
reader
.
s
,
"template-foo"
)
self
.
assertEquals
(
reader
.
encoding
,
"encoding-foo"
)
# Check that our unicode() above was called.
self
.
_assert_template
(
custom_loader
,
view
,
u'foo'
)
self
.
assertEquals
(
loader
.
s
,
"template-foo"
)
self
.
assertEquals
(
loader
.
encoding
,
"encoding-foo"
)
# 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