Add CLI support for archiving original / unsafe PDFs

This commit is contained in:
deeplow 2022-11-21 15:02:35 +00:00
parent c6a0b59379
commit b4849995e3
No known key found for this signature in database
GPG key ID: 577982871529A52A
3 changed files with 25 additions and 7 deletions

View file

@ -6,7 +6,7 @@ import click
from colorama import Back, Fore, Style
from . import args, container, errors
from .document import SAFE_EXTENSION
from .document import ARCHIVE_SUBDIR, SAFE_EXTENSION
from .logic import DangerzoneCore
from .util import get_version
@ -25,6 +25,12 @@ def print_header(s: str) -> None:
help=f"Default is filename ending with {SAFE_EXTENSION}",
)
@click.option("--ocr-lang", help="Language to OCR, defaults to none")
@click.option(
"--archive",
"archive",
flag_value=True,
help=f"Archives the unsafe version in a subdirectory named '{ARCHIVE_SUBDIR}'",
)
@click.argument(
"filenames",
required=True,
@ -34,20 +40,23 @@ def print_header(s: str) -> None:
)
@errors.handle_document_errors
def cli_main(
output_filename: Optional[str], ocr_lang: Optional[str], filenames: List[str]
output_filename: Optional[str],
ocr_lang: Optional[str],
filenames: List[str],
archive: bool,
) -> None:
setup_logging()
dangerzone = DangerzoneCore()
display_banner()
if len(filenames) == 1 and output_filename:
dangerzone.add_document_from_filename(filenames[0], output_filename)
dangerzone.add_document_from_filename(filenames[0], output_filename, archive)
elif len(filenames) > 1 and output_filename:
click.echo("--output-filename can only be used with one input file.")
exit(1)
else:
for filename in filenames:
dangerzone.add_document_from_filename(filename)
dangerzone.add_document_from_filename(filename, archive=archive)
# Validate OCR language
if ocr_lang:
@ -76,6 +85,12 @@ def cli_main(
print_header("Safe PDF(s) created successfully")
for document in documents_safe:
click.echo(document.output_filename)
if archive:
print_header(
f"Unsafe (original) documents moved to '{ARCHIVE_SUBDIR}' subdirectory"
)
if documents_failed != []:
print_header("Failed to convert document(s)")
for document in documents_failed:

View file

@ -97,7 +97,7 @@ def handle_document_errors(func: F) -> F:
except DocumentFilenameException as e:
if getattr(sys, "dangerzone_dev", False):
# Show the full traceback only on dev environments.
msg = "An exception occured while validating a document filename"
msg = "An exception occured while validating a document"
log.exception(msg)
click.echo(str(e))
exit(1)

View file

@ -42,9 +42,12 @@ class DangerzoneCore(object):
self.documents: List[Document] = []
def add_document_from_filename(
self, input_filename: str, output_filename: Optional[str] = None
self,
input_filename: str,
output_filename: Optional[str] = None,
archive: bool = False,
) -> None:
doc = Document(input_filename, output_filename)
doc = Document(input_filename, output_filename, archive=archive)
self.add_document(doc)
def add_document(self, doc: Document) -> None: