Import completely unlabelled songs. Import unreferenced verses

This commit is contained in:
Martin Thompson 2010-08-31 20:49:57 +01:00
parent 733bedebe8
commit b4cfea211e
4 changed files with 69 additions and 24 deletions

View File

@ -159,8 +159,11 @@ class OpenSongImport(SongImport):
Process the OpenSong file - pass in a file-like object,
not a filename
"""
self.authors = []
self.verse_order_list = []
self.set_defaults()
# Setup blank storage to append to
verse_order_list = []
topics = []
verselist = []
try:
tree = objectify.parse(file)
except Error, LxmlError:
@ -183,10 +186,10 @@ class OpenSongImport(SongImport):
setattr(self, fn_or_string, ustring)
else:
fn_or_string(ustring)
if u'theme' in fields and unicode(root.theme) not in self.topics:
self.topics.append(unicode(root.theme))
if u'alttheme' in fields and unicode(root.alttheme) not in self.topics:
self.topics.append(unicode(root.alttheme))
if u'theme' in fields and unicode(root.theme) not in topics:
topics.append(unicode(root.theme))
if u'alttheme' in fields and unicode(root.alttheme) not in topics:
topics.append(unicode(root.alttheme))
# data storage while importing
verses = {}
lyrics = unicode(root.lyrics)
@ -214,7 +217,7 @@ class OpenSongImport(SongImport):
versetype = thisline[1].upper()
if versetype.isdigit():
versenum = versetype
versetype = u'V'
versetype = u''
elif thisline[2] != u']':
# there's a number to go with it - extract that as well
right_bracket = thisline.find(u']')
@ -228,10 +231,13 @@ class OpenSongImport(SongImport):
if thisline[0].isdigit():
versenum = thisline[0]
words = thisline[1:].strip()
if words is None and \
versenum is not None and \
versetype is not None:
if words is None:# and \
#versenum is not None and \
#versetype is not None:
words = thisline
if not versenum:
versenum = u'1'
if versenum is not None:
versetag = u'%s%s' % (versetype, versenum)
if not verses.has_key(versetype):
@ -252,12 +258,15 @@ class OpenSongImport(SongImport):
versetypes.sort()
versetags = {}
for versetype in versetypes:
our_verse_type = versetype
if our_verse_type == u'':
our_verse_type = u'V'
versenums = verses[versetype].keys()
versenums.sort()
for num in versenums:
versetag = u'%s%s' % (versetype, num)
versetag = u'%s%s' % (our_verse_type, num)
lines = u'\n'.join(verses[versetype][num])
self.verses.append([versetag, lines])
verselist.append([versetag, lines])
# Keep track of what we have for error checking later
versetags[versetag] = 1
# now figure out the presentation order
@ -269,11 +278,18 @@ class OpenSongImport(SongImport):
if len(our_verse_order) > 0:
order = our_verse_order
else:
log.warn(u'No verse order available for %s, skipping.', self.title)
log.warn(u'No verse order available (either explicit or inferred) for %s, skipping.', self.title)
for tag in order:
if len(tag) == 1:
tag = tag + u'1' # Assume it's no.1 if it's not there
if not tag.isdigit():
tag = tag + u'1' # Assume it's no.1 if it's not there
else:
tag = u'V' + tag # Assume it's a verse if it has no prefix
if not versetags.has_key(tag):
log.warn(u'Got order %s but not in versetags, skipping', tag)
log.warn(u'Got order %s but not in versetags, dropping this item from presentation order', tag)
else:
self.verse_order_list.append(tag)
verse_order_list.append(tag)
# now copy the data
self.topics = topics
self.verse_order_list = verse_order_list
self.verses = verselist

View File

@ -52,6 +52,12 @@ class SongImport(QtCore.QObject):
"""
self.manager = manager
self.stop_import_flag = False
self.set_defaults()
def set_defaults(self):
"""
Create defaults for properties - call this before each song
if importing many songs at once to ensure a clean beginning
"""
self.title = u''
self.song_number = u''
self.alternate_title = u''

View File

@ -4,7 +4,7 @@
<author>MartiÑ Thómpson</author>
<copyright>2010 Martin Thompson</copyright>
<hymn_number>1</hymn_number>
<presentation>V1 C V2 C2 V3 B1 V1</presentation>
<presentation>V1 C V2 C2 3 B1 V1 T U</presentation>
<ccli>Blah</ccli>
<capo print="false"></capo>
<key></key>
@ -17,7 +17,12 @@
<alttheme>TestAltTheme</alttheme>
<tempo></tempo>
<time_sig></time_sig>
<lyrics>;Comment
<lyrics>[3]
. G A B
V3 Line 1
. G A B
V3 Line 2
. A B C
1 v1 Line 1___
2 v2 Line 1___
@ -25,10 +30,6 @@
1 V1 Line 2
2 V2 Line 2
[3]
V3 Line 1
V3 Line 2
[b1]
Bridge 1
---
@ -36,12 +37,18 @@
Bridge 1 line 2
[C]
. A B
. A B
Chorus 1
[C2]
. A B
Chorus 2
[T]
T Line 1
[X]
Unreferenced verse line 1
</lyrics>
<style index="default_style">
<title enabled="true" valign="bottom" align="center" include_verse="false" margin-left="0" margin-right="0" margin-top="0" margin-bottom="0" font="Helvetica" size="26" bold="true" italic="true" underline="false" color="#FFFFFF" border="true" border_color="#000000" shadow="true" shadow_color="#000000" fill="false" fill_color="#000000"/>

View File

@ -63,7 +63,9 @@ def test():
assert [u'B1', u'Bridge 1\nBridge 1 line 2'] in o.verses
assert [u'V1', u'v1 Line 1\nV1 Line 2'] in o.verses
assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.verses
assert o.verse_order_list == [u'V1', u'C1', u'V2', u'C2', u'V3', u'B1', u'V1']
assert [u'V3', u'V3 Line 1\nV3 Line 2'] in o.verses
assert [u'X1', u'Unreferenced verse line 1'] in o.verses
assert o.verse_order_list == [u'V1', u'C1', u'V2', u'C2', u'V3', u'B1', u'V1', u'T1']
assert o.ccli_number == u'Blah'
assert o.topics == [u'TestTheme', u'TestAltTheme']
@ -104,6 +106,20 @@ def test():
print o.verse_order_list
assert o.verse_order_list == [u'V1', u'V2', u'B1', u'C1', u'C2']
o.filenames = [u'test3.opensong']
o.do_import()
# o.finish()
o.print_song()
assert o.copyright == u'2010'
assert o.authors == [u'Martin Thompson']
assert o.title == u'Test single verse'
assert o.alternate_title == u''
assert o.ccli_number == u'123456'
assert o.verse_order_list == [u'V1']
assert o.topics == [u'Worship: Declaration']
print o.verses[0]
assert [u'V1', u'Line 1\nLine 2'] in o.verses
print "Tests passed"
pass