[pod] converter.py now works from Python2.4 to Python3. Thanks to Lennart Regebro who wrote the 'e=sys.exc_info()[1]' trick for catching exceptions in both Python2.4 and Python3 and Luc Saffre for discovering it.

This commit is contained in:
Gaetan Delannay 2014-06-18 11:46:43 +02:00
parent e11e754305
commit 114223a114

View file

@ -96,7 +96,7 @@ class Converter:
def getResultFilter(self): def getResultFilter(self):
'''Based on the result type, identifies which OO filter to use for the '''Based on the result type, identifies which OO filter to use for the
document conversion.''' document conversion.'''
if FILE_TYPES.has_key(self.resultType): if self.resultType in FILE_TYPES:
res = FILE_TYPES[self.resultType] res = FILE_TYPES[self.resultType]
if isinstance(res, dict): if isinstance(res, dict):
res = res[self.inputType] res = res[self.inputType]
@ -128,8 +128,9 @@ class Converter:
f.close() f.close()
os.remove(res) os.remove(res)
return unohelper.systemPathToFileUrl(res) return unohelper.systemPathToFileUrl(res)
except (OSError, IOError), ioe: except (OSError, IOError):
raise ConverterError(CANNOT_WRITE_RESULT % (res, ioe)) e = sys.exc_info()[1]
raise ConverterError(CANNOT_WRITE_RESULT % (res, e))
def connect(self): def connect(self):
'''Connects to LibreOffice''' '''Connects to LibreOffice'''
@ -155,8 +156,9 @@ class Converter:
# Get the central desktop object # Get the central desktop object
self.oo = smgr.createInstanceWithContext( self.oo = smgr.createInstanceWithContext(
'com.sun.star.frame.Desktop', self.loContext) 'com.sun.star.frame.Desktop', self.loContext)
except NoConnectException, nce: except NoConnectException:
raise ConverterError(CONNECT_ERROR % (self.port, nce)) e = sys.exc_info()[1]
raise ConverterError(CONNECT_ERROR % (self.port, e))
def updateOdtDocument(self): def updateOdtDocument(self):
'''If the input file is an ODT document, we will perform 2 tasks: '''If the input file is an ODT document, we will perform 2 tasks:
@ -222,8 +224,9 @@ class Converter:
self.doc.refresh() self.doc.refresh()
except AttributeError: except AttributeError:
pass pass
except IllegalArgumentException, iae: except IllegalArgumentException:
raise ConverterError(URL_NOT_FOUND % (self.docPath, iae)) e = sys.exc_info()[1]
raise ConverterError(URL_NOT_FOUND % (self.docPath, e))
def convertDocument(self): def convertDocument(self):
'''Calls LO to perform a document conversion. Note that the conversion '''Calls LO to perform a document conversion. Note that the conversion
@ -280,8 +283,9 @@ class ConverterScript:
converter = Converter(args[0], args[1], options.port) converter = Converter(args[0], args[1], options.port)
try: try:
converter.run() converter.run()
except ConverterError, ce: except ConverterError:
sys.stderr.write(str(ce)) e = sys.exc_info()[1]
sys.stderr.write(str(e))
sys.stderr.write('\n') sys.stderr.write('\n')
optParser.print_help() optParser.print_help()
sys.exit(ERROR_CODE) sys.exit(ERROR_CODE)