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
7a4dbe22
Commit
7a4dbe22
authored
Dec 26, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added new Reader class and tests.
parent
b2f59598
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
0 deletions
+136
-0
pystache/reader.py
+55
-0
tests/test_reader.py
+81
-0
No files found.
pystache/reader.py
0 → 100644
View file @
7a4dbe22
# coding: utf-8
"""
This module provides a Reader class to read a template given a path.
"""
from
__future__
import
with_statement
import
os
import
sys
DEFAULT_DECODE_ERRORS
=
'strict'
class
Reader
(
object
):
def
__init__
(
self
,
encoding
=
None
,
decode_errors
=
None
):
"""
Construct a template reader.
Arguments:
encoding: the file encoding. This is the name of the encoding to
use when converting file contents to unicode. This name is
passed as the encoding argument to Python's 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
()
self
.
decode_errors
=
decode_errors
self
.
encoding
=
encoding
def
read
(
self
,
path
):
"""
Read the template at the given path, and return it as a unicode string.
"""
with
open
(
path
,
'r'
)
as
f
:
text
=
f
.
read
()
text
=
unicode
(
text
,
self
.
encoding
,
self
.
decode_errors
)
return
text
tests/test_reader.py
0 → 100644
View file @
7a4dbe22
# encoding: utf-8
"""
Unit tests of reader.py.
"""
import
os
import
sys
import
unittest
from
pystache.reader
import
Reader
DATA_DIR
=
'tests/data'
class
ReaderTestCase
(
unittest
.
TestCase
):
def
_get_path
(
self
,
filename
):
return
os
.
path
.
join
(
DATA_DIR
,
filename
)
def
test_init__decode_errors
(
self
):
# Test the default value.
reader
=
Reader
()
self
.
assertEquals
(
reader
.
decode_errors
,
'strict'
)
reader
=
Reader
(
decode_errors
=
'replace'
)
self
.
assertEquals
(
reader
.
decode_errors
,
'replace'
)
def
test_init__encoding
(
self
):
# Test the default value.
reader
=
Reader
()
self
.
assertEquals
(
reader
.
encoding
,
sys
.
getdefaultencoding
())
reader
=
Reader
(
encoding
=
'foo'
)
self
.
assertEquals
(
reader
.
encoding
,
'foo'
)
def
test_read
(
self
):
"""
Test read().
"""
reader
=
Reader
()
path
=
self
.
_get_path
(
'ascii.mustache'
)
self
.
assertEquals
(
reader
.
read
(
path
),
'ascii: abc'
)
def
test_read__returns_unicode
(
self
):
"""
Test that read() returns unicode strings.
"""
reader
=
Reader
()
path
=
self
.
_get_path
(
'ascii.mustache'
)
contents
=
reader
.
read
(
path
)
self
.
assertEqual
(
type
(
contents
),
unicode
)
def
test_read__encoding
(
self
):
"""
Test read(): encoding attribute respected.
"""
reader
=
Reader
()
path
=
self
.
_get_path
(
'nonascii.mustache'
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
reader
.
encoding
=
'utf-8'
self
.
assertEquals
(
reader
.
read
(
path
),
u'non-ascii: é'
)
def
test_get__decode_errors
(
self
):
"""
Test get(): decode_errors attribute.
"""
reader
=
Reader
()
path
=
self
.
_get_path
(
'nonascii.mustache'
)
self
.
assertRaises
(
UnicodeDecodeError
,
reader
.
read
,
path
)
reader
.
decode_errors
=
'replace'
self
.
assertEquals
(
reader
.
read
(
path
),
u'non-ascii:
\ufffd\ufffd
'
)
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