Commit 008198b5 by Chris Jerdonek

Added optional encoding argument to Reader.read().

parent 78ef793b
......@@ -42,14 +42,17 @@ class Reader(object):
self.decode_errors = decode_errors
self.encoding = encoding
def read(self, path):
def read(self, path, encoding=None):
"""
Read the template at the given path, and return it as a unicode string.
"""
if encoding is None:
encoding = self.encoding
with open(path, 'r') as f:
text = f.read()
text = unicode(text, self.encoding, self.decode_errors)
text = unicode(text, encoding, self.decode_errors)
return text
......@@ -55,7 +55,7 @@ class ReaderTestCase(unittest.TestCase):
contents = reader.read(path)
self.assertEqual(type(contents), unicode)
def test_read__encoding(self):
def test_read__encoding__attribute(self):
"""
Test read(): encoding attribute respected.
......@@ -67,6 +67,17 @@ class ReaderTestCase(unittest.TestCase):
reader.encoding = 'utf-8'
self.assertEquals(reader.read(path), u'non-ascii: é')
def test_read__encoding__argument(self):
"""
Test read(): encoding argument respected.
"""
reader = Reader()
path = self._get_path('nonascii.mustache')
self.assertRaises(UnicodeDecodeError, reader.read, path)
self.assertEquals(reader.read(path, encoding='utf-8'), u'non-ascii: é')
def test_get__decode_errors(self):
"""
Test get(): decode_errors attribute.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment