forked from openlp/openlp
Changed internal 'song' attribute to clearer 'song_import'. OpenSong themes now map to Topics. Text cleaning called
This commit is contained in:
parent
bb34a304a9
commit
059032f237
@ -38,7 +38,7 @@ log = logging.getLogger(__name__)
|
||||
class OpenSongImportError(Exception):
|
||||
pass
|
||||
|
||||
class OpenSongImport:
|
||||
class OpenSongImport(object):
|
||||
"""
|
||||
Import songs exported from OpenSong - the format is described loosly here:
|
||||
http://www.opensong.org/d/manual/song_file_format_specification
|
||||
@ -130,27 +130,25 @@ class OpenSongImport:
|
||||
Process the OpenSong file - pass in a file-like object,
|
||||
not a filename
|
||||
"""
|
||||
self.song = SongImport(self.songmanager)
|
||||
self.song_import = SongImport(self.songmanager)
|
||||
tree = objectify.parse(file)
|
||||
root = tree.getroot()
|
||||
fields = dir(root)
|
||||
decode = {u'copyright':self.song.add_copyright,
|
||||
u'ccli':self.song.set_ccli_number,
|
||||
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}
|
||||
decode = {u'copyright':self.song_import.add_copyright,
|
||||
u'ccli':self.song_import.set_ccli_number,
|
||||
u'author':self.song_import.parse_author,
|
||||
u'title':self.song_import.set_title,
|
||||
u'aka':self.song_import.set_alternate_title,
|
||||
u'hymn_number':self.song_import.set_song_number}
|
||||
for (attr, fn) in decode.items():
|
||||
if attr in fields:
|
||||
fn(unicode(root.__getattr__(attr)))
|
||||
|
||||
res = []
|
||||
if u'theme' in fields:
|
||||
res.append(unicode(root.theme))
|
||||
self.song_import.topics.append(unicode(root.theme))
|
||||
if u'alttheme' in fields:
|
||||
res.append(unicode(root.alttheme))
|
||||
self.song.theme_name = u', '.join(res)
|
||||
|
||||
self.song_import.topics.append(unicode(root.alttheme))
|
||||
# data storage while importing
|
||||
verses = {}
|
||||
lyrics = unicode(root.lyrics)
|
||||
@ -207,7 +205,7 @@ class OpenSongImport:
|
||||
our_verse_order.append(versetag)
|
||||
if words:
|
||||
# Tidy text and remove the ____s from extended words
|
||||
# words=self.song.tidy_text(words)
|
||||
words=self.song_import.tidy_text(words)
|
||||
words=words.replace('_', '')
|
||||
verses[versetype][versenum].append(words)
|
||||
# done parsing
|
||||
@ -221,7 +219,7 @@ class OpenSongImport:
|
||||
for n in versenums:
|
||||
versetag = u'%s%s' %(v,n)
|
||||
lines = u'\n'.join(verses[v][n])
|
||||
self.song.verses.append([versetag, lines])
|
||||
self.song_import.verses.append([versetag, lines])
|
||||
versetags[versetag] = 1 # keep track of what we have for error checking later
|
||||
# now figure out the presentation order
|
||||
if u'presentation' in fields and root.presentation != u'':
|
||||
@ -236,7 +234,7 @@ class OpenSongImport:
|
||||
if not versetags.has_key(tag):
|
||||
log.warn(u'Got order %s but not in versetags, skipping', tag)
|
||||
else:
|
||||
self.song.verse_order_list.append(tag)
|
||||
self.song_import.verse_order_list.append(tag)
|
||||
def finish(self):
|
||||
""" Separate function, allows test suite to not pollute database"""
|
||||
self.song.finish()
|
||||
self.song_import.finish()
|
||||
|
@ -7,6 +7,7 @@ import os
|
||||
from traceback import print_exc
|
||||
import sys
|
||||
import codecs
|
||||
|
||||
def opensong_import_lots():
|
||||
ziploc = u'/home/mjt/openlp/OpenSong_Data/'
|
||||
files = []
|
||||
@ -34,7 +35,7 @@ def opensong_import_lots():
|
||||
o = OpenSongImport(manager)
|
||||
try:
|
||||
o.do_import_file(songfile)
|
||||
o.song.print_song()
|
||||
# o.song_import.print_song()
|
||||
except:
|
||||
print "Failure",
|
||||
|
||||
@ -51,6 +52,6 @@ def opensong_import_lots():
|
||||
#o.finish()
|
||||
print "OK"
|
||||
#os.unlink(filename)
|
||||
# o.song.print_song()
|
||||
# o.song_import.print_song()
|
||||
if __name__ == "__main__":
|
||||
opensong_import_lots()
|
||||
|
@ -33,55 +33,55 @@ def test():
|
||||
o = OpenSongImport(manager)
|
||||
o.do_import(u'test.opensong', commit=False)
|
||||
o.finish()
|
||||
o.song.print_song()
|
||||
assert o.song.copyright == u'2010 Martin Thompson'
|
||||
assert o.song.authors == [u'MartiÑ Thómpson']
|
||||
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'V3', u'B1', u'V1']
|
||||
assert o.song.ccli_number == u'Blah'
|
||||
assert o.song.theme_name == u'TestTheme, TestAltTheme'
|
||||
o.song_import.print_song()
|
||||
assert o.song_import.copyright == u'2010 Martin Thompson'
|
||||
assert o.song_import.authors == [u'MartiÑ Thómpson']
|
||||
assert o.song_import.title == u'Martins Test'
|
||||
assert o.song_import.alternate_title == u''
|
||||
assert o.song_import.song_number == u'1'
|
||||
assert [u'B1', u'Bridge 1\nBridge 1 line 2'] in o.song_import.verses
|
||||
assert [u'C1', u'Chorus 1'] in o.song_import.verses
|
||||
assert [u'C2', u'Chorus 2'] in o.song_import.verses
|
||||
assert not [u'C3', u'Chorus 3'] in o.song_import.verses
|
||||
assert [u'V1', u'v1 Line 1\nV1 Line 2'] in o.song_import.verses
|
||||
assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.song_import.verses
|
||||
assert o.song_import.verse_order_list == [u'V1', u'C1', u'V2', u'C2', u'V3', u'B1', u'V1']
|
||||
assert o.song_import.ccli_number == u'Blah'
|
||||
assert o.song_import.topics == [u'TestTheme', u'TestAltTheme']
|
||||
o.do_import(u'test.opensong.zip', commit=False)
|
||||
o.song_import.print_song()
|
||||
o.finish()
|
||||
o.song.print_song()
|
||||
assert o.song.copyright == u'2010 Martin Thompson'
|
||||
assert o.song.authors == [u'MartiÑ Thómpson']
|
||||
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'V3', u'B1', u'V1']
|
||||
assert o.song_import.copyright == u'2010 Martin Thompson'
|
||||
assert o.song_import.authors == [u'MartiÑ Thómpson']
|
||||
assert o.song_import.title == u'Martins Test'
|
||||
assert o.song_import.alternate_title == u''
|
||||
assert o.song_import.song_number == u'1'
|
||||
assert [u'B1', u'Bridge 1\nBridge 1 line 2'] in o.song_import.verses
|
||||
assert [u'C1', u'Chorus 1'] in o.song_import.verses
|
||||
assert [u'C2', u'Chorus 2'] in o.song_import.verses
|
||||
assert not [u'C3', u'Chorus 3'] in o.song_import.verses
|
||||
assert [u'V1', u'v1 Line 1\nV1 Line 2'] in o.song_import.verses
|
||||
assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.song_import.verses
|
||||
assert o.song_import.verse_order_list == [u'V1', u'C1', u'V2', u'C2', u'V3', u'B1', u'V1']
|
||||
|
||||
o = OpenSongImport(manager)
|
||||
o.do_import(u'test2.opensong', commit=False)
|
||||
# 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']
|
||||
o.song_import.print_song()
|
||||
assert o.song_import.copyright == u'2010 Martin Thompson'
|
||||
assert o.song_import.authors == [u'Martin Thompson']
|
||||
assert o.song_import.title == u'Martins 2nd Test'
|
||||
assert o.song_import.alternate_title == u''
|
||||
assert o.song_import.song_number == u'2'
|
||||
print o.song_import.verses
|
||||
assert [u'B1', u'Bridge 1\nBridge 1 line 2'] in o.song_import.verses
|
||||
assert [u'C1', u'Chorus 1'] in o.song_import.verses
|
||||
assert [u'C2', u'Chorus 2'] in o.song_import.verses
|
||||
assert not [u'C3', u'Chorus 3'] in o.song_import.verses
|
||||
assert [u'V1', u'v1 Line 1\nV1 Line 2'] in o.song_import.verses
|
||||
assert [u'V2', u'v2 Line 1\nV2 Line 2'] in o.song_import.verses
|
||||
print o.song_import.verse_order_list
|
||||
assert o.song_import.verse_order_list == [u'V1', u'V2', u'B1', u'C1', u'C2']
|
||||
|
||||
print "Tests passed"
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user