mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Move safe PDF naming logic to document.py
Let the Document class suggest the default filename for the safe PDF, based on the provided input filename, appended with the extension `-safe.pdf`. Previously, this logic was copy-pasted throughout the code, which made it difficult to maintain.
This commit is contained in:
parent
7aa08457bd
commit
2bed3c10e4
4 changed files with 17 additions and 13 deletions
|
@ -9,7 +9,7 @@ from colorama import Back, Fore, Style
|
||||||
|
|
||||||
from . import args, container, errors
|
from . import args, container, errors
|
||||||
from .container import convert
|
from .container import convert
|
||||||
from .document import Document
|
from .document import SAFE_EXTENSION, Document
|
||||||
from .global_common import GlobalCommon
|
from .global_common import GlobalCommon
|
||||||
from .util import get_version
|
from .util import get_version
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ def print_header(s: str) -> None:
|
||||||
@click.option(
|
@click.option(
|
||||||
"--output-filename",
|
"--output-filename",
|
||||||
callback=args.validate_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.option("--ocr-lang", help="Language to OCR, defaults to none")
|
||||||
@click.argument("filename", required=True, callback=args.validate_input_filename)
|
@click.argument("filename", required=True, callback=args.validate_input_filename)
|
||||||
|
@ -38,13 +38,11 @@ def cli_main(
|
||||||
|
|
||||||
document = Document(filename)
|
document = Document(filename)
|
||||||
|
|
||||||
# Validate safe PDF output filename
|
# Set PDF output filename
|
||||||
if output_filename:
|
if output_filename:
|
||||||
document.output_filename = output_filename
|
document.output_filename = output_filename
|
||||||
else:
|
else:
|
||||||
document.output_filename = (
|
document.set_default_output_filename()
|
||||||
f"{os.path.splitext(document.input_filename)[0]}-safe.pdf"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Validate OCR language
|
# Validate OCR language
|
||||||
if ocr_lang:
|
if ocr_lang:
|
||||||
|
|
|
@ -8,6 +8,8 @@ import appdirs
|
||||||
|
|
||||||
from .errors import DocumentFilenameException
|
from .errors import DocumentFilenameException
|
||||||
|
|
||||||
|
SAFE_EXTENSION = "-safe.pdf"
|
||||||
|
|
||||||
|
|
||||||
class Document:
|
class Document:
|
||||||
"""Track the state of a single document.
|
"""Track the state of a single document.
|
||||||
|
@ -75,3 +77,8 @@ class Document:
|
||||||
filename = self.normalize_filename(filename)
|
filename = self.normalize_filename(filename)
|
||||||
self.validate_output_filename(filename)
|
self.validate_output_filename(filename)
|
||||||
self._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}"
|
||||||
|
)
|
||||||
|
|
|
@ -453,12 +453,9 @@ class SettingsWidget(QtWidgets.QWidget):
|
||||||
f"Suspicious: {os.path.basename(self.document.input_filename)}"
|
f"Suspicious: {os.path.basename(self.document.input_filename)}"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update the save location
|
# Set the default save location
|
||||||
output_filename = (
|
self.document.set_default_output_filename()
|
||||||
f"{os.path.splitext(self.document.input_filename)[0]}-safe.pdf"
|
self.save_lineedit.setText(os.path.basename(self.document.output_filename))
|
||||||
)
|
|
||||||
self.document.output_filename = output_filename
|
|
||||||
self.save_lineedit.setText(os.path.basename(output_filename))
|
|
||||||
|
|
||||||
def save_browse_button_clicked(self) -> None:
|
def save_browse_button_clicked(self) -> None:
|
||||||
filename = QtWidgets.QFileDialog.getSaveFileName(
|
filename = QtWidgets.QFileDialog.getSaveFileName(
|
||||||
|
|
|
@ -6,13 +6,15 @@ import pytest
|
||||||
|
|
||||||
sys.dangerzone_dev = True # type: ignore[attr-defined]
|
sys.dangerzone_dev = True # type: ignore[attr-defined]
|
||||||
|
|
||||||
|
from dangerzone.document import SAFE_EXTENSION
|
||||||
|
|
||||||
SAMPLE_DIRECTORY = "test_docs"
|
SAMPLE_DIRECTORY = "test_docs"
|
||||||
BASIC_SAMPLE = "sample.pdf"
|
BASIC_SAMPLE = "sample.pdf"
|
||||||
test_docs_dir = Path(__file__).parent.joinpath(SAMPLE_DIRECTORY)
|
test_docs_dir = Path(__file__).parent.joinpath(SAMPLE_DIRECTORY)
|
||||||
test_docs = [
|
test_docs = [
|
||||||
p
|
p
|
||||||
for p in test_docs_dir.rglob("*")
|
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
|
# Pytest parameter decorators
|
||||||
|
|
Loading…
Reference in a new issue