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
|
self.song = None
|
||||||
|
|
||||||
def do_import(self, filename):
|
def do_import(self, filename):
|
||||||
|
file=open(filename)
|
||||||
|
self.do_import_file(file)
|
||||||
|
|
||||||
|
def do_import_file(self, file):
|
||||||
"""
|
"""
|
||||||
Process the OpenSong file
|
Process the OpenSong file
|
||||||
"""
|
"""
|
||||||
self.song = SongImport(self.songmanager)
|
self.song = SongImport(self.songmanager)
|
||||||
f=open(filename)
|
tree=objectify.parse(file)
|
||||||
tree=objectify.parse(f)
|
|
||||||
root=tree.getroot()
|
root=tree.getroot()
|
||||||
# xxx this bit ought to be more "iterable"... esp. if song had attributes not getters and setters...
|
fields=dir(root)
|
||||||
if root.copyright:
|
decode={u'copyright':self.song.add_copyright,
|
||||||
self.song.add_copyright(unicode(root.copyright))
|
u'author':self.song.parse_author,
|
||||||
if root.author:
|
u'title':self.song.set_title,
|
||||||
self.song.parse_author(unicode(root.author))
|
u'aka':self.song.set_alternate_title,
|
||||||
if root.title:
|
u'hymn_number':self.song.set_song_number}
|
||||||
self.song.set_title(unicode(root.title))
|
for (attr, fn) in decode.items():
|
||||||
if root.aka:
|
if attr in fields:
|
||||||
self.song.set_alternate_title(unicode(root.aka))
|
fn(unicode(root.__getattr__(attr)))
|
||||||
if root.hymn_number:
|
|
||||||
self.song.set_song_number(unicode(root.hymn_number))
|
|
||||||
|
|
||||||
# data storage while importing
|
# data storage while importing
|
||||||
verses={}
|
verses={}
|
||||||
lyrics=str(root.lyrics)
|
lyrics=unicode(root.lyrics)
|
||||||
# xxx what to do if no presentation order - need to figure it out on the fly
|
# 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'):
|
for l in lyrics.split('\n'):
|
||||||
# remove comments
|
# remove comments
|
||||||
semicolon = l.find(';')
|
semicolon = l.find(';')
|
||||||
@ -123,35 +128,39 @@ class OpenSongImport:
|
|||||||
# verse/chorus/etc. marker
|
# verse/chorus/etc. marker
|
||||||
if l[0] == u'[':
|
if l[0] == u'[':
|
||||||
versetype=l[1].upper()
|
versetype=l[1].upper()
|
||||||
if not verses.has_key(versetype):
|
|
||||||
verses[versetype]={}
|
|
||||||
if l[2] != u']':
|
if l[2] != u']':
|
||||||
# there's a number to go with it - extract that as well
|
# there's a number to go with it - extract that as well
|
||||||
right_bracket=l.find(u']')
|
right_bracket=l.find(u']')
|
||||||
versenum=int(l[2:right_bracket])
|
versenum=int(l[2:right_bracket])
|
||||||
|
versetag=u'%s%d'%(versetype,versenum)
|
||||||
else:
|
else:
|
||||||
versenum = None # allow error trap
|
versenum = None # allow error trap
|
||||||
continue
|
continue
|
||||||
words=None
|
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':
|
if l[0] >= u'0' and l[0] <= u'9':
|
||||||
versenum=int(l[0])
|
versenum=int(l[0])
|
||||||
words=l[1:].strip()
|
words=l[1:].strip()
|
||||||
|
versetag=u'%s%d'%(versetype,versenum)
|
||||||
if words is None and \
|
if words is None and \
|
||||||
versenum is not None and \
|
versenum is not None and \
|
||||||
versetype is not None:
|
versetype is not None:
|
||||||
words=l
|
words=l
|
||||||
if versenum is not None and \
|
if versenum is not None:
|
||||||
not verses[versetype].has_key(versenum):
|
if not verses.has_key(versetype):
|
||||||
|
verses[versetype]={}
|
||||||
|
if not verses[versetype].has_key(versenum):
|
||||||
verses[versetype][versenum]=[] # storage for lines in this verse
|
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:
|
if words:
|
||||||
# remove the ____s from extended words
|
# Tidy text and remove the ____s from extended words
|
||||||
words=words.replace(u'_', u'')
|
# words=self.song.tidy_text(words)
|
||||||
|
words=words.replace('_', '')
|
||||||
verses[versetype][versenum].append(words)
|
verses[versetype][versenum].append(words)
|
||||||
# done parsing
|
# done parsing
|
||||||
print u'Title:', root.title
|
|
||||||
versetypes=verses.keys()
|
versetypes=verses.keys()
|
||||||
versetypes.sort()
|
versetypes.sort()
|
||||||
versetags={}
|
versetags={}
|
||||||
@ -164,14 +173,17 @@ class OpenSongImport:
|
|||||||
self.song.verses.append([versetag, lines])
|
self.song.verses.append([versetag, lines])
|
||||||
versetags[versetag]=1 # keep track of what we have for error checking later
|
versetags[versetag]=1 # keep track of what we have for error checking later
|
||||||
# now figure out the presentation order
|
# now figure out the presentation order
|
||||||
if root.presentation:
|
if 'presentation' in fields and root.presentation != u'':
|
||||||
order=unicode(root.presentation).split(u' ')
|
order=unicode(root.presentation)
|
||||||
|
order=order.split()
|
||||||
|
else:
|
||||||
|
assert len(our_verse_order)>0
|
||||||
|
order=our_verse_order
|
||||||
for tag in order:
|
for tag in order:
|
||||||
if not versetags.has_key(tag):
|
if not versetags.has_key(tag):
|
||||||
raise OpenSongImportError
|
raise OpenSongImportError
|
||||||
else:
|
else:
|
||||||
self.song.verse_order_list.append(tag)
|
self.song.verse_order_list.append(tag)
|
||||||
|
def finish(self):
|
||||||
|
""" Separate function, allows test suite to not pollute database"""
|
||||||
self.song.print_song()
|
self.song.finish()
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
<tempo></tempo>
|
<tempo></tempo>
|
||||||
<time_sig></time_sig>
|
<time_sig></time_sig>
|
||||||
<lyrics>;Comment
|
<lyrics>;Comment
|
||||||
[V]
|
|
||||||
. A B C
|
. A B C
|
||||||
1 v1 Line 1___
|
1 v1 Line 1___
|
||||||
2 v2 Line 1___
|
2 v2 Line 1___
|
||||||
|
@ -5,10 +5,41 @@ def test():
|
|||||||
manager=SongManager()
|
manager=SongManager()
|
||||||
o=OpenSongImport(manager)
|
o=OpenSongImport(manager)
|
||||||
o.do_import(u'test.opensong')
|
o.do_import(u'test.opensong')
|
||||||
# xxx need some more asserts in here to test it...
|
# o.finish()
|
||||||
assert (1)
|
o.song.print_song()
|
||||||
# now to XML
|
assert o.song.copyright == u'2010 Martin Thompson'
|
||||||
# asserts
|
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
|
pass
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user