forked from openlp/openlp
using PySQLite type conversion
This commit is contained in:
parent
5e9ff18927
commit
ef91b24d8a
@ -59,65 +59,50 @@ class OpenLP1SongImport(SongImport):
|
|||||||
SongImport.__init__(self, manager)
|
SongImport.__init__(self, manager)
|
||||||
self.import_source = kwargs[u'filename']
|
self.import_source = kwargs[u'filename']
|
||||||
|
|
||||||
def decode_string(self, raw, encoding):
|
|
||||||
"""
|
|
||||||
Use chardet to detect the encoding of the raw string, and convert it
|
|
||||||
to unicode.
|
|
||||||
|
|
||||||
``raw``
|
|
||||||
The raw bytestring to decode.
|
|
||||||
``encoding``
|
|
||||||
The bytestring character encoding.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
decoded = unicode(raw, encoding)
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
log.exception(u'The openlp.org 1.x database is not %s encoded.' % \
|
|
||||||
encoding)
|
|
||||||
decoded = raw
|
|
||||||
return decoded
|
|
||||||
|
|
||||||
def do_import(self):
|
def do_import(self):
|
||||||
"""
|
"""
|
||||||
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
|
# Connect to the database
|
||||||
connection = sqlite.connect(self.import_source)
|
encoding = self.get_encoding()
|
||||||
|
if not encoding:
|
||||||
|
return False
|
||||||
|
connection = sqlite.connect(self.import_source, mode=0555,
|
||||||
|
encoding=(encoding, 'replace'))
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
# Determine if we're using a new or an old DB
|
# Determine if we're using a new or an old DB
|
||||||
cursor.execute(u'SELECT name FROM sqlite_master '
|
cursor.execute(u'SELECT name FROM sqlite_master '
|
||||||
u'WHERE type = \'table\' AND name = \'tracks\'')
|
u'WHERE type = \'table\' AND name = \'tracks\'')
|
||||||
table_list = cursor.fetchall()
|
new_db = len(cursor.fetchall()) > 0
|
||||||
new_db = len(table_list) > 0
|
|
||||||
# Count the number of records we need to import, for the progress bar
|
# Count the number of records we need to import, for the progress bar
|
||||||
|
cursor.execute(u'-- types int')
|
||||||
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
|
success = True
|
||||||
self.import_wizard.importProgressBar.setMaximum(count)
|
self.import_wizard.importProgressBar.setMaximum(count)
|
||||||
# "cache" our list of authors
|
# "cache" our list of authors
|
||||||
|
cursor.execute(u'-- types int, unicode')
|
||||||
cursor.execute(u'SELECT authorid, authorname FROM authors')
|
cursor.execute(u'SELECT authorid, authorname FROM authors')
|
||||||
authors = cursor.fetchall()
|
authors = cursor.fetchall()
|
||||||
if new_db:
|
if new_db:
|
||||||
# "cache" our list of tracks
|
# "cache" our list of tracks
|
||||||
|
cursor.execute(u'-- types int, unicode')
|
||||||
cursor.execute(u'SELECT trackid, fulltrackname FROM tracks')
|
cursor.execute(u'SELECT trackid, fulltrackname FROM tracks')
|
||||||
tracks = cursor.fetchall()
|
tracks = cursor.fetchall()
|
||||||
# Import the songs
|
# Import the songs
|
||||||
|
cursor.execute(u'-- types int, unicode, unicode, unicode')
|
||||||
cursor.execute(u'SELECT songid, songtitle, lyrics || \'\' AS lyrics, '
|
cursor.execute(u'SELECT songid, songtitle, lyrics || \'\' AS lyrics, '
|
||||||
u'copyrightinfo FROM songs')
|
u'copyrightinfo FROM songs')
|
||||||
songs = cursor.fetchall()
|
songs = cursor.fetchall()
|
||||||
encoding = self.get_encoding()
|
|
||||||
if not encoding:
|
|
||||||
self.stop_import_flag = True
|
|
||||||
return False
|
|
||||||
for song in songs:
|
for song in songs:
|
||||||
self.set_defaults()
|
self.set_defaults()
|
||||||
if self.stop_import_flag:
|
if self.stop_import_flag:
|
||||||
success = False
|
success = False
|
||||||
break
|
break
|
||||||
song_id = song[0]
|
song_id = song[0]
|
||||||
title = self.decode_string(song[1], encoding)
|
title = song[1]
|
||||||
lyrics = self.decode_string(song[2], encoding).replace(u'\r', u'')
|
lyrics = song[2].replace(u'\r\n', u'\n')
|
||||||
copyright = self.decode_string(song[3], encoding)
|
copyright = song[3]
|
||||||
self.import_wizard.incrementProgressBar(
|
self.import_wizard.incrementProgressBar(
|
||||||
unicode(translate('SongsPlugin.ImportWizardForm',
|
unicode(translate('SongsPlugin.ImportWizardForm',
|
||||||
'Importing "%s"...')) % title)
|
'Importing "%s"...')) % title)
|
||||||
@ -127,6 +112,7 @@ class OpenLP1SongImport(SongImport):
|
|||||||
if verse.strip() != u'':
|
if verse.strip() != u'':
|
||||||
self.add_verse(verse.strip())
|
self.add_verse(verse.strip())
|
||||||
self.add_copyright(copyright)
|
self.add_copyright(copyright)
|
||||||
|
cursor.execute(u'-- types int')
|
||||||
cursor.execute(u'SELECT authorid FROM songauthors '
|
cursor.execute(u'SELECT authorid FROM songauthors '
|
||||||
u'WHERE songid = %s' % song_id)
|
u'WHERE songid = %s' % song_id)
|
||||||
author_ids = cursor.fetchall()
|
author_ids = cursor.fetchall()
|
||||||
@ -136,12 +122,13 @@ class OpenLP1SongImport(SongImport):
|
|||||||
break
|
break
|
||||||
for author in authors:
|
for author in authors:
|
||||||
if author[0] == author_id[0]:
|
if author[0] == author_id[0]:
|
||||||
self.parse_author(self.decode_string(author[1], encoding))
|
self.parse_author(author[1])
|
||||||
break
|
break
|
||||||
if self.stop_import_flag:
|
if self.stop_import_flag:
|
||||||
success = False
|
success = False
|
||||||
break
|
break
|
||||||
if new_db:
|
if new_db:
|
||||||
|
cursor.execute(u'-- types int')
|
||||||
cursor.execute(u'SELECT trackid FROM songtracks '
|
cursor.execute(u'SELECT trackid FROM songtracks '
|
||||||
u'WHERE songid = %s ORDER BY listindex' % song_id)
|
u'WHERE songid = %s ORDER BY listindex' % song_id)
|
||||||
track_ids = cursor.fetchall()
|
track_ids = cursor.fetchall()
|
||||||
@ -151,7 +138,7 @@ class OpenLP1SongImport(SongImport):
|
|||||||
break
|
break
|
||||||
for track in tracks:
|
for track in tracks:
|
||||||
if track[0] == track_id[0]:
|
if track[0] == track_id[0]:
|
||||||
self.add_media_file(self.decode_string(track[1], encoding))
|
self.add_media_file(track[1])
|
||||||
break
|
break
|
||||||
if self.stop_import_flag:
|
if self.stop_import_flag:
|
||||||
success = False
|
success = False
|
||||||
|
Loading…
Reference in New Issue
Block a user