forked from openlp/openlp
Add robustness to Generic document and Songs of Fellowship importers.
bzr-revno: 1601
This commit is contained in:
commit
7459255b65
@ -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):
|
||||
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
|
||||
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):
|
||||
|
@ -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,6 +92,7 @@ class SofImport(OooImport):
|
||||
"""
|
||||
self.blanklines = 0
|
||||
self.new_song()
|
||||
try:
|
||||
paragraphs = self.document.getText().createEnumeration()
|
||||
while paragraphs.hasMoreElements():
|
||||
if self.stop_import_flag:
|
||||
@ -93,9 +100,10 @@ class SofImport(OooImport):
|
||||
paragraph = paragraphs.nextElement()
|
||||
if paragraph.supportsService("com.sun.star.text.Paragraph"):
|
||||
self.process_paragraph(paragraph)
|
||||
if self.song:
|
||||
self.finish()
|
||||
self.song = False
|
||||
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):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user