mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Add CLI support for archiving original / unsafe PDFs
This commit is contained in:
parent
c6a0b59379
commit
b4849995e3
3 changed files with 25 additions and 7 deletions
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue