add verse language to db (like OpenLyrics)

This commit is contained in:
Andreas Preikschat 2011-01-19 20:22:43 +01:00
parent 5c3d3922a9
commit cbeb289875
3 changed files with 31 additions and 10 deletions

View File

@ -61,14 +61,17 @@ class OpenLyricsImport(SongImport):
Imports the songs. Imports the songs.
""" """
self.import_wizard.progressBar.setMaximum(len(self.import_source)) self.import_wizard.progressBar.setMaximum(len(self.import_source))
parser = etree.XMLParser(remove_blank_text=True)
for file_path in self.import_source: for file_path in self.import_source:
if self.stop_import_flag: if self.stop_import_flag:
return False return False
self.import_wizard.incrementProgressBar(unicode(translate( self.import_wizard.incrementProgressBar(unicode(translate(
'SongsPlugin.OpenLyricsImport', 'Importing %s...')) % 'SongsPlugin.OpenLyricsImport', 'Importing %s...')) %
os.path.basename(file_path)) os.path.basename(file_path))
parser = etree.XMLParser(remove_blank_text=True) try:
parsed_file = etree.parse(file_path, parser) parsed_file = etree.parse(file_path, parser)
except etree.XMLSyntaxError:
return False
xml = unicode(etree.tostring(parsed_file)) xml = unicode(etree.tostring(parsed_file))
if self.openLyrics.xml_to_song(xml) is None: if self.openLyrics.xml_to_song(xml) is None:
log.debug(u'File could not be imported: %s' % file_path) log.debug(u'File could not be imported: %s' % file_path)

View File

@ -31,7 +31,7 @@ The basic XML for storing the lyrics in the song database is of the format::
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<song version="1.0"> <song version="1.0">
<lyrics> <lyrics>
<verse type="chorus" label="1"> <verse type="Chorus" label="1" lang="en">
<![CDATA[ ... ]]> <![CDATA[ ... ]]>
</verse> </verse>
</lyrics> </lyrics>
@ -84,7 +84,7 @@ class SongXML(object):
self.song_xml = objectify.fromstring(u'<song version="1.0" />') self.song_xml = objectify.fromstring(u'<song version="1.0" />')
self.lyrics = etree.SubElement(self.song_xml, u'lyrics') self.lyrics = etree.SubElement(self.song_xml, u'lyrics')
def add_verse_to_lyrics(self, type, number, content): def add_verse_to_lyrics(self, type, number, content, lang=None):
""" """
Add a verse to the *<lyrics>* tag. Add a verse to the *<lyrics>* tag.
@ -97,9 +97,15 @@ class SongXML(object):
``content`` ``content``
The actual text of the verse to be stored. The actual text of the verse to be stored.
``lang``
The verse's language code. This is not required, but should be added
if available.
""" """
verse = etree.Element(u'verse', type=unicode(type), verse = etree.Element(u'verse', type=unicode(type),
label=unicode(number)) label=unicode(number))
if lang:
verse.set(u'lang', lang)
verse.text = etree.CDATA(content) verse.text = etree.CDATA(content)
self.lyrics.append(verse) self.lyrics.append(verse)
@ -117,6 +123,11 @@ class SongXML(object):
``xml`` ``xml``
The XML of the song to be parsed. The XML of the song to be parsed.
The returned list has the following format::
[[{'lang': 'en', 'type': 'V', 'label': '1'}, u"The English verse."],
[{'lang': 'en', 'type': 'C', 'label': '1'}, u"The English chorus."]]
""" """
self.song_xml = None self.song_xml = None
if xml[:5] == u'<?xml': if xml[:5] == u'<?xml':
@ -196,7 +207,7 @@ class OpenLyrics(object):
This property is not supported. This property is not supported.
*<verse name="v1a" lang="he" translit="en">* *<verse name="v1a" lang="he" translit="en">*
The attribute *translit* and *lang* are not supported. The attribute *translit* is not supported.
*<verseOrder>* *<verseOrder>*
OpenLP supports this property. OpenLP supports this property.
@ -442,7 +453,10 @@ class OpenLyrics(object):
if not verse_number: if not verse_number:
verse_number = u'1' verse_number = u'1'
temp_verse_order.append((verse_type, verse_number, verse_part)) temp_verse_order.append((verse_type, verse_number, verse_part))
sxml.add_verse_to_lyrics(verse_type, verse_number, text) lang = None
if self._get(verse, u'lang'):
lang = self._get(verse, u'lang')
sxml.add_verse_to_lyrics(verse_type, verse_number, text, lang)
search_text = search_text + text search_text = search_text + text
song.search_lyrics = search_text.lower() song.search_lyrics = search_text.lower()
song.lyrics = unicode(sxml.extract_xml(), u'utf-8') song.lyrics = unicode(sxml.extract_xml(), u'utf-8')

View File

@ -134,7 +134,7 @@ class SongsPlugin(Plugin):
def onToolsReindexItemTriggered(self): def onToolsReindexItemTriggered(self):
""" """
Rebuild the search title of each song. Rebuild each song.
""" """
maxSongs = self.manager.get_object_count(Song) maxSongs = self.manager.get_object_count(Song)
progressDialog = QtGui.QProgressDialog( progressDialog = QtGui.QProgressDialog(
@ -150,8 +150,13 @@ class SongsPlugin(Plugin):
song.title = u'' song.title = u''
if song.alternate_title is None: if song.alternate_title is None:
song.alternate_title = u'' song.alternate_title = u''
song.search_title = self.whitespace.sub(u' ', song.title.lower() + \ song.search_title = self.whitespace.sub(u' ', song.title.lower() +
u' ' + song.alternate_title.lower()) u' ' + song.alternate_title.lower())
# Remove the "language" attribute from lyrics tag. This is not very
# important, but this keeps the database clean. This can be removed
# when everybody has run the reindex tool once.
song.lyrics = song.lyrics.replace(
u'<lyrics language="en">', u'<lyrics>')
lyrics = u'' lyrics = u''
verses = SongXML().get_verses(song.lyrics) verses = SongXML().get_verses(song.lyrics)
for verse in verses: for verse in verses:
@ -159,8 +164,7 @@ class SongsPlugin(Plugin):
song.search_lyrics = lyrics.lower() song.search_lyrics = lyrics.lower()
progressDialog.setValue(counter) progressDialog.setValue(counter)
self.manager.save_objects(songs) self.manager.save_objects(songs)
counter += 1 progressDialog.setValue(counter + 1)
progressDialog.setValue(counter)
self.mediaItem.displayResultsSong( self.mediaItem.displayResultsSong(
self.manager.get_all_objects(Song, order_by_ref=Song.search_title)) self.manager.get_all_objects(Song, order_by_ref=Song.search_title))