Finally done the openlp.org 1.x song importer.

This commit is contained in:
Raoul Snyman 2010-09-06 22:33:28 +02:00
parent 5e61bf938e
commit f82513f11d
3 changed files with 76 additions and 41 deletions

View File

@ -58,40 +58,56 @@ class OpenLP1SongImport(SongImport):
""" """
Run the import for an openlp.org 1.x song database. Run the import for an openlp.org 1.x song database.
""" """
# Connect to the database
connection = sqlite.connect(self.import_source) connection = sqlite.connect(self.import_source)
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute(u'SELECT COUNT(authorid) FROM authors') # Count the number of records we need to import, for the progress bar
count = int(cursor.fetchone()[0])
cursor.execute(u'SELECT COUNT(songid) FROM songs') cursor.execute(u'SELECT COUNT(songid) FROM songs')
count = int(cursor.fetchone()[0]) count = int(cursor.fetchone()[0])
success = True
self.import_wizard.importProgressBar.setMaximum(count) self.import_wizard.importProgressBar.setMaximum(count)
# Import the songs
old_cursor.execute(u'SELECT authorid AS id, authorname AS displayname FROM authors') cursor.execute(u'SELECT songid, songtitle, lyrics || \'\' AS lyrics, '
rows = old_cursor.fetchall() u'copyrightinfo FROM songs')
if not debug and verbose: songs = cursor.fetchall()
print 'done.' for song in songs:
author_map = {} self.set_defaults()
for row in rows: if self.stop_import_flag:
display_name = unicode(row[1], u'cp1252') success = False
names = display_name.split(u' ') break
first_name = names[0] song_id = song[0]
last_name = u' '.join(names[1:]) title = unicode(song[1], u'cp1252')
if last_name is None: lyrics = unicode(song[2], u'cp1252')
last_name = u'' copyright = unicode(song[3], u'cp1252')
sql_insert = u'INSERT INTO authors '\ self.import_wizard.incrementProgressBar(
'(id, first_name, last_name, display_name) '\ unicode(translate('SongsPlugin.ImportWizardForm',
'VALUES (NULL, ?, ?, ?)' 'Importing %s...')) % title)
sql_params = (first_name, last_name, display_name) self.title = title
if debug: self.process_song_text(lyrics)
print '...', display_sql(sql_insert, sql_params) self.add_copyright(copyright)
elif verbose: cursor.execute(u'SELECT displayname FROM authors a '
print '... importing "%s"' % display_name u'JOIN songauthors sa ON a.authorid = sa.authorid '
new_cursor.execute(sql_insert, sql_params) u'WHERE sa.songid = %s' % song_id)
author_map[row[0]] = new_cursor.lastrowid authors = cursor.fetchall()
if debug: for author in authors:
print ' >>> authors.authorid =', row[0], 'authors.id =', author_map[row[0]] if self.stop_import_flag:
success = False
break
cursor.execute(u'SELECT songid AS id, songtitle AS title, ' self.parse_author(unicode(author[0], u'cp1252'))
u'lyrics || \'\' AS lyrics, copyrightinfo AS copyright FROM songs') if self.stop_import_flag:
rows = cursor.fetchall() success = False
break
cursor.execute(u'SELECT fulltrackname FROM tracks t '
u'JOIN songtracks st ON t.trackid = st.trackid '
u'WHERE st.songid = %s ORDER BY st.listindex' % song_id)
tracks = cursor.fetchall()
for track in tracks:
if self.stop_import_flag:
success = False
break
self.add_media_file(unicode(track[0], u'cp1252'))
if self.stop_import_flag:
success = False
break
self.finish()
return success

View File

