diff --git a/HISTORY.rst b/HISTORY.rst index a0c08311fb..6872f2ae4a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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) =================== diff --git a/docs/releases/3.2.rst b/docs/releases/3.2.rst index d99900cea6..593f28dbf9 100644 --- a/docs/releases/3.2.rst +++ b/docs/releases/3.2.rst @@ -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 -------- diff --git a/mayan/apps/converter/__init__.py b/mayan/apps/converter/__init__.py index a643e11bef..d5750d6e40 100644 --- a/mayan/apps/converter/__init__.py +++ b/mayan/apps/converter/__init__.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals -from .runtime import converter_class # NOQA from .transformations import ( # NOQA BaseTransformation, TransformationResize, TransformationRotate, TransformationZoom diff --git a/mayan/apps/converter/runtime.py b/mayan/apps/converter/runtime.py deleted file mode 100644 index 1af7f31380..0000000000 --- a/mayan/apps/converter/runtime.py +++ /dev/null @@ -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 -) diff --git a/mayan/apps/converter/utils.py b/mayan/apps/converter/utils.py new file mode 100644 index 0000000000..c82f1a42a2 --- /dev/null +++ b/mayan/apps/converter/utils.py @@ -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) diff --git a/mayan/apps/documents/models/document_page_models.py b/mayan/apps/documents/models/document_page_models.py index 5ec7d6c2c4..558f18fba3 100644 --- a/mayan/apps/documents/models/document_page_models.py +++ b/mayan/apps/documents/models/document_page_models.py @@ -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) diff --git a/mayan/apps/documents/models/document_version_models.py b/mayan/apps/documents/models/document_version_models.py index b76c048b65..9d4ee3d25d 100644 --- a/mayan/apps/documents/models/document_version_models.py +++ b/mayan/apps/documents/models/document_version_models.py @@ -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() diff --git a/mayan/apps/ocr/classes.py b/mayan/apps/ocr/classes.py index 487f5994a3..77c158dc8e 100644 --- a/mayan/apps/ocr/classes.py +++ b/mayan/apps/ocr/classes.py @@ -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) diff --git a/mayan/apps/sources/classes.py b/mayan/apps/sources/classes.py index eace9dca0f..a89458a7dc 100644 --- a/mayan/apps/sources/classes.py +++ b/mayan/apps/sources/classes.py @@ -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()