Added merge PDF writer as alternative to Zipfile
This commit is contained in:
parent
515875f3ff
commit
8bd40e2c3e
@ -1,12 +1,30 @@
|
|||||||
import os
|
import os
|
||||||
from zipfile import ZipFile
|
|
||||||
|
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
|
from documents.parsers import merge_pdfs
|
||||||
|
|
||||||
|
|
||||||
|
class MergedPdfFile:
|
||||||
|
def __init__(self, output_file_path):
|
||||||
|
self._output_file_path = output_file_path
|
||||||
|
self._input_file_paths = []
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
merge_pdfs(self._input_file_paths, self._output_file_path)
|
||||||
|
|
||||||
|
def namelist(self):
|
||||||
|
return self._input_file_paths
|
||||||
|
|
||||||
|
def write(self, document_path, _):
|
||||||
|
self._input_file_paths.append(document_path)
|
||||||
|
|
||||||
|
|
||||||
class BulkArchiveStrategy:
|
class BulkArchiveStrategy:
|
||||||
def __init__(self, zipf: ZipFile, follow_formatting: bool = False):
|
def __init__(self, writer, follow_formatting: bool = False):
|
||||||
self.zipf = zipf
|
self.writer = writer
|
||||||
if follow_formatting:
|
if follow_formatting:
|
||||||
self.make_unique_filename = self._formatted_filepath
|
self.make_unique_filename = self._formatted_filepath
|
||||||
else:
|
else:
|
||||||
@ -27,7 +45,7 @@ class BulkArchiveStrategy:
|
|||||||
counter = 0
|
counter = 0
|
||||||
while True:
|
while True:
|
||||||
filename = folder + doc.get_public_filename(archive, counter)
|
filename = folder + doc.get_public_filename(archive, counter)
|
||||||
if filename in self.zipf.namelist():
|
if filename in self.writer.namelist():
|
||||||
counter += 1
|
counter += 1
|
||||||
else:
|
else:
|
||||||
return filename
|
return filename
|
||||||
@ -57,29 +75,29 @@ class BulkArchiveStrategy:
|
|||||||
|
|
||||||
class OriginalsOnlyStrategy(BulkArchiveStrategy):
|
class OriginalsOnlyStrategy(BulkArchiveStrategy):
|
||||||
def add_document(self, doc: Document):
|
def add_document(self, doc: Document):
|
||||||
self.zipf.write(doc.source_path, self.make_unique_filename(doc))
|
self.writer.write(doc.source_path, self.make_unique_filename(doc))
|
||||||
|
|
||||||
|
|
||||||
class ArchiveOnlyStrategy(BulkArchiveStrategy):
|
class ArchiveOnlyStrategy(BulkArchiveStrategy):
|
||||||
def add_document(self, doc: Document):
|
def add_document(self, doc: Document):
|
||||||
if doc.has_archive_version:
|
if doc.has_archive_version:
|
||||||
self.zipf.write(
|
self.writer.write(
|
||||||
doc.archive_path,
|
doc.archive_path,
|
||||||
self.make_unique_filename(doc, archive=True),
|
self.make_unique_filename(doc, archive=True),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.zipf.write(doc.source_path, self.make_unique_filename(doc))
|
self.writer.write(doc.source_path, self.make_unique_filename(doc))
|
||||||
|
|
||||||
|
|
||||||
class OriginalAndArchiveStrategy(BulkArchiveStrategy):
|
class OriginalAndArchiveStrategy(BulkArchiveStrategy):
|
||||||
def add_document(self, doc: Document):
|
def add_document(self, doc: Document):
|
||||||
if doc.has_archive_version:
|
if doc.has_archive_version:
|
||||||
self.zipf.write(
|
self.writer.write(
|
||||||
doc.archive_path,
|
doc.archive_path,
|
||||||
self.make_unique_filename(doc, archive=True, folder="archive/"),
|
self.make_unique_filename(doc, archive=True, folder="archive/"),
|
||||||
)
|
)
|
||||||
|
|
||||||
self.zipf.write(
|
self.writer.write(
|
||||||
doc.source_path,
|
doc.source_path,
|
||||||
self.make_unique_filename(doc, folder="originals/"),
|
self.make_unique_filename(doc, folder="originals/"),
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user