fixed bug #816186; fixed comments; fixed list comprehension

Fixes: https://launchpad.net/bugs/816186
This commit is contained in:
Andreas Preikschat 2011-08-05 13:21:39 +02:00
parent 4e043e1dcf
commit 6c7c27a7c9
2 changed files with 27 additions and 13 deletions

View File

@ -699,7 +699,8 @@ class SongImportForm(OpenLPWizard):
elif source_format == SongFormat.OpenLP1: elif source_format == SongFormat.OpenLP1:
# Import an openlp.org database # Import an openlp.org database
importer = self.plugin.importSongs(SongFormat.OpenLP1, importer = self.plugin.importSongs(SongFormat.OpenLP1,
filename=unicode(self.openLP1FilenameEdit.text()) filename=unicode(self.openLP1FilenameEdit.text()),
plugin=self.plugin
) )
elif source_format == SongFormat.OpenLyrics: elif source_format == SongFormat.OpenLyrics:
# Import OpenLyrics songs # Import OpenLyrics songs

View File

@ -57,6 +57,8 @@ class OpenLP1SongImport(SongImport):
The database providing the data to import. The database providing the data to import.
""" """
SongImport.__init__(self, manager, **kwargs) SongImport.__init__(self, manager, **kwargs)
self.available_themes = \
kwargs[u'plugin'].formparent.themeManagerContents.getThemes()
def do_import(self): def do_import(self):
""" """
@ -70,27 +72,34 @@ class OpenLP1SongImport(SongImport):
encoding = self.get_encoding() encoding = self.get_encoding()
if not encoding: if not encoding:
return return
# Connect to the database # Connect to the database.
connection = sqlite.connect(self.import_source, mode=0444, connection = sqlite.connect(self.import_source, mode=0444,
encoding=(encoding, 'replace')) 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\'')
new_db = len(cursor.fetchall()) > 0 new_db = len(cursor.fetchall()) > 0
# "cache" our list of authors # "cache" our list of authors.
cursor.execute(u'-- types int, unicode') 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'-- 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 # "cache" our list of themes.
cursor.execute(u'-- types int, unicode, unicode, unicode') cursor.execute(u'-- types int, unicode')
cursor.execute(u'SELECT settingsid, settingsname FROM settings')
themes = {}
for theme_id, theme_name in cursor.fetchall():
if theme_name in self.available_themes:
themes[theme_id] = theme_name
# Import the songs.
cursor.execute(u'-- types int, unicode, unicode, unicode, int')
cursor.execute(u'SELECT songid, songtitle, lyrics || \'\' AS lyrics, ' cursor.execute(u'SELECT songid, songtitle, lyrics || \'\' AS lyrics, '
u'copyrightinfo FROM songs') u'copyrightinfo, settingsid FROM songs')
songs = cursor.fetchall() songs = cursor.fetchall()
self.import_wizard.progressBar.setMaximum(len(songs)) self.import_wizard.progressBar.setMaximum(len(songs))
for song in songs: for song in songs:
@ -101,8 +110,12 @@ class OpenLP1SongImport(SongImport):
self.title = song[1] self.title = song[1]
lyrics = song[2].replace(u'\r\n', u'\n') lyrics = song[2].replace(u'\r\n', u'\n')
self.add_copyright(song[3]) self.add_copyright(song[3])
if themes.has_key(song[4]):
self.theme_name = themes[song[4]]
verses = lyrics.split(u'\n\n') verses = lyrics.split(u'\n\n')
[self.add_verse(verse.strip()) for verse in verses if verse.strip()] for verse in verses:
if verse.strip():
self.add_verse(verse.strip())
cursor.execute(u'-- types int') 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)
@ -137,12 +150,12 @@ class OpenLP1SongImport(SongImport):
""" """
Detect character encoding of an openlp.org 1.x song database. Detect character encoding of an openlp.org 1.x song database.
""" """
# Connect to the database # Connect to the database.
connection = sqlite.connect(self.import_source, mode=0444) connection = sqlite.connect(self.import_source, mode=0444)
cursor = connection.cursor() cursor = connection.cursor()
detector = UniversalDetector() detector = UniversalDetector()
# detect charset by authors # Detect charset by authors.
cursor.execute(u'SELECT authorname FROM authors') cursor.execute(u'SELECT authorname FROM authors')
authors = cursor.fetchall() authors = cursor.fetchall()
for author in authors: for author in authors:
@ -150,7 +163,7 @@ class OpenLP1SongImport(SongImport):
if detector.done: if detector.done:
detector.close() detector.close()
return detector.result[u'encoding'] return detector.result[u'encoding']
# detect charset by songs # Detect charset by songs.
cursor.execute(u'SELECT songtitle, copyrightinfo, ' cursor.execute(u'SELECT songtitle, copyrightinfo, '
u'lyrics || \'\' AS lyrics FROM songs') u'lyrics || \'\' AS lyrics FROM songs')
songs = cursor.fetchall() songs = cursor.fetchall()
@ -160,7 +173,7 @@ class OpenLP1SongImport(SongImport):
if detector.done: if detector.done:
detector.close() detector.close()
return detector.result[u'encoding'] return detector.result[u'encoding']
# detect charset by songs # Detect charset by songs.
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\'')
if len(cursor.fetchall()) > 0: if len(cursor.fetchall()) > 0: