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
3a960197
Commit
3a960197
authored
Dec 23, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed issue #65: "Loader should accept decode_errors like Renderer"
parent
10e867c9
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
71 additions
and
15 deletions
+71
-15
pystache/loader.py
+10
-2
tests/data/ascii.mustache
+2
-0
tests/data/nonascii.mustache
+2
-0
tests/test_loader.py
+57
-13
No files found.
pystache/loader.py
View file @
3a960197
...
...
@@ -8,12 +8,13 @@ This module provides a Loader class.
import
os
import
sys
DEFAULT_DECODE_ERRORS
=
'strict'
DEFAULT_EXTENSION
=
'mustache'
class
Loader
(
object
):
def
__init__
(
self
,
search_dirs
=
None
,
e
ncoding
=
None
,
extension
=
None
):
def
__init__
(
self
,
search_dirs
=
None
,
e
xtension
=
None
,
encoding
=
None
,
decode_errors
=
None
):
"""
Construct a template loader.
...
...
@@ -32,8 +33,14 @@ class Loader(object):
argument to the built-in function unicode(). Defaults to the
encoding name returned by sys.getdefaultencoding().
decode_errors: the string to pass as the "errors" argument to the
built-in function unicode() when converting file contents to
unicode. Defaults to "strict".
"""
if
decode_errors
is
None
:
decode_errors
=
DEFAULT_DECODE_ERRORS
if
encoding
is
None
:
encoding
=
sys
.
getdefaultencoding
()
...
...
@@ -46,6 +53,7 @@ class Loader(object):
if
isinstance
(
search_dirs
,
basestring
):
search_dirs
=
[
search_dirs
]
self
.
decode_errors
=
decode_errors
self
.
search_dirs
=
search_dirs
self
.
template_encoding
=
encoding
self
.
template_extension
=
extension
...
...
@@ -88,6 +96,6 @@ class Loader(object):
finally
:
f
.
close
()
template
=
unicode
(
template
,
self
.
template_encoding
)
template
=
unicode
(
template
,
self
.
template_encoding
,
self
.
decode_errors
)
return
template
tests/data/ascii.mustache
0 → 100644
View file @
3a960197
ascii: abc
\ No newline at end of file
tests/data/nonascii.mustache
0 → 100644
View file @
3a960197
non-ascii: é
\ No newline at end of file
tests/test_loader.py
View file @
3a960197
# encoding: utf-8
import
os
import
sys
import
unittest
from
pystache.loader
import
Loader
DATA_DIR
=
'tests/data'
class
LoaderTestCase
(
unittest
.
TestCase
):
search_dirs
=
'examples'
def
_loader
(
self
):
return
Loader
(
search_dirs
=
DATA_DIR
)
def
test_init__search_dirs
(
self
):
# Test the default value.
loader
=
Loader
()
...
...
@@ -17,18 +23,15 @@ class LoaderTestCase(unittest.TestCase):
loader
=
Loader
(
search_dirs
=
[
'foo'
])
self
.
assertEquals
(
loader
.
search_dirs
,
[
'foo'
])
def
test_init__
extension
(
self
):
def
test_init__
decode_errors
(
self
):
# Test the default value.
loader
=
Loader
()
self
.
assertEquals
(
loader
.
template_extension
,
'mustache
'
)
self
.
assertEquals
(
loader
.
decode_errors
,
'strict
'
)
loader
=
Loader
(
extension
=
'txt'
)
self
.
assertEquals
(
loader
.
template_extension
,
'txt'
)
loader
=
Loader
(
extension
=
False
)
self
.
assertTrue
(
loader
.
template_extension
is
False
)
loader
=
Loader
(
decode_errors
=
'replace'
)
self
.
assertEquals
(
loader
.
decode_errors
,
'replace'
)
def
test_init__
loader
(
self
):
def
test_init__
encoding
(
self
):
# Test the default value.
loader
=
Loader
()
self
.
assertEquals
(
loader
.
template_encoding
,
sys
.
getdefaultencoding
())
...
...
@@ -36,6 +39,17 @@ class LoaderTestCase(unittest.TestCase):
loader
=
Loader
(
encoding
=
'foo'
)
self
.
assertEquals
(
loader
.
template_encoding
,
'foo'
)
def
test_init__extension
(
self
):
# Test the default value.
loader
=
Loader
()
self
.
assertEquals
(
loader
.
template_extension
,
'mustache'
)
loader
=
Loader
(
extension
=
'txt'
)
self
.
assertEquals
(
loader
.
template_extension
,
'txt'
)
loader
=
Loader
(
extension
=
False
)
self
.
assertTrue
(
loader
.
template_extension
is
False
)
def
test_make_file_name
(
self
):
loader
=
Loader
()
...
...
@@ -72,12 +86,42 @@ class LoaderTestCase(unittest.TestCase):
loader
.
template_extension
=
False
self
.
assertEquals
(
loader
.
get
(
'extensionless'
),
"No file extension: {{foo}}"
)
def
test_get
__load_template__unicode_return_value
(
self
):
def
test_get
(
self
):
"""
Check that load_template() returns unicode strings
.
Test get()
.
"""
loader
=
Loader
(
search_dirs
=
self
.
search_dirs
)
template
=
loader
.
get
(
'simple'
)
loader
=
self
.
_loader
()
self
.
assertEquals
(
loader
.
get
(
'ascii'
),
'ascii: abc'
)
def
test_get__unicode_return_value
(
self
):
"""
Test that get() returns unicode strings.
"""
loader
=
self
.
_loader
()
actual
=
loader
.
get
(
'ascii'
)
self
.
assertEqual
(
type
(
actual
),
unicode
)
def
test_get__encoding
(
self
):
"""
Test get(): encoding attribute respected.
"""
loader
=
self
.
_loader
()
self
.
assertRaises
(
UnicodeDecodeError
,
loader
.
get
,
'nonascii'
)
loader
.
template_encoding
=
'utf-8'
self
.
assertEquals
(
loader
.
get
(
'nonascii'
),
u'non-ascii: é'
)
def
test_get__decode_errors
(
self
):
"""
Test get(): decode_errors attribute.
"""
loader
=
self
.
_loader
()
self
.
assertRaises
(
UnicodeDecodeError
,
loader
.
get
,
'nonascii'
)
loader
.
decode_errors
=
'replace'
self
.
assertEquals
(
loader
.
get
(
'nonascii'
),
u'non-ascii:
\ufffd\ufffd
'
)
self
.
assertEqual
(
type
(
template
),
unicode
)
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