def test_standardize(self): for raw_text, expected_standardization in ( ("Foo", "Foo"), # Check whitespace cleanup (" Foo ", "Foo"), ("Foo \t Bar", "Foo Bar"), ("Foo [ Tag ]", "Foo [Tag]"), ("Foo [Tag][Tag]", "Foo [Tag] [Tag]"), ("Foo[Tag]", "Foo [Tag]"), # Check quote handling (u"Don\u2019t Worry", "Don't Worry"), ("Name [7'']", 'Name [7"]'), (u"Name [7\u201d]", 'Name [7"]'), ): self.assertEqual(expected_standardization, titles.standardize(raw_text)) # Check that we reject malformed text. for bad_text in ( "Unbalanced [", "Unbalanced ]", "Unbalanced [Foo", "Unbalanced [Foo] Bar]", "Nested [Foo [Bar]]", "Empty Tag []", "Foo [No Interior Tags] Bar", ): self.assertEqual(None, titles.standardize(bad_text))
def _standardize_tags(all_au_files, new_album_name=None): """Perform album-level standardizations on a set of tags. Args: all_au_files: A list of AudioFile objects corresponding to all of the tracks on a single album. new_album_name: If not None, the text of the TALB tag is replaced by this string. If necessary, the AudioFile objects are modified in place, moving guest artists from TPE1 into TIT2. Raises: AlbumError: if the there are gaps or problems with the track numbers, or if the TALB tag is not consistent across all of the tracks. """ # Check that both TPE1 and TIT2 are present in every file. for au_file in all_au_files: if "TPE1" not in au_file.mutagen_id3: raise AlbumError("Missing TPE1 in %s" % au_file.path) if "TIT2" not in au_file.mutagen_id3: raise AlbumError("Mising TIT2 in %s" % au_file.path) # Check that the album names all match. all_talb = set( unicode(au_file.mutagen_id3["TALB"]) for au_file in all_au_files) if len(all_talb) != 1: # Is the inconsistency only an issue of upper vs. lower case? # If so, pick the version that is in the majority. if len(set(x.lower() for x in all_talb)) == 1: freq = {} for x in all_talb: freq[x] = freq.get(x, 0) + 1 all_talb = set([sorted((n, x) for x, n in freq.items())[0][1]]) else: raise AlbumError("Inconsistent album names: %s" % " / ".join(all_talb)) # Standardize the album name. album_name = titles.standardize(new_album_name or all_talb.pop()) if album_name is None: raise AlbumError('Invalid album name: "%s"' % album_name) for au_file in all_au_files: au_file.mutagen_id3["TALB"].text = [album_name] # Check and clean up track numbering. for au_file in all_au_files: if not "TRCK" in au_file.mutagen_id3: raise AlbumError("Missing TRCK tag in %s" % au_file.path) all_trck = [au_file.mutagen_id3["TRCK"] for au_file in all_au_files] try: order.verify_and_standardize(all_trck) except order.BadOrderError, ex: raise AlbumError(str(ex))
def _standardize_tags(all_au_files, new_album_name=None): """Perform album-level standardizations on a set of tags. Args: all_au_files: A list of AudioFile objects corresponding to all of the tracks on a single album. new_album_name: If not None, the text of the TALB tag is replaced by this string. If necessary, the AudioFile objects are modified in place, moving guest artists from TPE1 into TIT2. Raises: AlbumError: if the there are gaps or problems with the track numbers, or if the TALB tag is not consistent across all of the tracks. """ # Check that both TPE1 and TIT2 are present in every file. for au_file in all_au_files: if "TPE1" not in au_file.mutagen_id3: raise AlbumError("Missing TPE1 in %s" % au_file.path) if "TIT2" not in au_file.mutagen_id3: raise AlbumError("Mising TIT2 in %s" % au_file.path) # Check that the album names all match. all_talb = set(unicode(au_file.mutagen_id3["TALB"]) for au_file in all_au_files) if len(all_talb) != 1: # Is the inconsistency only an issue of upper vs. lower case? # If so, pick the version that is in the majority. if len(set(x.lower() for x in all_talb)) == 1: freq = {} for x in all_talb: freq[x] = freq.get(x, 0) + 1 all_talb = set([sorted((n, x) for x, n in freq.items())[0][1]]) else: raise AlbumError("Inconsistent album names: %s" % " / ".join(all_talb)) # Standardize the album name. album_name = titles.standardize(new_album_name or all_talb.pop()) if album_name is None: raise AlbumError('Invalid album name: "%s"' % album_name) for au_file in all_au_files: au_file.mutagen_id3["TALB"].text = [ album_name ] # Check and clean up track numbering. for au_file in all_au_files: if not "TRCK" in au_file.mutagen_id3: raise AlbumError("Missing TRCK tag in %s" % au_file.path) all_trck = [au_file.mutagen_id3["TRCK"] for au_file in all_au_files] try: order.verify_and_standardize(all_trck) except order.BadOrderError, ex: raise AlbumError(str(ex))
if len(all_tpe1) > 1: tpe1 = unicode(au_file.mutagen_id3["TPE1"]) new_tpe1, guest = artists.split_and_standardize(tpe1) if new_tpe1 is None: raise AlbumError("Bad TPE1: %s" % repr(tpe1)) elif tpe1 != new_tpe1: au_file.mutagen_id3["TPE1"].text = [new_tpe1] if guest is not None: guest_str = constants.TIT2_IMPORT_GUEST_FORMAT % { 'guest': guest } # We need to append the guest artist in a way that # respects any [tags] in the song title. tit2 = titles.append(tit2, guest_str) # Standardize and store the track name. std_tit2 = titles.standardize(tit2) if std_tit2 is None: raise AlbumError("Bad track name: %s" % tit2) au_file.mutagen_id3["TIT2"].text = [std_tit2] # Now attach an album ID to each track. album_id = _compute_album_id(all_au_files) for au_file in all_au_files: au_file.album_id = album_id class Album(object): def __init__(self, all_au_files): self.all_au_files = list(all_au_files) # Compute the album ID. self.album_id = _compute_album_id(self.all_au_files)
# guest artists and move them into the song titles. if len(all_tpe1) > 1: tpe1 = unicode(au_file.mutagen_id3["TPE1"]) new_tpe1, guest = artists.split_and_standardize(tpe1) if new_tpe1 is None: raise AlbumError("Bad TPE1: %s" % repr(tpe1)) elif tpe1 != new_tpe1: au_file.mutagen_id3["TPE1"].text = [new_tpe1] if guest is not None: guest_str = constants.TIT2_IMPORT_GUEST_FORMAT % { 'guest': guest } # We need to append the guest artist in a way that # respects any [tags] in the song title. tit2 = titles.append(tit2, guest_str) # Standardize and store the track name. std_tit2 = titles.standardize(tit2) if std_tit2 is None: raise AlbumError("Bad track name: %s" % tit2) au_file.mutagen_id3["TIT2"].text = [std_tit2] # Now attach an album ID to each track. album_id = _compute_album_id(all_au_files) for au_file in all_au_files: au_file.album_id = album_id class Album(object): def __init__(self, all_au_files): self.all_au_files = list(all_au_files) # Compute the album ID.