config/ext: Accept unicode defaults

This commit is contained in:
Thomas Adamcik 2013-04-24 23:31:39 +02:00
parent 004fe6dbf8
commit b7232797ea
4 changed files with 15 additions and 5 deletions

View File

@ -208,10 +208,10 @@ The default configuration for the extension is defined by the
:mod:`ConfigParser` compatible config section. The config section's name must
be the same as the extension's short name, as defined in the ``entry_points``
part of ``setup.py``, for example ``soundspot``. All extensions must include
an ``enabled`` config which should default to ``true``. Provide good defaults
for all config values so that as few users as possible will need to change
them. The exception is if the config value has security implications; in that
case you should default to the most secure configuration. Leave any
an ``enabled`` config which normally should default to ``true``. Provide good
defaults for all config values so that as few users as possible will need to
change them. The exception is if the config value has security implications; in
that case you should default to the most secure configuration. Leave any
configurations that doesn't have meaningful defaults blank, like ``username``
and ``password``. In the example below, we've chosen to maintain the default
config as a separate file named ``ext.conf``. This makes it easy to e.g.

View File

@ -72,6 +72,8 @@ def _load(files, defaults, overrides):
# TODO: simply return path to config file for defaults so we can load it
# all in the same way?
for default in defaults:
if isinstance(default, unicode):
default = default.encode('utf-8')
parser.readfp(io.BytesIO(default))
# Load config from a series of config files

View File

@ -36,7 +36,7 @@ class Extension(object):
def get_default_config(self):
"""The extension's default config as a bytestring
:returns: bytes
:returns: bytes or unicode
"""
raise NotImplementedError(
'Add at least a config section with "enabled = true"')

View File

@ -1,3 +1,5 @@
# encoding: utf-8
from __future__ import unicode_literals
import mock
@ -17,6 +19,12 @@ class LoadConfigTest(unittest.TestCase):
result = config._load([], [default], [])
self.assertEqual(expected, result)
def test_unicode_default(self):
default = '[foo]\nbar = æøå'
expected = {'foo': {'bar': 'æøå'.encode('utf-8')}}
result = config._load([], [default], [])
self.assertEqual(expected, result)
def test_load_defaults(self):
default1 = b'[foo]\nbar = baz'
default2 = b'[foo2]\n'