First test looks promsing

This commit is contained in:
Martin Thompson 2010-06-15 22:25:50 +01:00
parent 2f077c3982
commit fc27f8107e
4 changed files with 99 additions and 31 deletions

View File

@ -97,3 +97,4 @@ from mediaitem import SongMediaItem
from sofimport import SofImport from sofimport import SofImport
from oooimport import OooImport from oooimport import OooImport
from songimport import SongImport from songimport import SongImport
from opensongimport import OpenSongImport

View File

@ -26,9 +26,11 @@
import os import os
import re 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: Import songs exported from OpenSong - the format is described loosly here:
http://www.opensong.org/d/manual/song_file_format_specification http://www.opensong.org/d/manual/song_file_format_specification
@ -82,35 +84,94 @@ class opensongimport:
self.songmanager=songmanager self.songmanager=songmanager
self.song = None self.song = None
def osimport(self, filename): def do_import(self, filename):
""" """
Process the OpenSong file Process the OpenSong file
""" """
self.new_song() self.song = SongImport(self.songmanager)
f=open(filename) f=open(filename)
tree=objectify.parse(f) tree=objectify.parse(f)
root=tree.getroot() 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 # data storage while importing
self.verses=[] verses={}
lyrics=str(root.lyrics)
# xxx what to do if no presentation order - need to figure it out on the fly
# xxx this is common with SOF for l in lyrics.split('\n'):
def new_song(self): # remove comments
""" semicolon = l.find(';')
A change of song. Store the old, create a new if semicolon >= 0:
... but only if the last song was complete. If not, stick with it l=l[:semicolon]
""" l=l.strip()
if self.song: if l=='':
self.finish_verse() continue
if not self.song.check_complete(): # skip inline guitar chords
return if l[0] == u'.':
self.song.finish() continue
self.song = SongImport(self.manager) # verse/chorus/etc. marker
self.skip_to_close_bracket = False if l[0] == u'[':
self.is_chorus = False versetype=l[1].upper()
self.italics = False if not verses.has_key(versetype):
self.currentverse = u'' 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()

View File

@ -23,11 +23,11 @@
2 v2 Line 1___ 2 v2 Line 1___
. A B C7 . A B C7
1 V1 Line 2 1 V1 Line 2
2 V1 Line 2 2 V2 Line 2
[b1] [b1]
Bridge 1 Bridge 1
Bridge 1 line 2
[C1] [C1]
Chorus 1 Chorus 1
@ -42,4 +42,4 @@
<tabs/> <tabs/>
</body> </body>
<background strip_footer="0" color="#408080" position="1"/> <background strip_footer="0" color="#408080" position="1"/>
</style></song> </style></song>

View File

@ -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(): def test():
o=opensongimport.opensongimport(0)# xxx needs a song manager here manager=SongManager()
o.osimport(u'test.opensong') 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 pass
if __name__=="__main__": if __name__=="__main__":