Load the converter class on demand

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2019-04-27 02:47:23 -04:00
parent 3fa08debe9
commit 799d47054e
9 changed files with 30 additions and 22 deletions

View File

@@ -172,6 +172,7 @@
add_to_object.
* Rename transformation manager method get_for_model to
get_for_object.
* Load the converter class on demand.
3.1.11 (2019-04-XX)
===================

View File

@@ -204,6 +204,7 @@ Other changes
add_to_object.
* Rename transformation manager method get_for_model to
get_for_object.
* Load the converter class on demand.
Removals
--------

View File

@@ -1,6 +1,5 @@
from __future__ import unicode_literals
from .runtime import converter_class # NOQA
from .transformations import ( # NOQA
BaseTransformation, TransformationResize, TransformationRotate,
TransformationZoom

View File

@@ -1,9 +0,0 @@
from __future__ import unicode_literals
from django.utils.module_loading import import_string
from .settings import setting_graphics_backend
backend = converter_class = import_string(
dotted_path=setting_graphics_backend.value
)

View File

@@ -0,0 +1,13 @@
from __future__ import unicode_literals
import logging
from django.utils.module_loading import import_string
from .settings import setting_graphics_backend
logger = logging.getLogger(__name__)
def get_converter_class():
return import_string(dotted_path=setting_graphics_backend.value)

View File

@@ -11,11 +11,12 @@ from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from mayan.apps.converter import (
converter_class, BaseTransformation, TransformationResize,
TransformationRotate, TransformationZoom
BaseTransformation, TransformationResize, TransformationRotate,
TransformationZoom
)
from mayan.apps.converter.literals import DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION
from mayan.apps.converter.models import Transformation
from mayan.apps.converter.utils import get_converter_class
from ..managers import DocumentPageCachedImage, DocumentPageManager
from ..settings import (
@@ -71,7 +72,7 @@ class DocumentPage(models.Model):
def detect_orientation(self):
with self.document_version.open() as file_object:
converter = converter_class(
converter = get_converter_class()(
file_object=file_object,
mime_type=self.document_version.mimetype
)
@@ -193,7 +194,7 @@ class DocumentPage(models.Model):
if not setting_disable_base_image_cache.value and storage_documentimagecache.exists(cache_filename):
logger.debug('Page cache file "%s" found', cache_filename)
converter = converter_class(
converter = get_converter_class()(
file_object=storage_documentimagecache.open(cache_filename)
)
@@ -202,7 +203,7 @@ class DocumentPage(models.Model):
logger.debug('Page cache file "%s" not found', cache_filename)
try:
converter = converter_class(
converter = get_converter_class()(
file_object=self.document_version.get_intermidiate_file()
)
converter.seek(page_number=self.page_number - 1)

View File

@@ -12,9 +12,10 @@ from django.urls import reverse
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from mayan.apps.converter import converter_class, TransformationRotate
from mayan.apps.converter import TransformationRotate
from mayan.apps.converter.exceptions import InvalidOfficeFormat, PageCountError
from mayan.apps.converter.models import Transformation
from mayan.apps.converter.utils import get_converter_class
from mayan.apps.mimetype.api import get_mimetype
from ..events import event_document_new_version, event_document_version_revert
@@ -168,7 +169,7 @@ class DocumentVersion(models.Model):
logger.debug('Intermidiate file "%s" not found.', cache_filename)
try:
converter = converter_class(file_object=self.open())
converter = get_converter_class()(file_object=self.open())
pdf_file_object = converter.to_pdf()
with storage_documentimagecache.open(cache_filename, mode='wb+') as file_object:
@@ -382,7 +383,7 @@ class DocumentVersion(models.Model):
def update_page_count(self, save=True):
try:
with self.open() as file_object:
converter = converter_class(
converter = get_converter_class()(
file_object=file_object, mime_type=self.mimetype
)
detected_pages = converter.get_page_count()

View File

@@ -1,6 +1,6 @@
from __future__ import unicode_literals
from mayan.apps.converter import converter_class
from mayan.apps.converter.utils import get_converter_class
class OCRBackendBase(object):
@@ -10,7 +10,7 @@ class OCRBackendBase(object):
if not transformations:
transformations = []
self.converter = converter_class(file_object=file_object)
self.converter = get_converter_class()(file_object=file_object)
for transformation in transformations:
self.converter.transform(transformation=transformation)

View File

@@ -13,7 +13,8 @@ from django.urls import reverse
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.six.moves.urllib.parse import quote_plus, unquote_plus
from mayan.apps.converter import TransformationResize, converter_class
from mayan.apps.converter import TransformationResize
from mayan.apps.converter.utils import get_converter_class
from .storages import storage_staging_file_image_cache
@@ -143,7 +144,7 @@ class StagingFile(object):
try:
file_object = open(self.get_full_path(), mode='rb')
converter = converter_class(file_object=file_object)
converter = get_converter_class()(file_object=file_object)
page_image = converter.get_page()