forked from openlp/openlp
Handles songs without a preliminary V tag
This commit is contained in:
parent
3417d85a58
commit
e8a397d3ec
@ -85,29 +85,34 @@ class OpenSongImport:
|
||||
self.song = None
|
||||
|
||||
def do_import(self, filename):
|
||||
file=open(filename)
|
||||
self.do_import_file(file)
|
||||
|
||||
def do_import_file(self, file):
|
||||
"""
|
||||
Process the OpenSong file
|
||||
"""
|
||||
self.song = SongImport(self.songmanager)
|
||||
f=open(filename)
|
||||
tree=objectify.parse(f)
|
||||
tree=objectify.parse(file)
|
||||
root=tree.getroot()
|
||||
# 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))
|
||||
fields=dir(root)
|
||||
decode={u'copyright':self.song.add_copyright,
|
||||
u'author':self.song.parse_author,
|
||||
u'title':self.song.set_title,
|
||||
u'aka':self.song.set_alternate_title,
|
||||
u'hymn_number':self.song.set_song_number}
|
||||
for (attr, fn) in decode.items():
|
||||
if attr in fields:
|
||||
fn(unicode(root.__getattr__(attr)))
|
||||
|
||||
# data storage while importing
|
||||
verses={}
|
||||
lyrics=str(root.lyrics)
|
||||
# xxx what to do if no presentation order - need to figure it out on the fly
|
||||
lyrics=unicode(root.lyrics)
|
||||
# keep track of a "default" verse order, in case none is specified
|
||||
our_verse_order=[]
|
||||
verses_seen={}
|
||||
# in the absence of any other indication, verses are the default, erm, versetype!
|
||||
versetype=u'V'
|
||||
for l in lyrics.split('\n'):
|
||||
# remove comments
|
||||
semicolon = l.find(';')
|
||||
@ -123,35 +128,39 @@ class OpenSongImport:
|
||||
# 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])
|
||||
versetag=u'%s%d'%(versetype,versenum)
|
||||
else:
|
||||
versenum = None # allow error trap
|
||||
continue
|
||||
words=None
|
||||
|
||||
# number at start of line => verse number
|
||||
# number at start of line.. it's verse number
|
||||
if l[0] >= u'0' and l[0] <= u'9':
|
||||
versenum=int(l[0])
|
||||
words=l[1:].strip()
|
||||
|
||||
versetag=u'%s%d'%(versetype,versenum)
|
||||
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 versenum is not None:
|
||||
if not verses.has_key(versetype):
|
||||
verses[versetype]={}
|
||||
if not verses[versetype].has_key(versenum):
|
||||
verses[versetype][versenum]=[] # storage for lines in this verse
|
||||
if not verses_seen.has_key(versetag):
|
||||
verses_seen[versetag] = 1
|
||||
our_verse_order.append(versetag)
|
||||
if words:
|
||||
# remove the ____s from extended words
|
||||
words=words.replace(u'_', u'')
|
||||
# Tidy text and remove the ____s from extended words
|
||||
# words=self.song.tidy_text(words)
|
||||
words=words.replace('_', '')
|
||||
verses[versetype][versenum].append(words)
|
||||
# done parsing
|
||||
print u'Title:', root.title
|
||||
versetypes=verses.keys()
|
||||
versetypes.sort()
|
||||
versetags={}
|
||||
@ -164,14 +173,17 @@ class OpenSongImport:
|
||||
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()
|
||||
|
||||
if 'presentation' in fields and root.presentation != u'':
|
||||
order=unicode(root.presentation)
|
||||
order=order.split()
|
||||
else:
|
||||
assert len(our_verse_order)>0
|
||||
order=our_verse_order
|
||||
for tag in order:
|
||||
if not versetags.has_key(tag):
|
||||
raise OpenSongImportError
|
||||
else:
|
||||
self.song.verse_order_list.append(tag)
|
||||
def finish(self):
|
||||
""" Separate function, allows test suite to not pollute database"""
|
||||
self.song.finish()
|
||||
|
@ -17,7 +17,6 @@
|
||||
<tempo></tempo>
|
||||
<time_sig></time_sig>
|
||||
<lyrics>;Comment
|
||||
[V]
|
||||
. A B C
|
||||
1 v1 Line 1___
|
||||
2 v2 Line 1___
|
||||
|
@ -5,10 +5,41 @@ def test():
|
||||
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
|
||||
# o.finish()
|
||||
o.song.print_song()
|
||||
assert o.song.copyright == u'2010 Martin Thompson'
|
||||
assert o.song.authors == [u'Martin Thompson']
|
||||
assert o.song.title == u'Martins Test'
|
||||
assert o.song.alternate_title == u''
|
||||
assert o.song.song_number == u'1'
|
||||
assert [u'B1', u'Bridge 1\nBridge 1 line 2'] in o.song.verses
|
||||
assert [u'C1', u'Chorus 1'] in o.song.verses
|
||||
assert [u'C2', u'Chorus 2'] in o.song.verses
|
||||
assert not [u'C3', u'Chorus 3'] in o.song.verses
|
||||
assert [u'V1', u'v1 Line 1\nV1 Line 2'] in o.song.verses
|
||||
assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.song.verses
|
||||
assert o.song.verse_order_list == [u'V1', u'C1', u'V2', u'C2', u'B1', u'V1']
|
||||
|
||||
o=OpenSongImport(manager)
|
||||
o.do_import(u'test2.opensong')
|
||||
# o.finish()
|
||||
o.song.print_song()
|
||||
assert o.song.copyright == u'2010 Martin Thompson'
|
||||
assert o.song.authors == [u'Martin Thompson']
|
||||
assert o.song.title == u'Martins 2nd Test'
|
||||
assert o.song.alternate_title == u''
|
||||
assert o.song.song_number == u'2'
|
||||
print o.song.verses
|
||||
assert [u'B1', u'Bridge 1\nBridge 1 line 2'] in o.song.verses
|
||||
assert [u'C1', u'Chorus 1'] in o.song.verses
|
||||
assert [u'C2', u'Chorus 2'] in o.song.verses
|
||||
assert not [u'C3', u'Chorus 3'] in o.song.verses
|
||||
assert [u'V1', u'v1 Line 1\nV1 Line 2'] in o.song.verses
|
||||
assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.song.verses
|
||||
print o.song.verse_order_list
|
||||
assert o.song.verse_order_list == [u'V1', u'V2', u'B1', u'C1', u'C2']
|
||||
|
||||
print "Tests passed"
|
||||
pass
|
||||
|
||||
if __name__=="__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user