From fc27f8107e7b042043fc3145526aaae888b28188 Mon Sep 17 00:00:00 2001 From: Martin Thompson Date: Tue, 15 Jun 2010 22:25:50 +0100 Subject: [PATCH] First test looks promsing --- openlp/plugins/songs/lib/__init__.py | 1 + openlp/plugins/songs/lib/opensongimport.py | 111 ++++++++++++++---- openlp/plugins/songs/lib/test.opensong | 6 +- .../plugins/songs/lib/test_opensongimport.py | 12 +- 4 files changed, 99 insertions(+), 31 deletions(-) diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index 6c637ea9e..37e738664 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -97,3 +97,4 @@ from mediaitem import SongMediaItem from sofimport import SofImport from oooimport import OooImport from songimport import SongImport +from opensongimport import OpenSongImport diff --git a/openlp/plugins/songs/lib/opensongimport.py b/openlp/plugins/songs/lib/opensongimport.py index 4dcc7f20d..7b066c858 100644 --- a/openlp/plugins/songs/lib/opensongimport.py +++ b/openlp/plugins/songs/lib/opensongimport.py @@ -26,9 +26,11 @@ import os import re -# from songimport import SongImport +from songimport import SongImport +from lxml.etree import Element +from lxml import objectify -class opensongimport: +class OpenSongImport: """ Import songs exported from OpenSong - the format is described loosly here: http://www.opensong.org/d/manual/song_file_format_specification @@ -82,35 +84,94 @@ class opensongimport: self.songmanager=songmanager self.song = None - def osimport(self, filename): + def do_import(self, filename): """ Process the OpenSong file """ - self.new_song() + self.song = SongImport(self.songmanager) f=open(filename) tree=objectify.parse(f) root=tree.getroot() - print "Title", zroot.title + # xxx this bit ought to be more "iterable"... esp. if song had attributes not getters and setters... + if root.copyright: + self.song.add_copyright(unicode(root.copyright)) + if root.author: + self.song.parse_author(unicode(root.author)) + if root.title: + self.song.set_title(unicode(root.title)) + if root.aka: + self.song.set_alternate_title(unicode(root.aka)) + if root.hymn_number: + self.song.set_song_number(unicode(root.hymn_number)) + # data storage while importing - self.verses=[] - - - # xxx this is common with SOF - def new_song(self): - """ - A change of song. Store the old, create a new - ... but only if the last song was complete. If not, stick with it - """ - if self.song: - self.finish_verse() - if not self.song.check_complete(): - return - self.song.finish() - - self.song = SongImport(self.manager) - self.skip_to_close_bracket = False - self.is_chorus = False - self.italics = False - self.currentverse = u'' + verses={} + lyrics=str(root.lyrics) + # xxx what to do if no presentation order - need to figure it out on the fly + for l in lyrics.split('\n'): + # remove comments + semicolon = l.find(';') + if semicolon >= 0: + l=l[:semicolon] + l=l.strip() + if l=='': + continue + # skip inline guitar chords + if l[0] == u'.': + continue + + # verse/chorus/etc. marker + if l[0] == u'[': + versetype=l[1].upper() + if not verses.has_key(versetype): + verses[versetype]={} + if l[2] != u']': + # there's a number to go with it - extract that as well + right_bracket=l.find(u']') + versenum=int(l[2:right_bracket]) + else: + versenum = None # allow error trap + continue + words=None + + # number at start of line => verse number + if l[0] >= u'0' and l[0] <= u'9': + versenum=int(l[0]) + words=l[1:].strip() + + if words is None and \ + versenum is not None and \ + versetype is not None: + words=l + if versenum is not None and \ + not verses[versetype].has_key(versenum): + verses[versetype][versenum]=[] # storage for lines in this verse + if words: + # remove the ____s from extended words + words=words.replace(u'_', u'') + verses[versetype][versenum].append(words) + # done parsing + print u'Title:', root.title + versetypes=verses.keys() + versetypes.sort() + versetags={} + for v in versetypes: + versenums=verses[v].keys() + versenums.sort() + for n in versenums: + versetag= u'%s%s' %(v,n) + lines=u'\n'.join(verses[v][n]) + self.song.verses.append([versetag, lines]) + versetags[versetag]=1 # keep track of what we have for error checking later + # now figure out the presentation order + if root.presentation: + order=unicode(root.presentation).split(u' ') + for tag in order: + if not versetags.has_key(tag): + raise OpenSongImportError + else: + self.song.verse_order_list.append(tag) + + self.song.print_song() diff --git a/openlp/plugins/songs/lib/test.opensong b/openlp/plugins/songs/lib/test.opensong index 0fde072ca..20206cecb 100644 --- a/openlp/plugins/songs/lib/test.opensong +++ b/openlp/plugins/songs/lib/test.opensong @@ -23,11 +23,11 @@ 2 v2 Line 1___ . A B C7 1 V1 Line 2 -2 V1 Line 2 +2 V2 Line 2 [b1] Bridge 1 - + Bridge 1 line 2 [C1] Chorus 1 @@ -42,4 +42,4 @@ - \ No newline at end of file + diff --git a/openlp/plugins/songs/lib/test_opensongimport.py b/openlp/plugins/songs/lib/test_opensongimport.py index 79b8e9a69..655fc83cc 100644 --- a/openlp/plugins/songs/lib/test_opensongimport.py +++ b/openlp/plugins/songs/lib/test_opensongimport.py @@ -1,8 +1,14 @@ -import openlp.plugins.songs.lib.opensongimport +from openlp.plugins.songs.lib.opensongimport import OpenSongImport +from openlp.plugins.songs.lib.manager import SongManager def test(): - o=opensongimport.opensongimport(0)# xxx needs a song manager here - o.osimport(u'test.opensong') + manager=SongManager() + o=OpenSongImport(manager) + o.do_import(u'test.opensong') + # xxx need some more asserts in here to test it... + assert (1) + # now to XML + # asserts pass if __name__=="__main__":