From b7232797eab78591daa4df6ee220952ae0787028 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Wed, 24 Apr 2013 23:31:39 +0200 Subject: [PATCH] config/ext: Accept unicode defaults --- docs/extensiondev.rst | 8 ++++---- mopidy/config/__init__.py | 2 ++ mopidy/ext.py | 2 +- tests/config/config_test.py | 8 ++++++++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/extensiondev.rst b/docs/extensiondev.rst index 20d3afa0..df97ab1f 100644 --- a/docs/extensiondev.rst +++ b/docs/extensiondev.rst @@ -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. diff --git a/mopidy/config/__init__.py b/mopidy/config/__init__.py index df76b17d..66e17c5e 100644 --- a/mopidy/config/__init__.py +++ b/mopidy/config/__init__.py @@ -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 diff --git a/mopidy/ext.py b/mopidy/ext.py index 85dceee4..27e72854 100644 --- a/mopidy/ext.py +++ b/mopidy/ext.py @@ -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"') diff --git a/tests/config/config_test.py b/tests/config/config_test.py index cd708d85..dad52b2d 100644 --- a/tests/config/config_test.py +++ b/tests/config/config_test.py @@ -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'