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.
"""
self.import_wizard.progressBar.setMaximum(len(self.import_source))
parser = etree.XMLParser(remove_blank_text=True)
for file_path in self.import_source:
if self.stop_import_flag:
return False
self.import_wizard.incrementProgressBar(unicode(translate(
'SongsPlugin.OpenLyricsImport', 'Importing %s...')) %
os.path.basename(file_path))
parser = etree.XMLParser(remove_blank_text=True)
parsed_file = etree.parse(file_path, parser)
try:
parsed_file = etree.parse(file_path, parser)
except etree.XMLSyntaxError:
return False
xml = unicode(etree.tostring(parsed_file))
if self.openLyrics.xml_to_song(xml) is None:
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"?>
<song version="1.0">
<lyrics>
<verse type="chorus" label="1">
<verse type="Chorus" label="1" lang="en">
<![CDATA[ ... ]]>
</verse>
</lyrics>
@ -84,7 +84,7 @@ class SongXML(object):
self.song_xml = objectify.fromstring(u'<song version="1.0" />')
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.
@ -97,9 +97,15 @@ class SongXML(object):
``content``
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),
label=unicode(number))
if lang:
verse.set(u'lang', lang)
verse.text = etree.CDATA(content)
self.lyrics.append(verse)
@ -117,6 +123,11 @@ class SongXML(object):
``xml``
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
if xml[:5] == u'<?xml':
@ -196,7 +207,7 @@ class OpenLyrics(object):
This property is not supported.
*<verse name="v1a" lang="he" translit="en">*
The attribute *translit* and *lang* are not supported.
The attribute *translit* is not supported.
*<verseOrder>*
OpenLP supports this property.
@ -442,7 +453,10 @@ class OpenLyrics(object):
if not verse_number:
verse_number = u'1'
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
song.search_lyrics = search_text.lower()
song.lyrics = unicode(sxml.extract_xml(), u'utf-8')

View File

@ -134,7 +134,7 @@ class SongsPlugin(Plugin):
def onToolsReindexItemTriggered(self):
"""
Rebuild the search title of each song.
Rebuild each song.
"""
maxSongs = self.manager.get_object_count(Song)
progressDialog = QtGui.QProgressDialog(
@ -150,8 +150,13 @@ class SongsPlugin(Plugin):
song.title = u''
if song.alternate_title is None:
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())
# 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''
verses = SongXML().get_verses(song.lyrics)
for verse in verses:
@ -159,8 +164,7 @@ class SongsPlugin(Plugin):
song.search_lyrics = lyrics.lower()
progressDialog.setValue(counter)
self.manager.save_objects(songs)
counter += 1
progressDialog.setValue(counter)
progressDialog.setValue(counter + 1)
self.mediaItem.displayResultsSong(
self.manager.get_all_objects(Song, order_by_ref=Song.search_title))