Skip to content

Commit

Permalink
Merge branch 'feature/pdf-ids' into develop
Browse files Browse the repository at this point in the history
 [SVCS-699]
 Closes: #338
  • Loading branch information
felliott committed May 18, 2018
2 parents 5caac67 + 5d1ebe8 commit c19ae0e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
3 changes: 2 additions & 1 deletion mfr/core/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class BaseExporter(metaclass=abc.ABCMeta):

def __init__(self, ext, source_file_path, output_file_path, format):
def __init__(self, ext, source_file_path, output_file_path, format, metadata):

"""Initialize the base exporter.
Expand All @@ -19,6 +19,7 @@ def __init__(self, ext, source_file_path, output_file_path, format):
self.source_file_path = source_file_path
self.output_file_path = output_file_path
self.format = format
self.metadata = metadata
self.exporter_metrics = MetricsRecord('exporter')
if self._get_module_name():
self.metrics = self.exporter_metrics.new_subrecord(self._get_module_name())
Expand Down
4 changes: 2 additions & 2 deletions mfr/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def make_provider(name, request, url):
)


def make_exporter(name, source_file_path, output_file_path, format):
def make_exporter(name, source_file_path, output_file_path, format, metadata):
"""Returns an instance of :class:`mfr.core.extension.BaseExporter`
:param str name: The name of the extension to instantiate. (.jpg, .docx, etc)
Expand All @@ -49,7 +49,7 @@ def make_exporter(name, source_file_path, output_file_path, format):
namespace='mfr.exporters',
name=normalized_name,
invoke_on_load=True,
invoke_args=(normalized_name, source_file_path, output_file_path, format),
invoke_args=(normalized_name, source_file_path, output_file_path, format, metadata),
).driver
except RuntimeError:
raise exceptions.MakeExporterError(
Expand Down
12 changes: 12 additions & 0 deletions mfr/extensions/unoconv/export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import os
import subprocess

from pdfrw import (
PdfReader,
PdfWriter
)


from mfr.core import extension
from mfr.core import exceptions

Expand All @@ -20,6 +26,7 @@ def export(self):
'-vvv',
self.source_file_path
], check=True, timeout=settings.UNOCONV_TIMEOUT)

except subprocess.CalledProcessError as err:
name, extension = os.path.splitext(os.path.split(self.source_file_path)[-1])
raise exceptions.SubprocessError(
Expand All @@ -32,3 +39,8 @@ def export(self):
extension=extension or '',
exporter_class='unoconv',
)

pdf = PdfReader(self.output_file_path)
pdf.ID[0] = self.metadata.unique_key
pdf.ID[1] = self.metadata.unique_key
PdfWriter(self.output_file_path, trailer=pdf).write()
5 changes: 3 additions & 2 deletions mfr/server/handlers/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def prepare(self):
self.format = format[0].decode('utf-8')
self.exporter_name = utils.get_exporter_name(self.metadata.ext)

self.cache_file_id = '{}.{}'.format(self.metadata.unique_key, self.format)
self.cache_file_id = '{}.{}'.format(self.self.metadata.unique_key, self.format)

if self.exporter_name:
cache_file_path_str = '/export/{}.{}'.format(self.cache_file_id, self.exporter_name)
Expand Down Expand Up @@ -87,7 +87,8 @@ async def get(self):
self.metadata.ext,
self.source_file_path.full_path,
self.output_file_path.full_path,
self.format
self.format,
self.metadata,
)

self.extension_metrics.add('class', exporter._get_module_name())
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mistune==0.7

# Pdf
reportlab==3.4.0
pdfrw==0.4.0

# Pptx
# python-pptx==0.5.7
Expand Down
18 changes: 12 additions & 6 deletions tests/extensions/image/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def test_jpg(self, directory, file_name, tolerance):
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = ImageExporter(source_file_path=source_file_path, ext='.jpg',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={})

assert not os.path.exists(output_file_path)

Expand Down Expand Up @@ -64,7 +65,8 @@ def test_png_with_transparency(self, directory):
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = ImageExporter(source_file_path=source_file_path, ext='.png',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={})

assert not os.path.exists(output_file_path)

Expand Down Expand Up @@ -98,7 +100,8 @@ def test_bmp(self, directory):
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = ImageExporter(source_file_path=source_file_path, ext='.bmp',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={})

assert not os.path.exists(output_file_path)

Expand All @@ -123,7 +126,8 @@ def test_ratio(self, directory):
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = ImageExporter(source_file_path=source_file_path, ext='.png',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={})

assert not os.path.exists(output_file_path)

Expand All @@ -146,7 +150,8 @@ def test_exception_file_not_found(self, directory):
'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = ImageExporter(source_file_path=source_file_path, ext='.jpg',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={})

assert not os.path.exists(output_file_path)
with pytest.raises(exceptions.PillowImageError) as e:
Expand All @@ -159,7 +164,8 @@ def test_exception_courrupt_file(self, directory):
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = ImageExporter(source_file_path=source_file_path, ext='.jpg',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={})

assert not os.path.exists(output_file_path)
with pytest.raises(exceptions.PillowImageError) as e:
Expand Down
6 changes: 4 additions & 2 deletions tests/extensions/pdf/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def test_single_page_tiff(self, directory, file_name):
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = PdfExporter(source_file_path=source_file_path, ext='.tif',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={'unique_key': 'moo moo moo'})

assert not os.path.exists(output_file_path)

Expand All @@ -62,7 +63,8 @@ def test_bad_tiff(self, directory):
output_file_path = os.path.join(directory, 'test.{}'.format(settings.EXPORT_TYPE))
format = '{}.{}'.format(settings.EXPORT_MAXIMUM_SIZE, settings.EXPORT_TYPE)
exporter = PdfExporter(source_file_path=source_file_path, ext='.tif',
output_file_path=output_file_path, format=format)
output_file_path=output_file_path, format=format,
metadata={'unique_key': 'moo moo moo'})

assert not os.path.exists(output_file_path)

Expand Down

0 comments on commit c19ae0e

Please sign in to comment.