diff --git a/dangerzone/cli.py b/dangerzone/cli.py index b305a11..45a7305 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -9,7 +9,7 @@ from colorama import Back, Fore, Style from . import args, container, errors from .container import convert -from .document import Document +from .document import SAFE_EXTENSION, Document from .global_common import GlobalCommon from .util import get_version @@ -23,7 +23,7 @@ def print_header(s: str) -> None: @click.option( "--output-filename", callback=args.validate_output_filename, - help="Default is filename ending with -safe.pdf", + help=f"Default is filename ending with {SAFE_EXTENSION}", ) @click.option("--ocr-lang", help="Language to OCR, defaults to none") @click.argument("filename", required=True, callback=args.validate_input_filename) @@ -38,13 +38,11 @@ def cli_main( document = Document(filename) - # Validate safe PDF output filename + # Set PDF output filename if output_filename: document.output_filename = output_filename else: - document.output_filename = ( - f"{os.path.splitext(document.input_filename)[0]}-safe.pdf" - ) + document.set_default_output_filename() # Validate OCR language if ocr_lang: diff --git a/dangerzone/document.py b/dangerzone/document.py index 9d29fa7..6cfc28f 100644 --- a/dangerzone/document.py +++ b/dangerzone/document.py @@ -8,6 +8,8 @@ import appdirs from .errors import DocumentFilenameException +SAFE_EXTENSION = "-safe.pdf" + class Document: """Track the state of a single document. @@ -75,3 +77,8 @@ class Document: filename = self.normalize_filename(filename) self.validate_output_filename(filename) self._output_filename = filename + + def set_default_output_filename(self) -> None: + self.output_filename = ( + f"{os.path.splitext(self.input_filename)[0]}{SAFE_EXTENSION}" + ) diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 436bac6..a425ee9 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -453,12 +453,9 @@ class SettingsWidget(QtWidgets.QWidget): f"Suspicious: {os.path.basename(self.document.input_filename)}" ) - # Update the save location - output_filename = ( - f"{os.path.splitext(self.document.input_filename)[0]}-safe.pdf" - ) - self.document.output_filename = output_filename - self.save_lineedit.setText(os.path.basename(output_filename)) + # Set the default save location + self.document.set_default_output_filename() + self.save_lineedit.setText(os.path.basename(self.document.output_filename)) def save_browse_button_clicked(self) -> None: filename = QtWidgets.QFileDialog.getSaveFileName( diff --git a/tests/__init__.py b/tests/__init__.py index f11b309..dd32eac 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,13 +6,15 @@ import pytest sys.dangerzone_dev = True # type: ignore[attr-defined] +from dangerzone.document import SAFE_EXTENSION + SAMPLE_DIRECTORY = "test_docs" BASIC_SAMPLE = "sample.pdf" test_docs_dir = Path(__file__).parent.joinpath(SAMPLE_DIRECTORY) test_docs = [ p for p in test_docs_dir.rglob("*") - if p.is_file() and not p.name.endswith("-safe.pdf") + if p.is_file() and not p.name.endswith(SAFE_EXTENSION) ] # Pytest parameter decorators