From 781c26ffd00b72253bbf560896351e6435909231 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 27 Mar 2012 10:44:11 -0700 Subject: [PATCH] normalize to NFC on non-Mac platforms (#367) --- beets/library.py | 8 ++++++-- docs/changelog.rst | 3 ++- test/test_db.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/beets/library.py b/beets/library.py index 8a392384c..8965dd6dd 100644 --- a/beets/library.py +++ b/beets/library.py @@ -807,7 +807,7 @@ class Library(BaseLibrary): self.conn.commit() def destination(self, item, pathmod=None, in_album=False, - fragment=False, basedir=None): + fragment=False, basedir=None, platform=None): """Returns the path in the library directory designated for item item (i.e., where the file ought to be). in_album forces the item to be treated as part of an album. fragment makes this @@ -817,6 +817,7 @@ class Library(BaseLibrary): directory for the destination. """ pathmod = pathmod or os.path + platform = platform or sys.platform # Use a path format based on a query, falling back on the # default. @@ -878,7 +879,10 @@ class Library(BaseLibrary): subpath = subpath_tmpl.substitute(mapping, funcs) # Encode for the filesystem, dropping unencodable characters. - subpath = unicodedata.normalize('NFD', subpath) + if platform == 'darwin': + subpath = unicodedata.normalize('NFD', subpath) + else: + subpath = unicodedata.normalize('NFC', subpath) if isinstance(subpath, unicode) and not fragment: encoding = sys.getfilesystemencoding() or sys.getdefaultencoding() subpath = subpath.encode(encoding, 'replace') diff --git a/docs/changelog.rst b/docs/changelog.rst index 2b7544f4f..10df4931b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -24,7 +24,8 @@ Changelog tracks on the album (to help you guess what might be going wrong). * :doc:`/plugins/bpd`: Use Gstreamer's ``playbin2`` element instead of the deprecated ``playbin``. -* Filenames are normalized with Unicode Normal Form D (NFD). +* Filenames are normalized with Unicode Normal Form D (NFD) on Mac OS X and NFC + on all other platforms. 1.0b13 (March 16, 2012) diff --git a/test/test_db.py b/test/test_db.py index 06cca6b48..44b59b626 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -20,6 +20,7 @@ import ntpath import posixpath import shutil import re +import unicodedata import _common from _common import unittest @@ -404,6 +405,18 @@ class DestinationTest(unittest.TestCase): ]) self.assertEqual(p, 'bar/bar') + def test_unicode_normalized_nfd_on_mac(self): + instr = unicodedata.normalize('NFC', u'caf\xe9') + self.lib.path_formats = [('default', instr)] + dest = self.lib.destination(self.i, platform='darwin', fragment=True) + self.assertEqual(dest, unicodedata.normalize('NFD', instr)) + + def test_unicode_normalized_nfc_on_linux(self): + instr = unicodedata.normalize('NFD', u'caf\xe9') + self.lib.path_formats = [('default', instr)] + dest = self.lib.destination(self.i, platform='linux2', fragment=True) + self.assertEqual(dest, unicodedata.normalize('NFC', instr)) + class PathFormattingMixin(object): """Utilities for testing path formatting.""" def _setf(self, fmt):