Commit 85f5ef0f by Chris Jerdonek

Renamed Renderer.default_encoding to Renderer.string_encoding.

parent 5ba98b3d
......@@ -32,8 +32,8 @@ class Renderer(object):
"""
# TODO: rename default_encoding to string_encoding.
def __init__(self, file_encoding=None, default_encoding=None,
# TODO: file_encoding should default to the package default.
def __init__(self, file_encoding=None, string_encoding=None,
decode_errors=None, search_dirs=None, file_extension=None,
escape=None, partials=None):
"""
......@@ -66,12 +66,13 @@ class Renderer(object):
escape function, for example. One may also wish to consider
using markupsafe's escape function: markupsafe.escape().
file_encoding: the name of the encoding of all template files.
This encoding is used when reading and converting any template
files to unicode. All templates are converted to unicode prior
to parsing. Defaults to the default_encoding argument.
file_encoding: the name of the default encoding to use when reading
template files. All templates are converted to unicode prior
to parsing. This encoding is used when reading template files
and converting them to unicode. Defaults to the value of the
string_encoding argument.
default_encoding: the name of the encoding to use when converting
string_encoding: the name of the encoding to use when converting
to unicode any strings of type str encountered during the
rendering process. The name will be passed as the encoding
argument to the built-in function unicode(). Defaults to the
......@@ -94,15 +95,15 @@ class Renderer(object):
if decode_errors is None:
decode_errors = defaults.DECODE_ERRORS
if default_encoding is None:
default_encoding = defaults.STRING_ENCODING
if string_encoding is None:
string_encoding = defaults.STRING_ENCODING
if escape is None:
escape = defaults.TAG_ESCAPE
# This needs to be after we set the default default_encoding.
# This needs to be after we set the default string_encoding.
if file_encoding is None:
file_encoding = default_encoding
file_encoding = string_encoding
if file_extension is None:
file_extension = defaults.TEMPLATE_EXTENSION
......@@ -114,8 +115,7 @@ class Renderer(object):
search_dirs = [search_dirs]
self.decode_errors = decode_errors
# TODO: rename this attribute to string_encoding.
self.default_encoding = default_encoding
self.string_encoding = string_encoding
self.escape = escape
self.file_encoding = file_encoding
self.file_extension = file_extension
......@@ -148,7 +148,7 @@ class Renderer(object):
def unicode(self, s, encoding=None):
"""
Convert a string to unicode, using default_encoding and decode_errors.
Convert a string to unicode, using string_encoding and decode_errors.
Raises:
......@@ -160,10 +160,10 @@ class Renderer(object):
"""
if encoding is None:
encoding = self.default_encoding
encoding = self.string_encoding
# TODO: Wrap UnicodeDecodeErrors with a message about setting
# the default_encoding and decode_errors attributes.
# the string_encoding and decode_errors attributes.
return unicode(s, encoding, self.decode_errors)
def _make_loader(self):
......@@ -280,7 +280,7 @@ class Renderer(object):
Returns the rendering as a unicode string.
Prior to rendering, templates of type str are converted to unicode
using the default_encoding and decode_errors attributes. See the
using the string_encoding and decode_errors attributes. See the
constructor docstring for more information.
Arguments:
......
......@@ -54,21 +54,21 @@ class RendererInitTestCase(unittest.TestCase):
renderer = Renderer(escape=escape)
self.assertEquals(renderer.escape("bar"), "**bar")
def test_default_encoding__default(self):
def test_string_encoding__default(self):
"""
Check the default value.
"""
renderer = Renderer()
self.assertEquals(renderer.default_encoding, sys.getdefaultencoding())
self.assertEquals(renderer.string_encoding, sys.getdefaultencoding())
def test_default_encoding(self):
def test_string_encoding(self):
"""
Check that the constructor sets the attribute correctly.
"""
renderer = Renderer(default_encoding="foo")
self.assertEquals(renderer.default_encoding, "foo")
renderer = Renderer(string_encoding="foo")
self.assertEquals(renderer.string_encoding, "foo")
def test_decode_errors__default(self):
"""
......@@ -92,7 +92,7 @@ class RendererInitTestCase(unittest.TestCase):
"""
renderer = Renderer()
self.assertEquals(renderer.file_encoding, renderer.default_encoding)
self.assertEquals(renderer.file_encoding, renderer.string_encoding)
def test_file_encoding(self):
"""
......@@ -152,18 +152,18 @@ class RendererTestCase(unittest.TestCase):
## Test Renderer.unicode().
def test_unicode__default_encoding(self):
def test_unicode__string_encoding(self):
"""
Test that the default_encoding attribute is respected.
Test that the string_encoding attribute is respected.
"""
renderer = Renderer()
s = "é"
renderer.default_encoding = "ascii"
renderer.string_encoding = "ascii"
self.assertRaises(UnicodeDecodeError, renderer.unicode, s)
renderer.default_encoding = "utf-8"
renderer.string_encoding = "utf-8"
self.assertEquals(renderer.unicode(s), u"é")
def test_unicode__decode_errors(self):
......@@ -172,7 +172,7 @@ class RendererTestCase(unittest.TestCase):
"""
renderer = Renderer()
renderer.default_encoding = "ascii"
renderer.string_encoding = "ascii"
s = "déf"
renderer.decode_errors = "ignore"
......@@ -289,12 +289,12 @@ class RendererTestCase(unittest.TestCase):
renderer = Renderer()
template = "déf"
# Check that decode_errors and default_encoding are both respected.
# Check that decode_errors and string_encoding are both respected.
renderer.decode_errors = 'ignore'
renderer.default_encoding = 'ascii'
renderer.string_encoding = 'ascii'
self.assertEquals(renderer.render(template), "df")
renderer.default_encoding = 'utf_8'
renderer.string_encoding = 'utf_8'
self.assertEquals(renderer.render(template), u"déf")
def test_make_load_partial(self):
......@@ -387,7 +387,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
pass
renderer = Renderer()
renderer.default_encoding = 'ascii'
renderer.string_encoding = 'ascii'
renderer.partials = {'str': 'foo', 'subclass': MyUnicode('abc')}
engine = renderer._make_render_engine()
......@@ -439,7 +439,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
"""
renderer = Renderer()
renderer.default_encoding = 'ascii'
renderer.string_encoding = 'ascii'
engine = renderer._make_render_engine()
literal = engine.literal
......@@ -452,7 +452,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
"""
renderer = Renderer()
renderer.default_encoding = 'ascii'
renderer.string_encoding = 'ascii'
engine = renderer._make_render_engine()
literal = engine.literal
......@@ -520,7 +520,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
"""
renderer = Renderer()
renderer.default_encoding = 'ascii'
renderer.string_encoding = 'ascii'
engine = renderer._make_render_engine()
escape = engine.escape
......
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