Renamed the exception UnknownFormat to UnkownFileFormat and updated all
backends get_page_count method to raise it, having the api's get_page_count function decide how to handle the exception
This commit is contained in:
@@ -5,7 +5,7 @@ import hashlib
|
||||
from common.conf.settings import TEMPORARY_DIRECTORY
|
||||
|
||||
from converter.conf.settings import UNOCONV_PATH
|
||||
from converter.exceptions import OfficeConversionError
|
||||
from converter.exceptions import OfficeConversionError, UnknownFileFormat
|
||||
from converter.literals import DEFAULT_PAGE_NUMBER, \
|
||||
DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, DEFAULT_FILE_FORMAT
|
||||
|
||||
@@ -119,7 +119,12 @@ def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, **
|
||||
|
||||
|
||||
def get_page_count(input_filepath):
|
||||
return backend.get_page_count(input_filepath)
|
||||
try:
|
||||
return backend.get_page_count(input_filepath)
|
||||
except UnknownFileFormat:
|
||||
# If converter backend doesn't understand the format return
|
||||
# 1 as the total page count
|
||||
return 1
|
||||
|
||||
|
||||
def get_document_dimensions(document, *args, **kwargs):
|
||||
|
||||
@@ -3,7 +3,7 @@ import re
|
||||
|
||||
from converter.conf.settings import GM_PATH
|
||||
from converter.conf.settings import GM_SETTINGS
|
||||
from converter.exceptions import ConvertError, UnknownFormat, \
|
||||
from converter.exceptions import ConvertError, UnknownFileFormat, \
|
||||
IdentifyError
|
||||
from converter.backends import ConverterBase
|
||||
from converter.literals import TRANSFORMATION_RESIZE, \
|
||||
@@ -79,7 +79,7 @@ class ConverterClass(ConverterBase):
|
||||
error_line = proc.stderr.readline()
|
||||
if (CONVERTER_ERROR_STRING_NO_DECODER in error_line) or (CONVERTER_ERROR_STARTS_WITH in error_line):
|
||||
#Try to determine from error message which class of error is it
|
||||
raise UnknownFormat
|
||||
raise UnknownFileFormat
|
||||
else:
|
||||
raise ConvertError(error_line)
|
||||
|
||||
@@ -116,6 +116,5 @@ class ConverterClass(ConverterBase):
|
||||
def get_page_count(self, input_filepath):
|
||||
try:
|
||||
return len(self.identify_file(unicode(input_filepath)).splitlines())
|
||||
except:
|
||||
#TODO: send to other page number identifying program
|
||||
return 1
|
||||
except IdentifyError:
|
||||
raise UnknownFileFormat
|
||||
|
||||
@@ -3,7 +3,7 @@ import re
|
||||
|
||||
from converter.conf.settings import IM_IDENTIFY_PATH
|
||||
from converter.conf.settings import IM_CONVERT_PATH
|
||||
from converter.exceptions import ConvertError, UnknownFormat, \
|
||||
from converter.exceptions import ConvertError, UnknownFileFormat, \
|
||||
IdentifyError
|
||||
from converter.backends import ConverterBase
|
||||
from converter.literals import TRANSFORMATION_RESIZE, \
|
||||
@@ -75,7 +75,7 @@ class ConverterClass(ConverterBase):
|
||||
error_line = proc.stderr.readline()
|
||||
if CONVERTER_ERROR_STRING_NO_DECODER in error_line:
|
||||
#Try to determine from error message which class of error is it
|
||||
raise UnknownFormat
|
||||
raise UnknownFileFormat
|
||||
else:
|
||||
raise ConvertError(error_line)
|
||||
|
||||
@@ -114,6 +114,5 @@ class ConverterClass(ConverterBase):
|
||||
def get_page_count(self, input_filepath):
|
||||
try:
|
||||
return len(self.identify_file(unicode(input_filepath)).splitlines())
|
||||
except:
|
||||
#TODO: send to other page number identifying program
|
||||
return 1
|
||||
except IdentifyError:
|
||||
raise UnknownFileFormat
|
||||
|
||||
@@ -14,7 +14,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from mimetype.api import get_mimetype
|
||||
|
||||
from converter.exceptions import ConvertError, UnknownFormat, IdentifyError
|
||||
from converter.exceptions import ConvertError, UnknownFileFormat, IdentifyError
|
||||
from converter.backends import ConverterBase
|
||||
from converter.literals import TRANSFORMATION_RESIZE, \
|
||||
TRANSFORMATION_ROTATE, TRANSFORMATION_ZOOM
|
||||
@@ -38,9 +38,7 @@ class ConverterClass(ConverterBase):
|
||||
try:
|
||||
im = Image.open(input_filepath)
|
||||
except IOError: #cannot identify image file
|
||||
# Return a page count of 1, to atleast allow the document
|
||||
# to be created
|
||||
return 1
|
||||
return UnknownFileFormat
|
||||
|
||||
try:
|
||||
while 1:
|
||||
@@ -87,7 +85,7 @@ class ConverterClass(ConverterBase):
|
||||
try:
|
||||
im = Image.open(input_filepath)
|
||||
except Exception: # Python Imaging Library doesn't recognize it as an image
|
||||
raise UnknownFormat
|
||||
raise UnknownFileFormat
|
||||
finally:
|
||||
if tmpfile:
|
||||
cleanup(tmpfile)
|
||||
|
||||
@@ -5,17 +5,16 @@ class ConvertError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UnknownFormat(ConvertError):
|
||||
class UnknownFileFormat(ConvertError):
|
||||
"""
|
||||
Raised when the converter backend can't understand or there
|
||||
isn't an appropiate driver available
|
||||
Raised when the converter backend can't understand a file
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class IdentifyError(ConvertError):
|
||||
"""
|
||||
Raised by identify
|
||||
Raised by the graphcismagick and imagemagics identify program
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ from dynamic_search.api import register
|
||||
from converter.api import get_page_count
|
||||
from converter.api import get_available_transformations_choices
|
||||
from converter.api import convert
|
||||
from converter.exceptions import UnknownFormat, UnkownConvertError
|
||||
from converter.exceptions import UnknownFileFormat, UnkownConvertError
|
||||
from mimetype.api import get_document_mimetype, get_icon_file_path, \
|
||||
get_error_icon_file_path
|
||||
|
||||
@@ -245,7 +245,7 @@ class Document(models.Model):
|
||||
try:
|
||||
image_cache_name = self.get_image_cache_name(page=page)
|
||||
return convert(image_cache_name, cleanup_files=False, size=size, zoom=zoom, rotation=rotation)
|
||||
except UnknownFormat:
|
||||
except UnknownFileFormat:
|
||||
return get_icon_file_path(self.file_mimetype)
|
||||
except UnkownConvertError:
|
||||
return get_error_icon_file_path()
|
||||
|
||||
@@ -10,7 +10,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from mimetype.api import get_icon_file_path, get_error_icon_file_path, \
|
||||
get_mimetype
|
||||
from converter.api import convert, cache_cleanup
|
||||
from converter.exceptions import UnknownFormat, UnkownConvertError
|
||||
from converter.exceptions import UnknownFileFormat, UnkownConvertError
|
||||
|
||||
|
||||
DEFAULT_STAGING_DIRECTORY = u'/tmp'
|
||||
@@ -138,7 +138,7 @@ class StagingFile(object):
|
||||
def get_image(self, size, transformations):
|
||||
try:
|
||||
return convert(self.filepath, size=size, cleanup_files=False, transformations=transformations)
|
||||
except UnknownFormat:
|
||||
except UnknownFileFormat:
|
||||
return get_icon_file_path(get_mimetype(self.filepath))
|
||||
except UnkownConvertError:
|
||||
return get_error_icon_file_path()
|
||||
|
||||
Reference in New Issue
Block a user