diff --git a/picard/album.py b/picard/album.py index 4aedbbf70..edd100d65 100644 --- a/picard/album.py +++ b/picard/album.py @@ -58,7 +58,7 @@ class Album(DataObject, Item): # 'Translate' artist name if self.config.setting['translate_artist_names']: - m['albumartist'] = m['artist'] = translate_artist(m) + m['albumartist'] = m['artist'] = translate_artist(m['artist'], m['artistsort']) # Custom VA name if m['musicbrainz_artistid'] == VARIOUS_ARTISTS_ID: @@ -89,7 +89,7 @@ class Album(DataObject, Item): # 'Translate' artist name if self.config.setting['translate_artist_names']: - tm['artist'] = translate_artist(tm) + tm['artist'] = translate_artist(tm['artist'], tm['artistsort']) # Custom VA name if tm['musicbrainz_artistid'] == VARIOUS_ARTISTS_ID: diff --git a/picard/mbxml.py b/picard/mbxml.py index 8d7179a9e..f1127c789 100644 --- a/picard/mbxml.py +++ b/picard/mbxml.py @@ -80,30 +80,6 @@ def _relations_to_metadata(relation_lists, m): # TODO: Release, Track, URL relations -def _reverse_sortname(sortname): - """Reverse sortnames.""" - chunks = [a.strip() for a in sortname.split(",")] - if len(chunks) == 2: - return "%s %s" % (chunks[1], chunks[0]) - elif len(chunks) == 3: - return "%s %s %s" % (chunks[2], chunks[1], chunks[0]) - elif len(chunks) == 4: - return "%s %s, %s %s" % (chunks[1], chunks[0], chunks[3], chunks[2]) - else: - return sortname.strip() - - -def translate_artist(m): - """'Translate' the artist name by reversing the sortname.""" - name = m['artist'] - sortname = m['artistsort'] - for c in name: - ctg = unicodedata.category(c) - if ctg[0] not in ("P", "Z") and ctg != "Nd" and unicodedata.name(c).find("LATIN") == -1: - return " & ".join(map(_reverse_sortname, sortname.split("&"))) - return name - - def _set_artist_item(m, release, albumname, name, value): if release: m[albumname] = value diff --git a/picard/util/__init__.py b/picard/util/__init__.py index 2c4a66e5d..0f50cb706 100644 --- a/picard/util/__init__.py +++ b/picard/util/__init__.py @@ -209,6 +209,28 @@ def make_short_filename(prefix, filename, length=250, max_length=250, return os.path.join(*parts) +def _reverse_sortname(sortname): + """Reverse sortnames.""" + chunks = [a.strip() for a in sortname.split(",")] + if len(chunks) == 2: + return "%s %s" % (chunks[1], chunks[0]) + elif len(chunks) == 3: + return "%s %s %s" % (chunks[2], chunks[1], chunks[0]) + elif len(chunks) == 4: + return "%s %s, %s %s" % (chunks[1], chunks[0], chunks[3], chunks[2]) + else: + return sortname.strip() + + +def translate_artist(name, sortname): + """'Translate' the artist name by reversing the sortname.""" + for c in name: + ctg = unicodedata.category(c) + if ctg[0] not in ("P", "Z") and ctg != "Nd" and unicodedata.name(c).find("LATIN") == -1: + return " & ".join(map(_reverse_sortname, sortname.split("&"))) + return name + + try: from functools import partial except ImportError: diff --git a/test/test_utils.py b/test/test_utils.py index 819b967a0..00fea7336 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -75,7 +75,7 @@ class ShortFilenameTest(unittest.TestCase): class TranslateArtistTest(unittest.TestCase): - + def test_latin(self): self.failUnlessEqual(u"Jean Michel Jarre", util.translate_artist(u"Jean Michel Jarre", u"Jarre, Jean Michel")) self.failIfEqual(u"Jarre, Jean Michel", util.translate_artist(u"Jean Michel Jarre", u"Jarre, Jean Michel"))