From 532905bd7440ec7663110db5b6fa0c86f825ff28 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 23 Dec 2014 22:20:49 +0100 Subject: [PATCH 1/3] docs: Add :discuss: shortcut for linking to forum threads --- docs/conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/conf.py b/docs/conf.py index 52e84e06..f7475293 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -156,6 +156,7 @@ extlinks = { 'commit': ('https://github.com/mopidy/mopidy/commit/%s', 'commit '), 'mpris': ( 'https://github.com/mopidy/mopidy-mpris/issues/%s', 'mopidy-mpris#'), + 'discuss': ('https://discuss.mopidy.com/t/%s', 'discuss.mopidy.com/t/'), } From aa3b8ab5f84798428134d1dda2c944e98879a168 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 23 Dec 2014 22:11:11 +0100 Subject: [PATCH 2/3] path: Support unicode content when creating file --- mopidy/utils/path.py | 6 ++++-- tests/utils/test_path.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index 5870fc6e..7c1f671b 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -44,12 +44,14 @@ def get_or_create_file(file_path, mkdir=True, content=None): if not isinstance(file_path, bytes): raise ValueError('Path is not a bytestring.') file_path = expand_path(file_path) + if isinstance(content, unicode): + content = content.encode('utf-8') if mkdir: get_or_create_dir(os.path.dirname(file_path)) if not os.path.isfile(file_path): logger.info('Creating file %s', file_path) - with open(file_path, 'w') as fh: - if content: + with open(file_path, 'wb') as fh: + if content is not None: fh.write(content) return file_path diff --git a/tests/utils/test_path.py b/tests/utils/test_path.py index 078cdb20..db152654 100644 --- a/tests/utils/test_path.py +++ b/tests/utils/test_path.py @@ -105,12 +105,12 @@ class GetOrCreateFileTest(unittest.TestCase): with self.assertRaises(IOError): path.get_or_create_file(conflicting_dir) - def test_create_dir_with_unicode(self): + def test_create_dir_with_unicode_filename_throws_value_error(self): with self.assertRaises(ValueError): file_path = unicode(os.path.join(self.parent, b'test')) path.get_or_create_file(file_path) - def test_create_file_with_none(self): + def test_create_file_with_none_filename_throws_value_error(self): with self.assertRaises(ValueError): path.get_or_create_file(None) @@ -119,12 +119,18 @@ class GetOrCreateFileTest(unittest.TestCase): with self.assertRaises(IOError): path.get_or_create_file(file_path, mkdir=False) - def test_create_dir_with_default_content(self): + def test_create_dir_with_bytes_content(self): file_path = os.path.join(self.parent, b'test') created = path.get_or_create_file(file_path, content=b'foobar') with open(created) as fh: self.assertEqual(fh.read(), b'foobar') + def test_create_dir_with_unicode_content(self): + file_path = os.path.join(self.parent, b'test') + created = path.get_or_create_file(file_path, content='foobaræøå') + with open(created) as fh: + self.assertEqual(fh.read(), b'foobaræøå') + class PathToFileURITest(unittest.TestCase): def test_simple_path(self): From fcf39833ca33a107954f052cbaedcf988689d43a Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 23 Dec 2014 22:12:34 +0100 Subject: [PATCH 3/3] config: Support UTF-8 in default config Fixes issue reported at https://discuss.mopidy.com/t/428. Mopidy-HTTP-Kuechenradio includes a non-ASCII UTF-8 character in its default config. If Mopidy didn't already have a config file, it crashed when trying to create the initial config file based on the default config of all available extensions. --- docs/changelog.rst | 6 ++++++ mopidy/config/__init__.py | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index ba00ed1f..1ef13666 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -10,6 +10,12 @@ v0.19.5 (UNRELEASED) Bug fix release. +- config: Support UTF-8 in default config. If an extension with non-ASCII + characters in its default config was installed, and Mopidy didn't already + have a config file, Mopidy would crashed when trying to create the initial + config file based on the default config of all available extensions. + (Fixes: :discuss:`428`) + - Models: Hide empty collections from :func:`repr()` representations. - Models: Field values are no longer stored on the model instance when the diff --git a/mopidy/config/__init__.py b/mopidy/config/__init__.py index 5611c717..a6578825 100644 --- a/mopidy/config/__init__.py +++ b/mopidy/config/__init__.py @@ -97,8 +97,12 @@ def format_initial(extensions): versions = ['Mopidy %s' % versioning.get_version()] for extension in sorted(extensions, key=lambda ext: ext.dist_name): versions.append('%s %s' % (extension.dist_name, extension.version)) - description = _INITIAL_HELP.strip() % {'versions': '\n# '.join(versions)} - return description + '\n\n' + _format(config, {}, schemas, False, True) + + header = _INITIAL_HELP.strip() % {'versions': '\n# '.join(versions)} + formatted_config = _format( + config=config, comments={}, schemas=schemas, + display=False, disable=True).decode('utf-8') + return header + '\n\n' + formatted_config def _load(files, defaults, overrides):