forked from openlp/openlp
Main class handles zipfiles
This commit is contained in:
parent
94553048e3
commit
02fd6fe4da
@ -30,6 +30,11 @@ from songimport import SongImport
|
||||
from lxml.etree import Element
|
||||
from lxml import objectify
|
||||
|
||||
from zipfile import ZipFile
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class OpenSongImportError(Exception):
|
||||
pass
|
||||
|
||||
@ -89,10 +94,26 @@ class OpenSongImport:
|
||||
self.songmanager = songmanager
|
||||
self.song = None
|
||||
|
||||
def do_import(self, filename):
|
||||
file = open(filename)
|
||||
self.do_import_file(file)
|
||||
|
||||
def do_import(self, filename, commit=True):
|
||||
ext=os.path.splitext(filename)[1]
|
||||
if ext.lower() == ".zip":
|
||||
log.info('Zipfile found %s', filename)
|
||||
z=ZipFile(filename, u'r')
|
||||
for song in z.infolist():
|
||||
parts=os.path.split(song.filename)
|
||||
if parts[-1] == u'':
|
||||
#No final part => directory
|
||||
continue
|
||||
songfile=z.open(song)
|
||||
self.do_import_file(songfile)
|
||||
if commit:
|
||||
self.finish()
|
||||
else:
|
||||
log.info('Direct import %s', filename)
|
||||
file = open(filename)
|
||||
self.do_import_file(file)
|
||||
if commit:
|
||||
self.finish()
|
||||
def do_import_file(self, file):
|
||||
"""
|
||||
Process the OpenSong file
|
||||
|
@ -27,9 +27,9 @@ def opensong_import_lots():
|
||||
#No final part => directory
|
||||
continue
|
||||
print " ", file, ":",filename,
|
||||
# songfile=z.open(song)
|
||||
z.extract(song)
|
||||
songfile=open(filename, u'r')
|
||||
songfile=z.open(song)
|
||||
#z.extract(song)
|
||||
#songfile=open(filename, u'r')
|
||||
o=OpenSongImport(manager)
|
||||
try:
|
||||
o.do_import_file(songfile)
|
||||
@ -49,7 +49,7 @@ def opensong_import_lots():
|
||||
# continue
|
||||
# o.finish()
|
||||
print "OK"
|
||||
os.unlink(filename)
|
||||
#os.unlink(filename)
|
||||
# o.song.print_song()
|
||||
if __name__=="__main__":
|
||||
opensong_import_lots()
|
||||
|
@ -28,7 +28,23 @@ from openlp.plugins.songs.lib.manager import SongManager
|
||||
def test():
|
||||
manager = SongManager()
|
||||
o = OpenSongImport(manager)
|
||||
o.do_import(u'test.opensong')
|
||||
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'C', 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'C', u'V2', u'C2', u'V3', u'B1', u'V1']
|
||||
|
||||
o.do_import(u'test.opensong.zip', commit=False)
|
||||
# o.finish()
|
||||
o.song.print_song()
|
||||
assert o.song.copyright == u'2010 Martin Thompson'
|
||||
@ -45,7 +61,7 @@ def test():
|
||||
assert o.song.verse_order_list == [u'V1', u'C', u'V2', u'C2', u'V3', u'B1', u'V1']
|
||||
|
||||
o = OpenSongImport(manager)
|
||||
o.do_import(u'test2.opensong')
|
||||
o.do_import(u'test2.opensong', commit=False)
|
||||
# o.finish()
|
||||
o.song.print_song()
|
||||
assert o.song.copyright == u'2010 Martin Thompson'
|
||||
|
Loading…
Reference in New Issue
Block a user