Invalidate cached values on reentry, added logging
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
import subprocess
|
||||
import logging
|
||||
|
||||
from mimetype.api import get_mimetype
|
||||
from common.conf.settings import TEMPORARY_DIRECTORY
|
||||
@@ -27,10 +28,13 @@ CONVERTER_OFFICE_FILE_MIMETYPES = [
|
||||
'application/vnd.oasis.opendocument.graphics',
|
||||
]
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OfficeConverter(object):
|
||||
def __init__(self):
|
||||
self.backend_class = OfficeConverterBackendUnoconv
|
||||
self.backend = self.backend_class()
|
||||
self.exists = False
|
||||
self.mimetype = None
|
||||
self.encoding = None
|
||||
@@ -39,6 +43,10 @@ class OfficeConverter(object):
|
||||
return CONVERTER_OFFICE_FILE_MIMETYPES
|
||||
|
||||
def convert(self, input_filepath, mimetype=None):
|
||||
self.exists = False
|
||||
self.mimetype = None
|
||||
self.encoding = None
|
||||
|
||||
self.input_filepath = input_filepath
|
||||
|
||||
# Make sure file is of a known office format
|
||||
@@ -53,7 +61,6 @@ class OfficeConverter(object):
|
||||
self.exists = os.path.exists(self.output_filepath)
|
||||
if not self.exists:
|
||||
try:
|
||||
self.backend = self.backend_class()
|
||||
self.backend.convert(self.input_filepath, self.output_filepath)
|
||||
self.exists = True
|
||||
except OfficeBackendError, msg:
|
||||
@@ -65,11 +72,6 @@ class OfficeConverter(object):
|
||||
|
||||
def __str__(self):
|
||||
return str(self.__unicode__())
|
||||
|
||||
def __nonzero__(self):
|
||||
return self.exists
|
||||
|
||||
__bool__ = __nonzero__
|
||||
|
||||
|
||||
class OfficeConverterBackendUnoconv(object):
|
||||
@@ -79,9 +81,9 @@ class OfficeConverterBackendUnoconv(object):
|
||||
raise OfficeBackendError('cannot find unoconv executable')
|
||||
|
||||
def convert(self, input_filepath, output_filepath):
|
||||
"""
|
||||
'''
|
||||
Executes the program unoconv using subprocess's Popen
|
||||
"""
|
||||
'''
|
||||
self.input_filepath = input_filepath
|
||||
self.output_filepath = output_filepath
|
||||
|
||||
@@ -97,10 +99,15 @@ class OfficeConverterBackendUnoconv(object):
|
||||
command.append(self.input_filepath)
|
||||
|
||||
try:
|
||||
logger.debug('prev environment: %s' % os.environ)
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
logger.debug('post environment: %s' % os.environ)
|
||||
|
||||
readline = proc.stderr.readline()
|
||||
if return_code != 0:
|
||||
raise OfficeBackendError(proc.stderr.readline())
|
||||
except OSError, msg:
|
||||
raise OfficeBackendError(msg)
|
||||
except Exception, msg:
|
||||
logger.error('Unhandled exception: %s' % msg)
|
||||
|
||||
Reference in New Issue
Block a user