Add robustness to Generic document and Songs of Fellowship importers.

bzr-revno: 1601
This commit is contained in:
Gerald Britton 2011-06-01 17:43:29 +02:00 committed by Raoul Snyman
commit 7459255b65
2 changed files with 48 additions and 17 deletions

View File

@ -30,6 +30,7 @@ import os
from PyQt4 import QtCore
from openlp.core.utils import get_uno_command, get_uno_instance
from openlp.core.lib import translate
from songimport import SongImport
log = logging.getLogger(__name__)
@ -39,8 +40,10 @@ 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
from com.sun.star.style.BreakType import PAGE_BEFORE, PAGE_AFTER, PAGE_BOTH
class OooImport(SongImport):
@ -57,7 +60,17 @@ 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],
translate('SongsPlugin.SongImport',
'Unable to open 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:
@ -68,6 +81,13 @@ class OooImport(SongImport):
if self.document:
self.process_ooo_document()
self.close_ooo_file()
else:
self.log_error(self.filepath,
translate('SongsPlugin.SongImport',
'Unable to open file'))
else:
self.log_error(self.filepath,
translate('SongsPlugin.SongImport', 'File not found'))
self.close_ooo()
def process_ooo_document(self):
@ -99,13 +119,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:
@ -145,8 +168,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):

View File

@ -31,21 +31,27 @@
# 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
log = logging.getLogger(__name__)
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
@ -86,16 +92,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):
"""