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:
Roberto Rosario
2011-08-05 03:24:01 -04:00
parent eaea853391
commit cd0b1577a7
7 changed files with 25 additions and 25 deletions
+7 -2
View File
@@ -5,7 +5,7 @@ import hashlib
from common.conf.settings import TEMPORARY_DIRECTORY from common.conf.settings import TEMPORARY_DIRECTORY
from converter.conf.settings import UNOCONV_PATH 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, \ from converter.literals import DEFAULT_PAGE_NUMBER, \
DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, DEFAULT_FILE_FORMAT 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): 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): 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_PATH
from converter.conf.settings import GM_SETTINGS from converter.conf.settings import GM_SETTINGS
from converter.exceptions import ConvertError, UnknownFormat, \ from converter.exceptions import ConvertError, UnknownFileFormat, \
IdentifyError IdentifyError
from converter.backends import ConverterBase from converter.backends import ConverterBase
from converter.literals import TRANSFORMATION_RESIZE, \ from converter.literals import TRANSFORMATION_RESIZE, \
@@ -79,7 +79,7 @@ class ConverterClass(ConverterBase):
error_line = proc.stderr.readline() error_line = proc.stderr.readline()
if (CONVERTER_ERROR_STRING_NO_DECODER in error_line) or (CONVERTER_ERROR_STARTS_WITH in error_line): 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 #Try to determine from error message which class of error is it
raise UnknownFormat raise UnknownFileFormat
else: else:
raise ConvertError(error_line) raise ConvertError(error_line)
@@ -116,6 +116,5 @@ class ConverterClass(ConverterBase):
def get_page_count(self, input_filepath): def get_page_count(self, input_filepath):
try: try:
return len(self.identify_file(unicode(input_filepath)).splitlines()) return len(self.identify_file(unicode(input_filepath)).splitlines())
except: except IdentifyError:
#TODO: send to other page number identifying program raise UnknownFileFormat
return 1
+4 -5
View File
@@ -3,7 +3,7 @@ import re
from converter.conf.settings import IM_IDENTIFY_PATH from converter.conf.settings import IM_IDENTIFY_PATH
from converter.conf.settings import IM_CONVERT_PATH from converter.conf.settings import IM_CONVERT_PATH
from converter.exceptions import ConvertError, UnknownFormat, \ from converter.exceptions import ConvertError, UnknownFileFormat, \
IdentifyError IdentifyError
from converter.backends import ConverterBase from converter.backends import ConverterBase
from converter.literals import TRANSFORMATION_RESIZE, \ from converter.literals import TRANSFORMATION_RESIZE, \
@@ -75,7 +75,7 @@ class ConverterClass(ConverterBase):
error_line = proc.stderr.readline() error_line = proc.stderr.readline()
if CONVERTER_ERROR_STRING_NO_DECODER in error_line: if CONVERTER_ERROR_STRING_NO_DECODER in error_line:
#Try to determine from error message which class of error is it #Try to determine from error message which class of error is it
raise UnknownFormat raise UnknownFileFormat
else: else:
raise ConvertError(error_line) raise ConvertError(error_line)
@@ -114,6 +114,5 @@ class ConverterClass(ConverterBase):
def get_page_count(self, input_filepath): def get_page_count(self, input_filepath):
try: try:
return len(self.identify_file(unicode(input_filepath)).splitlines()) return len(self.identify_file(unicode(input_filepath)).splitlines())
except: except IdentifyError:
#TODO: send to other page number identifying program raise UnknownFileFormat
return 1
+3 -5
View File
@@ -14,7 +14,7 @@ from django.utils.translation import ugettext_lazy as _
from mimetype.api import get_mimetype 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.backends import ConverterBase
from converter.literals import TRANSFORMATION_RESIZE, \ from converter.literals import TRANSFORMATION_RESIZE, \
TRANSFORMATION_ROTATE, TRANSFORMATION_ZOOM TRANSFORMATION_ROTATE, TRANSFORMATION_ZOOM
@@ -38,9 +38,7 @@ class ConverterClass(ConverterBase):
try: try:
im = Image.open(input_filepath) im = Image.open(input_filepath)
except IOError: #cannot identify image file except IOError: #cannot identify image file
# Return a page count of 1, to atleast allow the document return UnknownFileFormat
# to be created
return 1
try: try:
while 1: while 1:
@@ -87,7 +85,7 @@ class ConverterClass(ConverterBase):
try: try:
im = Image.open(input_filepath) im = Image.open(input_filepath)
except Exception: # Python Imaging Library doesn't recognize it as an image except Exception: # Python Imaging Library doesn't recognize it as an image
raise UnknownFormat raise UnknownFileFormat
finally: finally:
if tmpfile: if tmpfile:
cleanup(tmpfile) cleanup(tmpfile)
+3 -4
View File
@@ -5,17 +5,16 @@ class ConvertError(Exception):
pass pass
class UnknownFormat(ConvertError): class UnknownFileFormat(ConvertError):
""" """
Raised when the converter backend can't understand or there Raised when the converter backend can't understand a file
isn't an appropiate driver available
""" """
pass pass
class IdentifyError(ConvertError): class IdentifyError(ConvertError):
""" """
Raised by identify Raised by the graphcismagick and imagemagics identify program
""" """
pass pass
+2 -2
View File
@@ -16,7 +16,7 @@ from dynamic_search.api import register
from converter.api import get_page_count from converter.api import get_page_count
from converter.api import get_available_transformations_choices from converter.api import get_available_transformations_choices
from converter.api import convert 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, \ from mimetype.api import get_document_mimetype, get_icon_file_path, \
get_error_icon_file_path get_error_icon_file_path
@@ -245,7 +245,7 @@ class Document(models.Model):
try: try:
image_cache_name = self.get_image_cache_name(page=page) image_cache_name = self.get_image_cache_name(page=page)
return convert(image_cache_name, cleanup_files=False, size=size, zoom=zoom, rotation=rotation) 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) return get_icon_file_path(self.file_mimetype)
except UnkownConvertError: except UnkownConvertError:
return get_error_icon_file_path() return get_error_icon_file_path()
+2 -2
View File
@@ -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, \ from mimetype.api import get_icon_file_path, get_error_icon_file_path, \
get_mimetype get_mimetype
from converter.api import convert, cache_cleanup from converter.api import convert, cache_cleanup
from converter.exceptions import UnknownFormat, UnkownConvertError from converter.exceptions import UnknownFileFormat, UnkownConvertError
DEFAULT_STAGING_DIRECTORY = u'/tmp' DEFAULT_STAGING_DIRECTORY = u'/tmp'
@@ -138,7 +138,7 @@ class StagingFile(object):
def get_image(self, size, transformations): def get_image(self, size, transformations):
try: try:
return convert(self.filepath, size=size, cleanup_files=False, transformations=transformations) return convert(self.filepath, size=size, cleanup_files=False, transformations=transformations)
except UnknownFormat: except UnknownFileFormat:
return get_icon_file_path(get_mimetype(self.filepath)) return get_icon_file_path(get_mimetype(self.filepath))
except UnkownConvertError: except UnkownConvertError:
return get_error_icon_file_path() return get_error_icon_file_path()