Refactored the converter backend system
This commit is contained in:
@@ -2,10 +2,6 @@ from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from navigation.api import register_sidebar_template
|
||||
|
||||
TRANFORMATION_CHOICES = {
|
||||
u'rotate': u'-rotate %(degrees)d'
|
||||
}
|
||||
|
||||
formats_list = {'text': _('file formats'), 'view': 'formats_list', 'famfam': 'pictures'}
|
||||
|
||||
register_sidebar_template(['formats_list'], 'converter_file_formats_help.html')
|
||||
|
||||
+18
-52
@@ -3,62 +3,28 @@ import subprocess
|
||||
|
||||
from django.utils.importlib import import_module
|
||||
from django.template.defaultfilters import slugify
|
||||
|
||||
from converter.conf.settings import UNPAPER_PATH
|
||||
from converter.conf.settings import OCR_OPTIONS
|
||||
from converter.conf.settings import DEFAULT_OPTIONS
|
||||
from converter.conf.settings import LOW_QUALITY_OPTIONS
|
||||
from converter.conf.settings import HIGH_QUALITY_OPTIONS
|
||||
from converter.conf.settings import PRINT_QUALITY_OPTIONS
|
||||
from converter.conf.settings import GRAPHICS_BACKEND
|
||||
from converter.conf.settings import UNOCONV_PATH
|
||||
|
||||
from converter.exceptions import UnpaperError, OfficeConversionError
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from common import TEMPORARY_DIRECTORY
|
||||
from documents.utils import document_save_to_temp_dir
|
||||
|
||||
DEFAULT_ZOOM_LEVEL = 100
|
||||
DEFAULT_ROTATION = 0
|
||||
DEFAULT_PAGE_INDEX_NUMBER = 0
|
||||
DEFAULT_FILE_FORMAT = u'jpg'
|
||||
DEFAULT_OCR_FILE_FORMAT = u'tif'
|
||||
|
||||
QUALITY_DEFAULT = u'quality_default'
|
||||
QUALITY_LOW = u'quality_low'
|
||||
QUALITY_HIGH = u'quality_high'
|
||||
QUALITY_PRINT = u'quality_print'
|
||||
|
||||
QUALITY_SETTINGS = {
|
||||
QUALITY_DEFAULT: DEFAULT_OPTIONS,
|
||||
QUALITY_LOW: LOW_QUALITY_OPTIONS,
|
||||
QUALITY_HIGH: HIGH_QUALITY_OPTIONS,
|
||||
QUALITY_PRINT: PRINT_QUALITY_OPTIONS
|
||||
}
|
||||
from converter.conf.settings import UNPAPER_PATH
|
||||
from converter.conf.settings import OCR_OPTIONS
|
||||
from converter.conf.settings import UNOCONV_PATH
|
||||
from converter.exceptions import UnpaperError, OfficeConversionError
|
||||
from converter.utils import load_backend
|
||||
from converter.literals import DEFAULT_PAGE_INDEX_NUMBER, \
|
||||
DEFAULT_OCR_FILE_FORMAT, QUALITY_DEFAULT, DEFAULT_ZOOM_LEVEL, \
|
||||
DEFAULT_ROTATION, DEFAULT_FILE_FORMAT, QUALITY_PRINT
|
||||
|
||||
CONVERTER_OFFICE_FILE_EXTENSIONS = [
|
||||
u'ods', u'docx', u'doc'
|
||||
]
|
||||
|
||||
|
||||
def _lazy_load(fn):
|
||||
_cached = []
|
||||
|
||||
def _decorated():
|
||||
if not _cached:
|
||||
_cached.append(fn())
|
||||
return _cached[0]
|
||||
return _decorated
|
||||
|
||||
|
||||
@_lazy_load
|
||||
def _get_backend():
|
||||
return import_module(GRAPHICS_BACKEND)
|
||||
|
||||
try:
|
||||
backend = _get_backend()
|
||||
except ImportError:
|
||||
raise ImportError(u'Missing or incorrect converter backend: %s' % GRAPHICS_BACKEND)
|
||||
backend = load_backend().ConverterClass()
|
||||
except ImproperlyConfigured:
|
||||
raise ImproperlyConfigured(u'Missing or incorrect converter backend: %s' % GRAPHICS_BACKEND)
|
||||
|
||||
|
||||
def cleanup(filename):
|
||||
@@ -173,7 +139,7 @@ def convert(input_filepath, *args, **kwargs):
|
||||
if format == u'jpg':
|
||||
extra_options += u' -quality 85'
|
||||
try:
|
||||
backend.execute_convert(input_filepath=input_arg, arguments=extra_options, output_filepath=u'%s:%s' % (file_format, output_filepath), quality=quality)
|
||||
backend.convert_file(input_filepath=input_arg, arguments=extra_options, output_filepath=u'%s:%s' % (file_format, output_filepath), quality=quality)
|
||||
finally:
|
||||
if cleanup_files:
|
||||
cleanup(input_filepath)
|
||||
@@ -185,7 +151,7 @@ def convert(input_filepath, *args, **kwargs):
|
||||
|
||||
def get_page_count(input_filepath):
|
||||
try:
|
||||
return len(backend.execute_identify(unicode(input_filepath)).splitlines())
|
||||
return len(backend.identify_file(unicode(input_filepath)).splitlines())
|
||||
except:
|
||||
#TODO: send to other page number identifying program
|
||||
return 1
|
||||
@@ -195,7 +161,7 @@ def get_document_dimensions(document, *args, **kwargs):
|
||||
document_filepath = create_image_cache_filename(document.checksum, *args, **kwargs)
|
||||
if os.path.exists(document_filepath):
|
||||
options = [u'-format', u'%w %h']
|
||||
return [int(dimension) for dimension in backend.execute_identify(unicode(document_filepath), options).split()]
|
||||
return [int(dimension) for dimension in backend.identify_file(unicode(document_filepath), options).split()]
|
||||
else:
|
||||
return [0, 0]
|
||||
|
||||
@@ -219,13 +185,13 @@ def convert_document_for_ocr(document, page=DEFAULT_PAGE_INDEX_NUMBER, file_form
|
||||
transformation_string, warnings = document_page.get_transformation_string()
|
||||
|
||||
#Apply default transformations
|
||||
backend.execute_convert(input_filepath=input_arg, quality=QUALITY_HIGH, arguments=transformation_string, output_filepath=transformation_output_file)
|
||||
backend.convert_file(input_filepath=input_arg, quality=QUALITY_HIGH, arguments=transformation_string, output_filepath=transformation_output_file)
|
||||
#Do OCR operations
|
||||
backend.execute_convert(input_filepath=transformation_output_file, arguments=OCR_OPTIONS, output_filepath=unpaper_input_file)
|
||||
backend.convert_file(input_filepath=transformation_output_file, arguments=OCR_OPTIONS, output_filepath=unpaper_input_file)
|
||||
# Process by unpaper
|
||||
execute_unpaper(input_filepath=unpaper_input_file, output_filepath=unpaper_output_file)
|
||||
# Convert to tif
|
||||
backend.execute_convert(input_filepath=unpaper_output_file, output_filepath=convert_output_file)
|
||||
backend.convert_file(input_filepath=unpaper_output_file, output_filepath=convert_output_file)
|
||||
finally:
|
||||
cleanup(transformation_output_file)
|
||||
cleanup(unpaper_input_file)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
class ConverterBase(object):
|
||||
"""
|
||||
Base class that all backend classes must inherit
|
||||
"""
|
||||
|
||||
def identify_file(self, input_filepath, *args, **kwargs):
|
||||
raise NotImplementedError("Your %s class has not defined a identify_file() method, which is required." % self.__class__.__name__)
|
||||
|
||||
def identify_document(self, document, *args, **kwargs):
|
||||
raise NotImplementedError("Your %s class has not defined a identify_document() method, which is required." % self.__class__.__name__)
|
||||
|
||||
def convert_file(self, input_filepath, *args, **kwargs):
|
||||
raise NotImplementedError("Your %s class has not defined a convert_file() method, which is required." % self.__class__.__name__)
|
||||
|
||||
def convert_document(self, document, *args, **kwargs):
|
||||
raise NotImplementedError("Your %s class has not defined a convert_document() method, which is required." % self.__class__.__name__)
|
||||
|
||||
def get_format_list(self):
|
||||
raise NotImplementedError("Your %s class has not defined a get_format_list() method, which is required." % self.__class__.__name__)
|
||||
|
||||
def get_available_transformations(self):
|
||||
raise NotImplementedError("Your %s class has not defined a get_available_transformations() method, which is required." % self.__class__.__name__)
|
||||
|
||||
def get_available_transformations_labels(self):
|
||||
return ([(name, data['label']) for name, data in self.get_available_transformations().items()])
|
||||
|
||||
def get_transformation_string(self, transformation_list):
|
||||
transformations = []
|
||||
warnings = []
|
||||
transformation_choices = self.get_available_transformations()
|
||||
for transformation in transformation_list:
|
||||
try:
|
||||
if transformation['transformation'] in transformation_choices:
|
||||
transformations.append(
|
||||
transformation_choices[transformation['transformation']]['command_line'] % eval(
|
||||
transformation['arguments']
|
||||
)
|
||||
)
|
||||
except Exception, e:
|
||||
warnings.append(e)
|
||||
|
||||
return u' '.join(transformations), warnings
|
||||
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
from converter.conf.settings import GM_PATH
|
||||
from converter.conf.settings import GM_SETTINGS
|
||||
from converter.api import QUALITY_DEFAULT, QUALITY_SETTINGS
|
||||
from converter.exceptions import ConvertError, UnknownFormat, IdentifyError
|
||||
|
||||
CONVERTER_ERROR_STRING_NO_DECODER = u'No decode delegate for this image format'
|
||||
CONVERTER_ERROR_STARTS_WITH = u'starts with'
|
||||
|
||||
|
||||
def execute_identify(input_filepath, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(GM_PATH))
|
||||
command.append(u'identify')
|
||||
if arguments:
|
||||
command.extend(arguments)
|
||||
command.append(unicode(input_filepath))
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise IdentifyError(proc.stderr.readline())
|
||||
return proc.stdout.read()
|
||||
|
||||
|
||||
def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(GM_PATH))
|
||||
command.append(u'convert')
|
||||
command.extend(unicode(QUALITY_SETTINGS[quality]).split())
|
||||
command.extend(unicode(GM_SETTINGS).split())
|
||||
command.append(unicode(input_filepath))
|
||||
if arguments:
|
||||
command.extend(unicode(arguments).split())
|
||||
command.append(unicode(output_filepath))
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
#Got an error from convert program
|
||||
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
|
||||
else:
|
||||
raise ConvertError(error_line)
|
||||
|
||||
|
||||
def get_format_list():
|
||||
"""
|
||||
Call GraphicsMagick to parse all of it's supported file formats, and
|
||||
return a list of the names and descriptions
|
||||
"""
|
||||
format_regex = re.compile(' *([A-Z0-9]+)[*]? +([A-Z0-9]+) +([rw\-+]+) *(.*).*')
|
||||
formats = []
|
||||
command = []
|
||||
command.append(unicode(GM_PATH))
|
||||
command.append(u'convert')
|
||||
command.append(u'-list')
|
||||
command.append(u'formats')
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise ConvertError(proc.stderr.readline())
|
||||
|
||||
for line in proc.stdout.readlines():
|
||||
fields = format_regex.findall(line)
|
||||
if fields:
|
||||
formats.append((fields[0][0], fields[0][3]))
|
||||
|
||||
return formats
|
||||
@@ -0,0 +1,85 @@
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from converter.conf.settings import GM_PATH
|
||||
from converter.conf.settings import GM_SETTINGS
|
||||
from converter.literals import QUALITY_DEFAULT, QUALITY_SETTINGS
|
||||
from converter.exceptions import ConvertError, UnknownFormat, IdentifyError
|
||||
from converter.backends import ConverterBase
|
||||
|
||||
CONVERTER_ERROR_STRING_NO_DECODER = u'No decode delegate for this image format'
|
||||
CONVERTER_ERROR_STARTS_WITH = u'starts with'
|
||||
|
||||
|
||||
class ConverterClass(ConverterBase):
|
||||
def identify_file(self, input_filepath, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(GM_PATH))
|
||||
command.append(u'identify')
|
||||
if arguments:
|
||||
command.extend(arguments)
|
||||
command.append(unicode(input_filepath))
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise IdentifyError(proc.stderr.readline())
|
||||
return proc.stdout.read()
|
||||
|
||||
|
||||
def convert_file(self, input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(GM_PATH))
|
||||
command.append(u'convert')
|
||||
command.extend(unicode(QUALITY_SETTINGS[quality]).split())
|
||||
command.extend(unicode(GM_SETTINGS).split())
|
||||
command.append(unicode(input_filepath))
|
||||
if arguments:
|
||||
command.extend(unicode(arguments).split())
|
||||
command.append(unicode(output_filepath))
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
#Got an error from convert program
|
||||
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
|
||||
else:
|
||||
raise ConvertError(error_line)
|
||||
|
||||
|
||||
def get_format_list(self):
|
||||
"""
|
||||
Call GraphicsMagick to parse all of it's supported file formats, and
|
||||
return a list of the names and descriptions
|
||||
"""
|
||||
format_regex = re.compile(' *([A-Z0-9]+)[*]? +([A-Z0-9]+) +([rw\-+]+) *(.*).*')
|
||||
formats = []
|
||||
command = []
|
||||
command.append(unicode(GM_PATH))
|
||||
command.append(u'convert')
|
||||
command.append(u'-list')
|
||||
command.append(u'formats')
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise ConvertError(proc.stderr.readline())
|
||||
|
||||
for line in proc.stdout.readlines():
|
||||
fields = format_regex.findall(line)
|
||||
if fields:
|
||||
formats.append((fields[0][0], fields[0][3]))
|
||||
|
||||
return formats
|
||||
|
||||
|
||||
def get_available_transformations(self):
|
||||
return {
|
||||
'rotate': {
|
||||
'label': _(u'Rotate [degrees]'),
|
||||
'arguments': [{'name': 'degrees'}],
|
||||
'command_line': u'-rotate %(degrees)d'
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
from converter.conf.settings import IM_IDENTIFY_PATH
|
||||
from converter.conf.settings import IM_CONVERT_PATH
|
||||
from converter.api import QUALITY_DEFAULT, QUALITY_SETTINGS
|
||||
from converter.exceptions import ConvertError, UnknownFormat, \
|
||||
IdentifyError
|
||||
|
||||
CONVERTER_ERROR_STRING_NO_DECODER = u'no decode delegate for this image format'
|
||||
|
||||
|
||||
def execute_identify(input_filepath, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(IM_IDENTIFY_PATH))
|
||||
if arguments:
|
||||
command.extend(arguments)
|
||||
command.append(unicode(input_filepath))
|
||||
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise IdentifyError(proc.stderr.readline())
|
||||
return proc.stdout.read()
|
||||
|
||||
|
||||
def execute_convert(input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(IM_CONVERT_PATH))
|
||||
command.extend(unicode(QUALITY_SETTINGS[quality]).split())
|
||||
command.append(unicode(input_filepath))
|
||||
if arguments:
|
||||
command.extend(unicode(arguments).split())
|
||||
command.append(unicode(output_filepath))
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
#Got an error from convert program
|
||||
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
|
||||
else:
|
||||
raise ConvertError(error_line)
|
||||
|
||||
|
||||
def get_format_list():
|
||||
"""
|
||||
Call ImageMagick to parse all of it's supported file formats, and
|
||||
return a list of the names and descriptions
|
||||
"""
|
||||
format_regex = re.compile(' *([A-Z0-9]+)[*]? +([A-Z0-9]+) +([rw\-+]+) *(.*).*')
|
||||
formats = []
|
||||
command = []
|
||||
command.append(unicode(IM_CONVERT_PATH))
|
||||
command.append(u'-list')
|
||||
command.append(u'format')
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise ConvertError(proc.stderr.readline())
|
||||
|
||||
for line in proc.stdout.readlines():
|
||||
fields = format_regex.findall(line)
|
||||
if fields:
|
||||
formats.append((fields[0][0], fields[0][3]))
|
||||
|
||||
return formats
|
||||
@@ -0,0 +1,82 @@
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from converter.conf.settings import IM_IDENTIFY_PATH
|
||||
from converter.conf.settings import IM_CONVERT_PATH
|
||||
from converter.api import QUALITY_DEFAULT, QUALITY_SETTINGS
|
||||
from converter.exceptions import ConvertError, UnknownFormat, \
|
||||
IdentifyError
|
||||
from converter.backends import ConverterBase
|
||||
|
||||
CONVERTER_ERROR_STRING_NO_DECODER = u'no decode delegate for this image format'
|
||||
|
||||
|
||||
class ConverterClass(ConverterBase):
|
||||
def identify_file(self, input_filepath, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(IM_IDENTIFY_PATH))
|
||||
if arguments:
|
||||
command.extend(arguments)
|
||||
command.append(unicode(input_filepath))
|
||||
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise IdentifyError(proc.stderr.readline())
|
||||
return proc.stdout.read()
|
||||
|
||||
|
||||
def convert_file(self, input_filepath, output_filepath, quality=QUALITY_DEFAULT, arguments=None):
|
||||
command = []
|
||||
command.append(unicode(IM_CONVERT_PATH))
|
||||
command.extend(unicode(QUALITY_SETTINGS[quality]).split())
|
||||
command.append(unicode(input_filepath))
|
||||
if arguments:
|
||||
command.extend(unicode(arguments).split())
|
||||
command.append(unicode(output_filepath))
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
#Got an error from convert program
|
||||
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
|
||||
else:
|
||||
raise ConvertError(error_line)
|
||||
|
||||
|
||||
def get_format_list(self):
|
||||
"""
|
||||
Call ImageMagick to parse all of it's supported file formats, and
|
||||
return a list of the names and descriptions
|
||||
"""
|
||||
format_regex = re.compile(' *([A-Z0-9]+)[*]? +([A-Z0-9]+) +([rw\-+]+) *(.*).*')
|
||||
formats = []
|
||||
command = []
|
||||
command.append(unicode(IM_CONVERT_PATH))
|
||||
command.append(u'-list')
|
||||
command.append(u'format')
|
||||
proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
return_code = proc.wait()
|
||||
if return_code != 0:
|
||||
raise ConvertError(proc.stderr.readline())
|
||||
|
||||
for line in proc.stdout.readlines():
|
||||
fields = format_regex.findall(line)
|
||||
if fields:
|
||||
formats.append((fields[0][0], fields[0][3]))
|
||||
|
||||
return formats
|
||||
|
||||
|
||||
def get_available_transformations(self):
|
||||
return {
|
||||
'rotate': {
|
||||
'label': _(u'Rotate [degrees]'),
|
||||
'arguments': [{'name': 'degrees'}],
|
||||
'command_line': u'-rotate %(degrees)d'
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
from converter.conf.settings import DEFAULT_OPTIONS
|
||||
from converter.conf.settings import LOW_QUALITY_OPTIONS
|
||||
from converter.conf.settings import HIGH_QUALITY_OPTIONS
|
||||
from converter.conf.settings import PRINT_QUALITY_OPTIONS
|
||||
|
||||
DEFAULT_ZOOM_LEVEL = 100
|
||||
DEFAULT_ROTATION = 0
|
||||
DEFAULT_PAGE_INDEX_NUMBER = 0
|
||||
DEFAULT_FILE_FORMAT = u'jpg'
|
||||
DEFAULT_OCR_FILE_FORMAT = u'tif'
|
||||
|
||||
QUALITY_DEFAULT = u'quality_default'
|
||||
QUALITY_LOW = u'quality_low'
|
||||
QUALITY_HIGH = u'quality_high'
|
||||
QUALITY_PRINT = u'quality_print'
|
||||
|
||||
QUALITY_SETTINGS = {
|
||||
QUALITY_DEFAULT: DEFAULT_OPTIONS,
|
||||
QUALITY_LOW: LOW_QUALITY_OPTIONS,
|
||||
QUALITY_HIGH: HIGH_QUALITY_OPTIONS,
|
||||
QUALITY_PRINT: PRINT_QUALITY_OPTIONS
|
||||
}
|
||||
+53
-2
@@ -1,6 +1,10 @@
|
||||
import os
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
|
||||
#http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python
|
||||
|
||||
|
||||
def copyfile(source, dest, buffer_size=1024 * 1024):
|
||||
"""
|
||||
Copy a file from source to dest. source and dest
|
||||
@@ -21,3 +25,50 @@ def copyfile(source, dest, buffer_size=1024 * 1024):
|
||||
|
||||
source.close()
|
||||
dest.close()
|
||||
|
||||
|
||||
def _lazy_load(fn):
|
||||
_cached = []
|
||||
|
||||
def _decorated():
|
||||
if not _cached:
|
||||
_cached.append(fn())
|
||||
return _cached[0]
|
||||
return _decorated
|
||||
|
||||
|
||||
@_lazy_load
|
||||
def load_backend():
|
||||
from converter.conf.settings import GRAPHICS_BACKEND as backend_name
|
||||
|
||||
try:
|
||||
module = import_module('.base', 'converter.backends.%s' % backend_name)
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"Short names for CONVERTER_BACKEND are deprecated; prepend with 'converter.backends.'",
|
||||
PendingDeprecationWarning
|
||||
)
|
||||
return module
|
||||
except ImportError, e:
|
||||
# Look for a fully qualified converter backend name
|
||||
try:
|
||||
return import_module('.base', backend_name)
|
||||
except ImportError, e_user:
|
||||
# The converter backend wasn't found. Display a helpful error message
|
||||
# listing all possible (built-in) converter backends.
|
||||
backend_dir = os.path.join(os.path.dirname(__file__), 'backends')
|
||||
try:
|
||||
available_backends = [f for f in os.listdir(backend_dir)
|
||||
if os.path.isdir(os.path.join(backend_dir, f))
|
||||
and not f.startswith('.')]
|
||||
except EnvironmentError:
|
||||
available_backends = []
|
||||
available_backends.sort()
|
||||
if backend_name not in available_backends:
|
||||
error_msg = ("%r isn't an available converter backend. \n" +
|
||||
"Try using converter.backends.XXX, where XXX is one of:\n %s\n" +
|
||||
"Error was: %s") % \
|
||||
(backend_name, ", ".join(map(repr, available_backends)), e_user)
|
||||
raise ImproperlyConfigured(error_msg)
|
||||
else:
|
||||
raise # If there's some other error, this must be an error in Mayan itself.
|
||||
|
||||
Reference in New Issue
Block a user