- fixed author wihtou first_name e. g. "Luther"

- open xml file properly
- tweakes
This commit is contained in:
Andreas Preikschat 2011-01-04 23:06:43 +01:00
parent cc229d05af
commit 449610d50e
2 changed files with 26 additions and 22 deletions

View File

@ -28,12 +28,17 @@ The :mod:`openlyricsimport` module provides the functionality for importing
songs which are saved as OpenLyrics files. songs which are saved as OpenLyrics files.
""" """
import logging
import os import os
from lxml import etree
from openlp.core.lib import translate from openlp.core.lib import translate
from openlp.plugins.songs.lib.songimport import SongImport from openlp.plugins.songs.lib.songimport import SongImport
from openlp.plugins.songs.lib import OpenLyricsParser from openlp.plugins.songs.lib import OpenLyricsParser
log = logging.getLogger(__name__)
class OpenLyricsImport(SongImport): class OpenLyricsImport(SongImport):
""" """
This provides the Openlyrics import. This provides the Openlyrics import.
@ -42,6 +47,7 @@ class OpenLyricsImport(SongImport):
""" """
Initialise the import. Initialise the import.
""" """
log.debug(u'initialise OpenLyricsImport')
SongImport.__init__(self, master_manager) SongImport.__init__(self, master_manager)
self.master_manager = master_manager self.master_manager = master_manager
self.openLyricsParser = OpenLyricsParser(master_manager) self.openLyricsParser = OpenLyricsParser(master_manager)
@ -58,15 +64,14 @@ class OpenLyricsImport(SongImport):
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
file = open(file_path)
lines = file.readlines()
file.close()
lines = [line.strip() for line in lines]
xml = u''.join(lines)
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)
file = etree.parse(file_path, parser)
xml = etree.tostring(file)
if self.openLyricsParser.xml_to_song(xml) == 0: if self.openLyricsParser.xml_to_song(xml) == 0:
log.debug(u'File could not be imported: %s' % file_path)
# Importing this song failed! For now we stop import. # Importing this song failed! For now we stop import.
return False return False
return True return True

View File

@ -248,7 +248,8 @@ class OpenLyricsParser(object):
This class represents the converter for Song to/from This class represents the converter for Song to/from
`OpenLyrics <http://openlyrics.info/>`_ XML. `OpenLyrics <http://openlyrics.info/>`_ XML.
""" """
# TODO: complete OpenLyrics standard implementation as fare as possible! # TODO: Complete OpenLyrics standard implementation and document what is
# supported and what not!
def __init__(self, manager): def __init__(self, manager):
self.manager = manager self.manager = manager
@ -316,8 +317,6 @@ class OpenLyricsParser(object):
song = Song() song = Song()
if xml[:5] == u'<?xml': if xml[:5] == u'<?xml':
xml = xml[38:] xml = xml[38:]
# Remove chords
xml = re.compile(u'<chord name=".*?"/>').sub(u'', xml)
song_xml = objectify.fromstring(xml) song_xml = objectify.fromstring(xml)
properties = song_xml.properties properties = song_xml.properties
# Process Copyright # Process Copyright
@ -349,17 +348,17 @@ class OpenLyricsParser(object):
for verse in lyrics.verse: for verse in lyrics.verse:
text = u'' text = u''
for line in verse.lines: for line in verse.lines:
for line in line.line: text = u'\n'.join([unicode(line) for line in line.line])
line = unicode(line) # Remove chords
if not text: text = re.compile(u'<chord name=".*?"/>').sub(u'', text)
text = line # OpenLyrics allows e. g. "c", but we need "c1".
else: if self._get(verse, u'name').isalpha():
text += u'\n' + line verse.set(u'name', self._get(verse, u'name') + u'1')
type = VerseType.expand_string(verse.attrib[u'name'][0]) type = VerseType.expand_string(self._get(verse, u'name')[0])
sxml.add_verse_to_lyrics(type, verse.attrib[u'name'][1], text) sxml.add_verse_to_lyrics(
# TODO: test this verse_order thing! type, self._get(verse, u'name')[1], text)
song.verse_order += u'%s%s ' % (type[0], song.verse_order += u'%s%s ' % (type[0],
verse.attrib[u'name'][1]) self._get(verse, u'name')[1])
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')
@ -399,7 +398,7 @@ class OpenLyricsParser(object):
try: try:
for songbook in properties.songbooks.songbook: for songbook in properties.songbooks.songbook:
self._process_songbook(self._get(songbook, u'name'), song) self._process_songbook(self._get(songbook, u'name'), song)
if songbook.get(u'entry'): if self._get(songbook, u'entry'):
song.song_number = self._get(songbook, u'entry') song.song_number = self._get(songbook, u'entry')
# OpenLp does only support one song book, so take the first one. # OpenLp does only support one song book, so take the first one.
break break
@ -427,7 +426,7 @@ class OpenLyricsParser(object):
The element's attribute (unicode). The element's attribute (unicode).
""" """
if element.get(attribute) is not None: if element.get(attribute) is not None:
return element.get(attribute) return unicode(element.get(attribute))
return u'' return u''
def _text(self, element): def _text(self, element):
@ -483,8 +482,8 @@ class OpenLyricsParser(object):
Author.display_name == name) Author.display_name == name)
if author is None: if author is None:
# We need to create a new author, as the author does not exist. # We need to create a new author, as the author does not exist.
author = Author.populate(first_name=name.rsplit(u' ', 1)[0], author = Author.populate(last_name=name.split(u' ')[-1],
last_name=name.rsplit(u' ', 1)[1], display_name=name) first_name=u' '.join(name.split(u' ')[:-1]), display_name=name)
self.manager.save_object(author) self.manager.save_object(author)
song.authors.append(author) song.authors.append(author)