From d05c349f41319dd483394297e0f04563e05bd63a Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Tue, 24 May 2011 16:41:14 -0400 Subject: [PATCH 1/7] Catch common errors during import and report to user --- openlp/plugins/songs/lib/oooimport.py | 23 ++++++++++++++++++----- openlp/plugins/songs/lib/sofimport.py | 26 ++++++++++++++++---------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/openlp/plugins/songs/lib/oooimport.py b/openlp/plugins/songs/lib/oooimport.py index d43541bc7..1c5c7e1ef 100644 --- a/openlp/plugins/songs/lib/oooimport.py +++ b/openlp/plugins/songs/lib/oooimport.py @@ -40,6 +40,7 @@ if os.name == u'nt': PAGE_BOTH = 6 else: import uno + from com.sun.star.connection import NoConnectException from com.sun.star.style.BreakType import PAGE_BEFORE, PAGE_AFTER, PAGE_BOTH class OooImport(SongImport): @@ -56,7 +57,16 @@ class OooImport(SongImport): self.process_started = False def do_import(self): - self.start_ooo() + if not isinstance(self.import_source, list): + return + try: + self.start_ooo() + except NoConnectException as exc: + self.log_error( + self.import_source[0], + u'Unable to connect to OpenOffice.org or LibreOffice') + log.error(exc) + return self.import_wizard.progressBar.setMaximum(len(self.import_source)) for filename in self.import_source: if self.stop_import_flag: @@ -98,13 +108,16 @@ class OooImport(SongImport): while uno_instance is None and loop < 5: try: uno_instance = get_uno_instance(resolver) - except: + except NoConnectException: log.exception("Failed to resolve uno connection") self.start_ooo_process() loop += 1 - manager = uno_instance.ServiceManager - self.desktop = manager.createInstanceWithContext( - "com.sun.star.frame.Desktop", uno_instance) + else: + manager = uno_instance.ServiceManager + self.desktop = manager.createInstanceWithContext( + "com.sun.star.frame.Desktop", uno_instance) + return + raise def start_ooo_process(self): try: diff --git a/openlp/plugins/songs/lib/sofimport.py b/openlp/plugins/songs/lib/sofimport.py index 7f9fa16bc..f35094a96 100644 --- a/openlp/plugins/songs/lib/sofimport.py +++ b/openlp/plugins/songs/lib/sofimport.py @@ -30,10 +30,14 @@ # http://www.oooforum.org/forum/viewtopic.phtml?t=14409 # http://wiki.services.openoffice.org/wiki/Python +import logging import os import re from oooimport import OooImport +from com.sun.star.uno import RuntimeException + +log = logging.getLogger(__name__) if os.name == u'nt': BOLD = 150.0 @@ -85,16 +89,18 @@ class SofImport(OooImport): """ self.blanklines = 0 self.new_song() - paragraphs = self.document.getText().createEnumeration() - while paragraphs.hasMoreElements(): - if self.stop_import_flag: - return - paragraph = paragraphs.nextElement() - if paragraph.supportsService("com.sun.star.text.Paragraph"): - self.process_paragraph(paragraph) - if self.song: - self.finish() - self.song = False + try: + paragraphs = self.document.getText().createEnumeration() + while paragraphs.hasMoreElements(): + if self.stop_import_flag: + return + paragraph = paragraphs.nextElement() + if paragraph.supportsService("com.sun.star.text.Paragraph"): + self.process_paragraph(paragraph) + except RuntimeException as exc: + log.exception(u'Error processing file: %s', exc) + if not self.finish(): + self.log_error(self.filepath) def process_paragraph(self, paragraph): """ From 4399fc108f18000715ec6ee8caa7b50f601ad21f Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Wed, 25 May 2011 17:38:32 -0400 Subject: [PATCH 2/7] Fixed a silent error when OpenOffice fails to open a file.\ Also, added the file name to the exception log and made the exception explicit --- openlp/plugins/songs/lib/oooimport.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/openlp/plugins/songs/lib/oooimport.py b/openlp/plugins/songs/lib/oooimport.py index f570f33d3..a03a36121 100644 --- a/openlp/plugins/songs/lib/oooimport.py +++ b/openlp/plugins/songs/lib/oooimport.py @@ -80,6 +80,10 @@ class OooImport(SongImport): if self.document: self.process_ooo_document() self.close_ooo_file() + else: + self.log_error(self.filepath) + else: + self.log_error(self.filepath) self.close_ooo() def process_ooo_document(self): @@ -160,8 +164,8 @@ class OooImport(SongImport): else: self.import_wizard.incrementProgressBar( u'Processing file ' + filepath, 0) - except: - log.exception("open_ooo_file failed") + except AttributeError: + log.exception("open_ooo_file failed: %s", url) return def close_ooo_file(self): From e70ba41373abceb0a143e54e2135ae512cbbc52f Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Thu, 26 May 2011 09:35:01 -0400 Subject: [PATCH 3/7] Add specific error messages if OoO open fails. Use a lambda to avoid repetition --- openlp/plugins/songs/lib/oooimport.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/openlp/plugins/songs/lib/oooimport.py b/openlp/plugins/songs/lib/oooimport.py index a03a36121..5e4cd25d4 100644 --- a/openlp/plugins/songs/lib/oooimport.py +++ b/openlp/plugins/songs/lib/oooimport.py @@ -71,6 +71,10 @@ class OooImport(SongImport): log.error(exc) return self.import_wizard.progressBar.setMaximum(len(self.import_source)) + error_msg = lambda: self.log_error( + self.filepath, + translate('SongsPlugin.SongImport', + u'Unable to open file')) for filename in self.import_source: if self.stop_import_flag: break @@ -81,9 +85,9 @@ class OooImport(SongImport): self.process_ooo_document() self.close_ooo_file() else: - self.log_error(self.filepath) + error_msg() else: - self.log_error(self.filepath) + error_msg() self.close_ooo() def process_ooo_document(self): From 74e47dcbc7fb8c948a6447915551e3984af36ea8 Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Thu, 26 May 2011 14:20:14 -0400 Subject: [PATCH 4/7] Add specific error messages for file problems. Remove unicode strings in calls to translate() --- openlp/plugins/songs/lib/oooimport.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/openlp/plugins/songs/lib/oooimport.py b/openlp/plugins/songs/lib/oooimport.py index 5e4cd25d4..3cc61f4e3 100644 --- a/openlp/plugins/songs/lib/oooimport.py +++ b/openlp/plugins/songs/lib/oooimport.py @@ -67,14 +67,10 @@ class OooImport(SongImport): self.log_error( self.import_source[0], translate('SongsPlugin.SongImport', - u'Unable to open OpenOffice.org or LibreOffice')) + 'Unable to open OpenOffice.org or LibreOffice')) log.error(exc) return self.import_wizard.progressBar.setMaximum(len(self.import_source)) - error_msg = lambda: self.log_error( - self.filepath, - translate('SongsPlugin.SongImport', - u'Unable to open file')) for filename in self.import_source: if self.stop_import_flag: break @@ -85,9 +81,13 @@ class OooImport(SongImport): self.process_ooo_document() self.close_ooo_file() else: - error_msg() + self.log_error(self.filepath, + translate('SongsPlugin.SongImport', + 'Unable to open file')) else: - error_msg() + self.log_error(self.filepath, + translate('SongsPlugin.SongImport', + 'File not found')) self.close_ooo() def process_ooo_document(self): From caa6639c7b184d348c25099479a3cd66895550e5 Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Sat, 28 May 2011 16:43:33 -0400 Subject: [PATCH 5/7] Uglification to conform to weird standards :) --- openlp/plugins/songs/lib/oooimport.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/openlp/plugins/songs/lib/oooimport.py b/openlp/plugins/songs/lib/oooimport.py index 45e85f64c..ddbab8b31 100644 --- a/openlp/plugins/songs/lib/oooimport.py +++ b/openlp/plugins/songs/lib/oooimport.py @@ -82,12 +82,11 @@ class OooImport(SongImport): self.close_ooo_file() else: self.log_error(self.filepath, - translate('SongsPlugin.SongImport', - 'Unable to open file')) + translate('SongsPlugin.SongImport', + 'Unable to open file')) else: self.log_error(self.filepath, - translate('SongsPlugin.SongImport', - 'File not found')) + translate('SongsPlugin.SongImport', 'File not found')) self.close_ooo() def process_ooo_document(self): From cde0eb5ab571462751e6291317597aed4a86f331 Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Mon, 30 May 2011 17:15:34 -0400 Subject: [PATCH 6/7] Only import uno exceptions if not running Windoes --- openlp/plugins/songs/lib/oooimport.py | 1 + openlp/plugins/songs/lib/sofimport.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/openlp/plugins/songs/lib/oooimport.py b/openlp/plugins/songs/lib/oooimport.py index ddbab8b31..fb6ee43a6 100644 --- a/openlp/plugins/songs/lib/oooimport.py +++ b/openlp/plugins/songs/lib/oooimport.py @@ -40,6 +40,7 @@ if os.name == u'nt': PAGE_BEFORE = 4 PAGE_AFTER = 5 PAGE_BOTH = 6 + NoConnectException = Exception else: import uno from com.sun.star.connection import NoConnectException diff --git a/openlp/plugins/songs/lib/sofimport.py b/openlp/plugins/songs/lib/sofimport.py index 5cc0482f2..7f0bed72b 100644 --- a/openlp/plugins/songs/lib/sofimport.py +++ b/openlp/plugins/songs/lib/sofimport.py @@ -36,7 +36,7 @@ import os import re from oooimport import OooImport -from com.sun.star.uno import RuntimeException + log = logging.getLogger(__name__) @@ -44,12 +44,14 @@ if os.name == u'nt': BOLD = 150.0 ITALIC = 2 from oooimport import PAGE_BEFORE, PAGE_AFTER, PAGE_BOTH + RuntimeException = Exception else: try: from com.sun.star.awt.FontWeight import BOLD from com.sun.star.awt.FontSlant import ITALIC from com.sun.star.style.BreakType import PAGE_BEFORE, PAGE_AFTER, \ PAGE_BOTH + from com.sun.star.uno import RuntimeException except ImportError: pass From 6856e3ef44bf2e6df3740176153453099954666b Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Tue, 31 May 2011 16:05:47 -0400 Subject: [PATCH 7/7] Ensure exception is raised if all attempts to connect with uno fail --- openlp/plugins/songs/lib/oooimport.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openlp/plugins/songs/lib/oooimport.py b/openlp/plugins/songs/lib/oooimport.py index fb6ee43a6..85e0b67bc 100644 --- a/openlp/plugins/songs/lib/oooimport.py +++ b/openlp/plugins/songs/lib/oooimport.py @@ -128,7 +128,7 @@ class OooImport(SongImport): self.desktop = manager.createInstanceWithContext( "com.sun.star.frame.Desktop", uno_instance) return - raise + raise def start_ooo_process(self): try: