def clean_song(manager, song): """ Cleans the search title, rebuilds the search lyrics, adds a default author if the song does not have one and other clean ups. This should always called when a new song is added or changed. :param manager: The song database manager object. :param song: The song object. """ from .openlyricsxml import SongXML if song.title: song.title = clean_title(song.title) else: song.title = '' if song.alternate_title: song.alternate_title = clean_title(song.alternate_title) else: song.alternate_title = '' song.search_title = clean_string(song.title) + '@' + clean_string(song.alternate_title) if isinstance(song.lyrics, bytes): song.lyrics = str(song.lyrics, encoding='utf8') verses = SongXML().get_verses(song.lyrics) song.search_lyrics = ' '.join([clean_string(verse[1]) for verse in verses]) # The song does not have any author, add one. if not song.authors_songs: name = SongStrings.AuthorUnknown author = manager.get_object_filtered(Author, Author.display_name == name) if author is None: author = Author.populate(display_name=name, last_name='', first_name='') song.add_author(author) if song.copyright: song.copyright = CONTROL_CHARS.sub('', song.copyright).strip()
def clean_title(title): """ Cleans the song title by removing Unicode control chars groups C0 & C1, as well as any trailing spaces. :param title: The song title to clean :return: A clean title """ return CONTROL_CHARS.sub('', title).rstrip()
def clean_song(manager, song): """ Cleans the search title, rebuilds the search lyrics, adds a default author if the song does not have one and other clean ups. This should always called when a new song is added or changed. :param manager: The song database manager object. :param song: The song object. """ from .openlyricsxml import SongXML if song.title: song.title = clean_title(song.title) else: song.title = '' if song.alternate_title: song.alternate_title = clean_title(song.alternate_title) else: song.alternate_title = '' song.search_title = clean_string(song.title) + '@' + clean_string( song.alternate_title) if isinstance(song.lyrics, bytes): song.lyrics = str(song.lyrics, encoding='utf8') verses = SongXML().get_verses(song.lyrics) song.search_lyrics = ' '.join( [clean_string(clean_tags(verse[1])) for verse in verses]) # The song does not have any author, add one. if not song.authors_songs: name = SongStrings.AuthorUnknown author = manager.get_object_filtered(Author, Author.display_name == name) if author is None: author = Author.populate(display_name=name, last_name='', first_name='') song.add_author(author) if song.copyright: song.copyright = CONTROL_CHARS.sub('', song.copyright).strip()
def clean_song(manager, song): """ Cleans the search title, rebuilds the search lyrics, adds a default author if the song does not have one and other clean ups. This should always called when a new song is added or changed. ``manager`` The song's manager. ``song`` The song object. """ if isinstance(song.title, buffer): song.title = unicode(song.title) if isinstance(song.alternate_title, buffer): song.alternate_title = unicode(song.alternate_title) if isinstance(song.lyrics, buffer): song.lyrics = unicode(song.lyrics) if song.title: song.title = clean_title(song.title) else: song.title = u'' if song.alternate_title: song.alternate_title = clean_title(song.alternate_title) else: song.alternate_title = u'' song.search_title = clean_string(song.title) + u'@' + clean_string(song.alternate_title) # Only do this, if we the song is a 1.9.4 song (or older). if song.lyrics.find(u'<lyrics language="en">') != -1: # Remove the old "language" attribute from lyrics tag (prior to 1.9.5). # This is not very important, but this keeps the database clean. This # can be removed when everybody has cleaned his songs. song.lyrics = song.lyrics.replace(u'<lyrics language="en">', u'<lyrics>') verses = SongXML().get_verses(song.lyrics) song.search_lyrics = u' '.join([clean_string(verse[1]) for verse in verses]) # We need a new and clean SongXML instance. sxml = SongXML() # Rebuild the song's verses, to remove any wrong verse names (for # example translated ones), which might have been added prior to 1.9.5. # List for later comparison. compare_order = [] for verse in verses: verse_type = VerseType.Tags[VerseType.from_loose_input(verse[0][u'type'])] sxml.add_verse_to_lyrics( verse_type, verse[0][u'label'], verse[1], verse[0].get(u'lang') ) compare_order.append((u'%s%s' % (verse_type, verse[0][u'label'])).upper()) if verse[0][u'label'] == u'1': compare_order.append(verse_type.upper()) song.lyrics = unicode(sxml.extract_xml(), u'utf-8') # Rebuild the verse order, to convert translated verse tags, which might # have been added prior to 1.9.5. if song.verse_order: order = CONTROL_CHARS.sub(u'', song.verse_order).strip().split() else: order = [] new_order = [] for verse_def in order: verse_type = VerseType.Tags[ VerseType.from_loose_input(verse_def[0])] if len(verse_def) > 1: new_order.append((u'%s%s' % (verse_type, verse_def[1:])).upper()) else: new_order.append(verse_type.upper()) song.verse_order = u' '.join(new_order) # Check if the verse order contains tags for verses which do not exist. for order in new_order: if order not in compare_order: song.verse_order = u'' break else: verses = SongXML().get_verses(song.lyrics) song.search_lyrics = u' '.join([clean_string(verse[1]) for verse in verses]) # The song does not have any author, add one. if not song.authors: name = SongStrings.AuthorUnknown author = manager.get_object_filtered(Author, Author.display_name == name) if author is None: author = Author.populate(display_name=name, last_name=u'', first_name=u'') song.authors.append(author) if song.copyright: song.copyright = CONTROL_CHARS.sub(u'', song.copyright).strip()
def clean_title(title): """ Cleans the song title by removing Unicode control chars groups C0 & C1, as well as any trailing spaces """ return CONTROL_CHARS.sub(u'', title).rstrip()