From 35665ff336433cfc6c5f54b22c7c5dd4b4cd6337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20P=C3=B5ldaru?= Date: Wed, 21 Dec 2011 00:44:35 +0200 Subject: [PATCH 1/2] Fixes error when trying to import openlp.org v1 bible or songs db from a file with non-ascii characters. Also some notes about error handling. --- openlp/plugins/bibles/lib/openlp1.py | 22 +++++++++++++++++++--- openlp/plugins/songs/lib/olp1import.py | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/openlp/plugins/bibles/lib/openlp1.py b/openlp/plugins/bibles/lib/openlp1.py index b822b9b9d..741dfcc7a 100644 --- a/openlp/plugins/bibles/lib/openlp1.py +++ b/openlp/plugins/bibles/lib/openlp1.py @@ -27,6 +27,7 @@ import logging import sqlite +import sys from openlp.core.lib import Receiver from openlp.core.ui.wizard import WizardStrings @@ -53,9 +54,14 @@ class OpenLP1Bible(BibleDB): connection = None cursor = None try: - connection = sqlite.connect(self.filename) + connection = sqlite.connect( + self.filename.encode(sys.getfilesystemencoding())) cursor = connection.cursor() - except: + except sqlite.DatabaseError: + log.exception(u'File "%s" is encrypted or not a sqlite database, ' + 'therefore not an openlp.org 1.x database either' % self.filename) + # Please add an user error here! + # This file is not an openlp.org 1.x bible database. return False #Create the bible language language_id = self.get_language(bible_name) @@ -63,7 +69,17 @@ class OpenLP1Bible(BibleDB): log.exception(u'Importing books from "%s" failed' % self.filename) return False # Create all books. - cursor.execute(u'SELECT id, testament_id, name, abbreviation FROM book') + try: + cursor.execute( + u'SELECT id, testament_id, name, abbreviation FROM book') + except sqlite.DatabaseError as error: + log.exception(u'DatabaseError: %s' % error) + if error == 'no such table: book': + # Please add an user error here! + # This file is not an openlp.org 1.x bible database. + return False + else: + raise sqlite.DatabaseError(error) books = cursor.fetchall() self.wizard.progressBar.setMaximum(len(books) + 1) for book in books: diff --git a/openlp/plugins/songs/lib/olp1import.py b/openlp/plugins/songs/lib/olp1import.py index fa43d94cf..f564eb774 100644 --- a/openlp/plugins/songs/lib/olp1import.py +++ b/openlp/plugins/songs/lib/olp1import.py @@ -165,7 +165,8 @@ class OpenLP1SongImport(SongImport): Detect character encoding of an openlp.org 1.x song database. """ # Connect to the database. - connection = sqlite.connect(self.importSource, mode=0444) + connection = sqlite.connect(self.importSource.encode( + sys.getfilesystemencoding()), mode=0444) cursor = connection.cursor() detector = UniversalDetector() From c1d1d38eba8561792786d71ac78a694cd845efd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20P=C3=B5ldaru?= Date: Wed, 21 Dec 2011 10:21:37 +0200 Subject: [PATCH 2/2] Such precision would not be needed in user errors, I suppose. The ones who would benefit from this can read log anyway. --- openlp/plugins/bibles/lib/openlp1.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/openlp/plugins/bibles/lib/openlp1.py b/openlp/plugins/bibles/lib/openlp1.py index 741dfcc7a..7701f1828 100644 --- a/openlp/plugins/bibles/lib/openlp1.py +++ b/openlp/plugins/bibles/lib/openlp1.py @@ -74,12 +74,9 @@ class OpenLP1Bible(BibleDB): u'SELECT id, testament_id, name, abbreviation FROM book') except sqlite.DatabaseError as error: log.exception(u'DatabaseError: %s' % error) - if error == 'no such table: book': - # Please add an user error here! - # This file is not an openlp.org 1.x bible database. - return False - else: - raise sqlite.DatabaseError(error) + # Please add an user error here! + # This file is not an openlp.org 1.x bible database. + return False books = cursor.fetchall() self.wizard.progressBar.setMaximum(len(books) + 1) for book in books: