diff --git a/dangerzone/document.py b/dangerzone/document.py index 415b92d..ca004e8 100644 --- a/dangerzone/document.py +++ b/dangerzone/document.py @@ -109,7 +109,10 @@ class Document: @suffix.setter def suffix(self, suf: str) -> None: - self._suffix = suf + if self._output_filename is None: + self._suffix = suf + else: + raise errors.SuffixNotApplicableException() @property def default_output_filename(self) -> str: diff --git a/dangerzone/errors.py b/dangerzone/errors.py index e012235..0c2d3dd 100644 --- a/dangerzone/errors.py +++ b/dangerzone/errors.py @@ -71,6 +71,13 @@ class OutputDirIsNotDirException(DocumentFilenameException): super().__init__("Specified output directory is actually not a directory") +class SuffixNotApplicableException(DocumentFilenameException): + """Exception for when the suffix cannot be applied to the output filename.""" + + def __init__(self) -> None: + super().__init__("Cannot set a suffix after setting an output filename") + + def handle_document_errors(func: F) -> F: """Log document-related errors and exit gracefully.""" diff --git a/tests/test_document.py b/tests/test_document.py index 264bcdd..1cff181 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -113,6 +113,10 @@ def test_set_output_filename_suffix(sample_doc: str) -> None: d.suffix = safe_extension assert d.output_filename.endswith(safe_extension) + d.output_filename = "something_else.pdf" + with pytest.raises(errors.SuffixNotApplicableException) as e: + d.suffix = "-new-trusted.pdf" + def test_is_unconverted_by_default(sample_doc: str) -> None: d = Document(sample_doc)