@ -116,10 +116,11 @@ class OpenSongImport(SongImport):
opensong files. If `self.commit` is set False, the import will not be opensong files. If `self.commit` is set False, the import will not be
committed to the database (useful for test scripts). committed to the database (useful for test scripts).
""" """
success = False success = True
self.import_wizard.importProgressBar.setMaximum(len(self.filenames)) self.import_wizard.importProgressBar.setMaximum(len(self.filenames))
for filename in self.filenames: for filename in self.filenames:
if self.stop_import_flag: if self.stop_import_flag:
success = False
break break
ext = os.path.splitext(filename)[1] ext = os.path.splitext(filename)[1]
if ext.lower() == u'.zip': if ext.lower() == u'.zip':
@ -130,24 +131,28 @@ class OpenSongImport(SongImport):
len(z.infolist())) len(z.infolist()))
for song in z.infolist(): for song in z.infolist():
if self.stop_import_flag: if self.stop_import_flag:
success = False
break break
parts = os.path.split(song.filename) parts = os.path.split(song.filename)
if parts[-1] == u'': if parts[-1] == u'':
#No final part => directory #No final part => directory
continue continue
self.import_wizard.incrementProgressBar(u'Importing %s...' \ self.import_wizard.incrementProgressBar(
% parts[-1]) unicode(translate('SongsPlugin.ImportWizardForm',
'Importing %s...')) % parts[-1])
songfile = z.open(song) songfile = z.open(song)
self.do_import_file(songfile) self.do_import_file(songfile)
if self.commit: if self.commit:
self.finish() self.finish()
self.set_defaults() self.set_defaults()
if self.stop_import_flag: if self.stop_import_flag:
success = False
break break
else: else:
log.info('Direct import %s', filename) log.info('Direct import %s', filename)
self.import_wizard.incrementProgressBar(u'Importing %s...' \ self.import_wizard.incrementProgressBar(
% os.path.split(filename)[-1]) unicode(translate('SongsPlugin.ImportWizardForm',
'Importing %s...')) % os.path.split(filename)[-1])
file = open(filename) file = open(filename)
self.do_import_file(file) self.do_import_file(file)
if self.commit: if self.commit:
@ -155,7 +160,7 @@ class OpenSongImport(SongImport):
self.set_defaults() self.set_defaults()
if not self.commit: if not self.commit:
self.finish() self.finish()
return success
def do_import_file(self, file): def do_import_file(self, file):
""" """

View File

@ -30,7 +30,7 @@ from PyQt4 import QtCore
from openlp.core.lib import Receiver, translate from openlp.core.lib import Receiver, translate
from openlp.plugins.songs.lib import VerseType from openlp.plugins.songs.lib import VerseType
from openlp.plugins.songs.lib.db import Song, Author, Topic, Book from openlp.plugins.songs.lib.db import Song, Author, Topic, Book, MediaFile
from openlp.plugins.songs.lib.xml import SongXMLBuilder from openlp.plugins.songs.lib.xml import SongXMLBuilder
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -66,6 +66,7 @@ class SongImport(QtCore.QObject):
self.ccli_number = u'' self.ccli_number = u''
self.authors = [] self.authors = []
self.topics = [] self.topics = []
self.media_files = []
self.song_book_name = u'' self.song_book_name = u''
self.song_book_pub = u'' self.song_book_pub = u''
self.verse_order_list = [] self.verse_order_list = []
@ -76,7 +77,7 @@ class SongImport(QtCore.QObject):
'SongsPlugin.SongImport', 'copyright')) 'SongsPlugin.SongImport', 'copyright'))
self.copyright_symbol = unicode(translate( self.copyright_symbol = unicode(translate(
'SongsPlugin.SongImport', '\xa9')) 'SongsPlugin.SongImport', '\xa9'))
def stop_import(self): def stop_import(self):
""" """
Sets the flag for importers to stop their import Sets the flag for importers to stop their import
@ -184,6 +185,14 @@ class SongImport(QtCore.QObject):
return return
self.authors.append(author) self.authors.append(author)
def add_media_file(self, filename):
"""
Add a media file to the list
"""
if filename in self.media_files:
return
self.media_files.append(filename)
def add_verse(self, verse, versetag=None): def add_verse(self, verse, versetag=None):
""" """
Add a verse. This is the whole verse, lines split by \n Add a verse. This is the whole verse, lines split by \n
@ -279,11 +288,16 @@ class SongImport(QtCore.QObject):
for authortext in self.authors: for authortext in self.authors:
author = self.manager.get_object_filtered(Author, author = self.manager.get_object_filtered(Author,
Author.display_name == authortext) Author.display_name == authortext)
if author is None: if not author:
author = Author.populate(display_name = authortext, author = Author.populate(display_name = authortext,
last_name=authortext.split(u' ')[-1], last_name=authortext.split(u' ')[-1],
first_name=u' '.join(authortext.split(u' ')[:-1])) first_name=u' '.join(authortext.split(u' ')[:-1]))
song.authors.append(author) song.authors.append(author)
for filename in self.media_files:
media_file = self.manager.get_object_filtered(MediaFile,
MediaFile.file_name == filename)
if not media_file:
song.media_files.append(MediaFile.populate(file_name=filename))
if self.song_book_name: if self.song_book_name:
song_book = self.manager.get_object_filtered(Book, song_book = self.manager.get_object_filtered(Book,
Book.name == self.song_book_name) Book.name == self.song_book_name)