Added GUI for importing OpenSong

This commit is contained in:
Martin Thompson 2010-06-30 22:19:18 +01:00
parent 02fd6fe4da
commit 38ccc21f89
4 changed files with 53 additions and 12 deletions

View File

@ -95,6 +95,10 @@ class OpenSongImport:
self.song = None self.song = None
def do_import(self, filename, commit=True): def do_import(self, filename, commit=True):
"""
Import either a single opensong file, or a zipfile containing multiple opensong files
If the commit parameter is set False, the import will not be committed to the database (useful for test scripts)
"""
ext=os.path.splitext(filename)[1] ext=os.path.splitext(filename)[1]
if ext.lower() == ".zip": if ext.lower() == ".zip":
log.info('Zipfile found %s', filename) log.info('Zipfile found %s', filename)
@ -116,7 +120,7 @@ class OpenSongImport:
self.finish() self.finish()
def do_import_file(self, file): def do_import_file(self, file):
""" """
Process the OpenSong file Process the OpenSong file - pass in a file-like object, not a filename
""" """
self.song = SongImport(self.songmanager) self.song = SongImport(self.songmanager)
tree = objectify.parse(file) tree = objectify.parse(file)
@ -209,8 +213,7 @@ class OpenSongImport:
order = our_verse_order order = our_verse_order
for tag in order: for tag in order:
if not versetags.has_key(tag): if not versetags.has_key(tag):
print u'Got order', tag, u'but not in versetags, skipping' log.warn(u'Got order %s but not in versetags, skipping', tag)
raise OpenSongImportError # xxx keep error, or just warn?
else: else:
self.song.verse_order_list.append(tag) self.song.verse_order_list.append(tag)
def finish(self): def finish(self):

View File

@ -302,8 +302,7 @@ class SongImport(object):
song.theme_name = self.theme_name song.theme_name = self.theme_name
song.ccli_number = self.ccli_number song.ccli_number = self.ccli_number
for authortext in self.authors: for authortext in self.authors:
filter_string = u'display_name=%s' % authortext author = self.manager.get_object_filtered(Author, Author.display_name == authortext)
author = self.manager.get_object_filtered(Author, filter_string)
if author is None: if author is None:
author = Author() author = Author()
author.display_name = authortext author.display_name = authortext
@ -312,8 +311,7 @@ class SongImport(object):
self.manager.save_object(author) self.manager.save_object(author)
song.authors.append(author) song.authors.append(author)
if self.song_book_name: if self.song_book_name:
filter_string = u'name=%s' % self.song_book_name song_book = self.manager.get_object_filtered(Book, Book.name == self.song_book_name)
song_book = self.manager.get_object_filtered(Book, filter_string)
if song_book is None: if song_book is None:
song_book = Book() song_book = Book()
song_book.name = self.song_book_name song_book.name = self.song_book_name
@ -321,8 +319,7 @@ class SongImport(object):
self.manager.save_object(song_book) self.manager.save_object(song_book)
song.song_book_id = song_book.id song.song_book_id = song_book.id
for topictext in self.topics: for topictext in self.topics:
filter_string = u'name=%s' % topictext topic = self.manager.get_object_filtered(Topic.name == topictext)
topic = self.manager.get_object_filtered(Topic, filter_string)
if topic is None: if topic is None:
topic = Topic() topic = Topic()
topic.name = topictext topic.name = topictext

View File

@ -10,8 +10,8 @@ def opensong_import_lots():
ziploc=u'/home/mjt/openlp/OpenSong_Data/' ziploc=u'/home/mjt/openlp/OpenSong_Data/'
files=[] files=[]
files=['test.opensong.zip'] files=['test.opensong.zip']
# files.extend(glob(ziploc+u'Songs.zip')) files.extend(glob(ziploc+u'Songs.zip'))
# files.extend(glob(ziploc+u'SOF.zip')) files.extend(glob(ziploc+u'SOF.zip'))
# files.extend(glob(ziploc+u'spanish_songs_for_opensong.zip')) # files.extend(glob(ziploc+u'spanish_songs_for_opensong.zip'))
# files.extend(glob(ziploc+u'opensong_*.zip')) # files.extend(glob(ziploc+u'opensong_*.zip'))
errfile=codecs.open(u'import_lots_errors.txt', u'w', u'utf8') errfile=codecs.open(u'import_lots_errors.txt', u'w', u'utf8')
@ -47,7 +47,7 @@ def opensong_import_lots():
print_exc(3) print_exc(3)
sys.exit(1) sys.exit(1)
# continue # continue
# o.finish() o.finish()
print "OK" print "OK"
#os.unlink(filename) #os.unlink(filename)
# o.song.print_song() # o.song.print_song()

View File

@ -38,6 +38,8 @@ try:
except ImportError: except ImportError:
OOo_available = False OOo_available = False
from openlp.plugins.songs.lib import OpenSongImport
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class SongsPlugin(Plugin): class SongsPlugin(Plugin):
@ -143,6 +145,25 @@ class SongsPlugin(Plugin):
QtCore.SIGNAL(u'triggered()'), self.onImportSofItemClick) QtCore.SIGNAL(u'triggered()'), self.onImportSofItemClick)
QtCore.QObject.connect(self.ImportOooItem, QtCore.QObject.connect(self.ImportOooItem,
QtCore.SIGNAL(u'triggered()'), self.onImportOooItemClick) QtCore.SIGNAL(u'triggered()'), self.onImportOooItemClick)
# OpenSong import menu item - will be removed and the
# functionality will be contained within the import wizard
self.ImportOpenSongItem = QtGui.QAction(import_menu)
self.ImportOpenSongItem.setObjectName(u'ImportOpenSongItem')
self.ImportOpenSongItem.setText(
translate('SongsPlugin',
'OpenSong (temp menu item)'))
self.ImportOpenSongItem.setToolTip(
translate('SongsPlugin',
'Import songs from OpenSong files' +
'(either raw text or ZIPfiles)'))
self.ImportOpenSongItem.setStatusTip(
translate('SongsPlugin',
'Import songs from OpenSong files' +
'(either raw text or ZIPfiles)'))
import_menu.addAction(self.ImportOpenSongItem)
QtCore.QObject.connect(self.ImportOpenSongItem,
QtCore.SIGNAL(u'triggered()'), self.onImportOpenSongItemClick)
def add_export_menu_item(self, export_menu): def add_export_menu_item(self, export_menu):
""" """
@ -183,6 +204,26 @@ class SongsPlugin(Plugin):
QtGui.QMessageBox.Ok) QtGui.QMessageBox.Ok)
Receiver.send_message(u'songs_load_list') Receiver.send_message(u'songs_load_list')
def onImportOpenSongItemClick(self):
filenames = QtGui.QFileDialog.getOpenFileNames(
None, translate('SongsPlugin',
'Open OpenSong file'),
u'', u'OpenSong file (*. *.zip *.ZIP)')
try:
for filename in filenames:
importer = OpenSongImport(self.manager)
importer.do_import(unicode(filename))
except:
log.exception('Could not import OpenSong file')
QtGui.QMessageBox.critical(None,
translate('SongsPlugin',
'Import Error'),
translate('SongsPlugin',
'Error importing OpenSong file'),
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok),
QtGui.QMessageBox.Ok)
Receiver.send_message(u'songs_load_list')
def onImportOooItemClick(self): def onImportOooItemClick(self):
filenames = QtGui.QFileDialog.getOpenFileNames( filenames = QtGui.QFileDialog.getOpenFileNames(
None, translate('SongsPlugin', None, translate('SongsPlugin',