mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
chore: remove unused code
This commit removes code that's not being used, it can be exceptions with the `as e` where the exception itself is not used, the same with `with` statements, and some other parts where there were duplicated code.
This commit is contained in:
parent
99f1e15fd2
commit
cbbd6afcc1
10 changed files with 21 additions and 31 deletions
|
@ -13,8 +13,6 @@ from .isolation_provider.qubes import Qubes, is_qubes_native_conversion
|
||||||
from .logic import DangerzoneCore
|
from .logic import DangerzoneCore
|
||||||
from .util import get_version, replace_control_chars
|
from .util import get_version, replace_control_chars
|
||||||
|
|
||||||
F = TypeVar("F", bound=Callable[..., Any])
|
|
||||||
|
|
||||||
|
|
||||||
def print_header(s: str) -> None:
|
def print_header(s: str) -> None:
|
||||||
click.echo("")
|
click.echo("")
|
||||||
|
|
|
@ -217,7 +217,6 @@ class DocumentToPixels(DangerzoneConverter):
|
||||||
raise errors.MaxPagesException()
|
raise errors.MaxPagesException()
|
||||||
await self.write_page_count(doc.page_count)
|
await self.write_page_count(doc.page_count)
|
||||||
|
|
||||||
page_base = "/tmp/page"
|
|
||||||
for page in doc.pages():
|
for page in doc.pages():
|
||||||
# TODO check if page.number is doc-controlled
|
# TODO check if page.number is doc-controlled
|
||||||
page_num = page.number + 1 # pages start in 1
|
page_num = page.number + 1 # pages start in 1
|
||||||
|
|
|
@ -232,13 +232,13 @@ class UpdaterThread(QtCore.QThread):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
info = res.json()
|
info = res.json()
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError:
|
||||||
raise ValueError(f"Received a non-JSON response from {self.GH_RELEASE_URL}")
|
raise ValueError(f"Received a non-JSON response from {self.GH_RELEASE_URL}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
version = info["tag_name"].lstrip("v")
|
version = info["tag_name"].lstrip("v")
|
||||||
changelog = markdown.markdown(info["body"])
|
changelog = markdown.markdown(info["body"])
|
||||||
except KeyError as e:
|
except KeyError:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Missing required fields in JSON response from {self.GH_RELEASE_URL}"
|
f"Missing required fields in JSON response from {self.GH_RELEASE_URL}"
|
||||||
)
|
)
|
||||||
|
|
|
@ -100,7 +100,7 @@ class IsolationProvider(ABC):
|
||||||
assert p.stdin is not None
|
assert p.stdin is not None
|
||||||
p.stdin.write(f.read())
|
p.stdin.write(f.read())
|
||||||
p.stdin.close()
|
p.stdin.close()
|
||||||
except BrokenPipeError as e:
|
except BrokenPipeError:
|
||||||
raise errors.ConverterProcException()
|
raise errors.ConverterProcException()
|
||||||
|
|
||||||
assert p.stdout
|
assert p.stdout
|
||||||
|
|
|
@ -114,9 +114,6 @@ class Qubes(IsolationProvider):
|
||||||
with zipfile.ZipFile(temp_file, "w") as z:
|
with zipfile.ZipFile(temp_file, "w") as z:
|
||||||
z.mkdir("dangerzone/")
|
z.mkdir("dangerzone/")
|
||||||
z.writestr("dangerzone/__init__.py", "")
|
z.writestr("dangerzone/__init__.py", "")
|
||||||
import dangerzone.conversion
|
|
||||||
|
|
||||||
conv_path = Path(dangerzone.conversion.__file__).parent
|
|
||||||
for root, _, files in os.walk(_conv_path):
|
for root, _, files in os.walk(_conv_path):
|
||||||
for file in files:
|
for file in files:
|
||||||
if file.endswith(".py"):
|
if file.endswith(".py"):
|
||||||
|
|
|
@ -33,9 +33,7 @@ class DangerzoneCore(object):
|
||||||
|
|
||||||
# Load settings
|
# Load settings
|
||||||
self.settings = Settings(self)
|
self.settings = Settings(self)
|
||||||
|
|
||||||
self.documents: List[Document] = []
|
self.documents: List[Document] = []
|
||||||
|
|
||||||
self.isolation_provider = isolation_provider
|
self.isolation_provider = isolation_provider
|
||||||
|
|
||||||
def add_document_from_filename(
|
def add_document_from_filename(
|
||||||
|
|
|
@ -86,7 +86,7 @@ def test_no_update(
|
||||||
|
|
||||||
menu_actions_before = window.hamburger_button.menu().actions()
|
menu_actions_before = window.hamburger_button.menu().actions()
|
||||||
|
|
||||||
with qtbot.waitSignal(updater.finished) as blocker:
|
with qtbot.waitSignal(updater.finished):
|
||||||
updater.start()
|
updater.start()
|
||||||
|
|
||||||
# Check that the callback function gets an empty report.
|
# Check that the callback function gets an empty report.
|
||||||
|
@ -126,7 +126,7 @@ def test_update_detected(
|
||||||
|
|
||||||
menu_actions_before = window.hamburger_button.menu().actions()
|
menu_actions_before = window.hamburger_button.menu().actions()
|
||||||
|
|
||||||
with qtbot.waitSignal(updater.finished) as blocker:
|
with qtbot.waitSignal(updater.finished):
|
||||||
updater.start()
|
updater.start()
|
||||||
|
|
||||||
menu_actions_after = window.hamburger_button.menu().actions()
|
menu_actions_after = window.hamburger_button.menu().actions()
|
||||||
|
@ -190,7 +190,7 @@ def test_update_detected(
|
||||||
|
|
||||||
# Collapse the "What's New" section and ensure that the dialog's height
|
# Collapse the "What's New" section and ensure that the dialog's height
|
||||||
# increases.
|
# increases.
|
||||||
with qtbot.waitSignal(collapsible_box.toggle_animation.finished) as blocker:
|
with qtbot.waitSignal(collapsible_box.toggle_animation.finished):
|
||||||
collapsible_box.toggle_button.click()
|
collapsible_box.toggle_button.click()
|
||||||
|
|
||||||
assert dialog.sizeHint().height() > height_initial
|
assert dialog.sizeHint().height() > height_initial
|
||||||
|
@ -198,7 +198,7 @@ def test_update_detected(
|
||||||
|
|
||||||
# Uncollapse the "What's New" section, and ensure that the dialog's height gets
|
# Uncollapse the "What's New" section, and ensure that the dialog's height gets
|
||||||
# back to the original value.
|
# back to the original value.
|
||||||
with qtbot.waitSignal(collapsible_box.toggle_animation.finished) as blocker:
|
with qtbot.waitSignal(collapsible_box.toggle_animation.finished):
|
||||||
collapsible_box.toggle_button.click()
|
collapsible_box.toggle_button.click()
|
||||||
|
|
||||||
assert dialog.sizeHint().height() == height_initial
|
assert dialog.sizeHint().height() == height_initial
|
||||||
|
@ -236,7 +236,7 @@ def test_update_error(
|
||||||
|
|
||||||
menu_actions_before = window.hamburger_button.menu().actions()
|
menu_actions_before = window.hamburger_button.menu().actions()
|
||||||
|
|
||||||
with qtbot.waitSignal(updater.finished) as blocker:
|
with qtbot.waitSignal(updater.finished):
|
||||||
updater.start()
|
updater.start()
|
||||||
|
|
||||||
menu_actions_after = window.hamburger_button.menu().actions()
|
menu_actions_after = window.hamburger_button.menu().actions()
|
||||||
|
@ -262,7 +262,7 @@ def test_update_error(
|
||||||
|
|
||||||
# Test 2 - Check that the second error does not notify the user either.
|
# Test 2 - Check that the second error does not notify the user either.
|
||||||
updater.dangerzone.settings.set("updater_last_check", 0)
|
updater.dangerzone.settings.set("updater_last_check", 0)
|
||||||
with qtbot.waitSignal(updater.finished) as blocker:
|
with qtbot.waitSignal(updater.finished):
|
||||||
updater.start()
|
updater.start()
|
||||||
|
|
||||||
assert load_svg_spy.call_count == 0
|
assert load_svg_spy.call_count == 0
|
||||||
|
@ -279,7 +279,7 @@ def test_update_error(
|
||||||
|
|
||||||
# Test 3 - Check that a third error shows a new menu entry.
|
# Test 3 - Check that a third error shows a new menu entry.
|
||||||
updater.dangerzone.settings.set("updater_last_check", 0)
|
updater.dangerzone.settings.set("updater_last_check", 0)
|
||||||
with qtbot.waitSignal(updater.finished) as blocker:
|
with qtbot.waitSignal(updater.finished):
|
||||||
updater.start()
|
updater.start()
|
||||||
|
|
||||||
menu_actions_after = window.hamburger_button.menu().actions()
|
menu_actions_after = window.hamburger_button.menu().actions()
|
||||||
|
|
|
@ -85,7 +85,7 @@ class TestQubes(IsolationProviderTest):
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
shell=True,
|
shell=True,
|
||||||
)
|
)
|
||||||
with pytest.raises(errors.ConverterProcException) as e:
|
with pytest.raises(errors.ConverterProcException):
|
||||||
doc = Document(sample_doc)
|
doc = Document(sample_doc)
|
||||||
provider.doc_to_pixels(doc, tmpdir, proc)
|
provider.doc_to_pixels(doc, tmpdir, proc)
|
||||||
assert provider.get_proc_exception(proc) == errors.QubesQrexecFailed
|
assert provider.get_proc_exception(proc) == errors.QubesQrexecFailed
|
||||||
|
|
|
@ -306,8 +306,6 @@ class TestCliConversion(TestCliBasic):
|
||||||
result.assert_failure()
|
result.assert_failure()
|
||||||
|
|
||||||
def test_archive(self, tmp_path: Path, sample_pdf: str) -> None:
|
def test_archive(self, tmp_path: Path, sample_pdf: str) -> None:
|
||||||
test_string = "original file"
|
|
||||||
|
|
||||||
original_doc_path = str(tmp_path / "doc.pdf")
|
original_doc_path = str(tmp_path / "doc.pdf")
|
||||||
safe_doc_path = str(tmp_path / f"doc{SAFE_EXTENSION}")
|
safe_doc_path = str(tmp_path / f"doc{SAFE_EXTENSION}")
|
||||||
archived_doc_path = str(tmp_path / ARCHIVE_SUBDIR / "doc.pdf")
|
archived_doc_path = str(tmp_path / ARCHIVE_SUBDIR / "doc.pdf")
|
||||||
|
@ -322,7 +320,7 @@ class TestCliConversion(TestCliBasic):
|
||||||
assert os.path.exists(safe_doc_path)
|
assert os.path.exists(safe_doc_path)
|
||||||
|
|
||||||
def test_dummy_conversion(self, tmp_path: Path, sample_pdf: str) -> None:
|
def test_dummy_conversion(self, tmp_path: Path, sample_pdf: str) -> None:
|
||||||
result = self.run_cli([sample_pdf, "--unsafe-dummy-conversion"])
|
self.run_cli([sample_pdf, "--unsafe-dummy-conversion"])
|
||||||
|
|
||||||
def test_dummy_conversion_bulk(self, tmp_path: Path, sample_pdf: str) -> None:
|
def test_dummy_conversion_bulk(self, tmp_path: Path, sample_pdf: str) -> None:
|
||||||
filenames = ["1.pdf", "2.pdf", "3.pdf"]
|
filenames = ["1.pdf", "2.pdf", "3.pdf"]
|
||||||
|
|
|
@ -29,12 +29,12 @@ def test_input_file_none() -> None:
|
||||||
Attempts to read a document's filename when no doc has been set
|
Attempts to read a document's filename when no doc has been set
|
||||||
"""
|
"""
|
||||||
d = Document()
|
d = Document()
|
||||||
with pytest.raises(errors.NotSetInputFilenameException) as e:
|
with pytest.raises(errors.NotSetInputFilenameException):
|
||||||
d.input_filename
|
d.input_filename
|
||||||
|
|
||||||
|
|
||||||
def test_input_file_non_existing() -> None:
|
def test_input_file_non_existing() -> None:
|
||||||
with pytest.raises(errors.InputFileNotFoundException) as e:
|
with pytest.raises(errors.InputFileNotFoundException):
|
||||||
Document("non-existing-file.pdf")
|
Document("non-existing-file.pdf")
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ def test_input_file_non_existing() -> None:
|
||||||
# https://stackoverflow.com/questions/72528318/what-file-permissions-make-a-file-unreadable-by-owner-in-windows
|
# https://stackoverflow.com/questions/72528318/what-file-permissions-make-a-file-unreadable-by-owner-in-windows
|
||||||
@pytest.mark.skipif(platform.system() == "Windows", reason="Unix-specific")
|
@pytest.mark.skipif(platform.system() == "Windows", reason="Unix-specific")
|
||||||
def test_input_file_unreadable(unreadable_pdf: str) -> None:
|
def test_input_file_unreadable(unreadable_pdf: str) -> None:
|
||||||
with pytest.raises(errors.InputFileNotReadableException) as e:
|
with pytest.raises(errors.InputFileNotReadableException):
|
||||||
Document(unreadable_pdf)
|
Document(unreadable_pdf)
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,8 +52,8 @@ def test_output_file_unwriteable_dir(sample_pdf: str, tmp_path: Path) -> None:
|
||||||
# make parent dir unwriteable
|
# make parent dir unwriteable
|
||||||
sample_pdf_safe = str(tmp_path / "document-safe.pdf")
|
sample_pdf_safe = str(tmp_path / "document-safe.pdf")
|
||||||
os.chmod(tmp_path, 0o400)
|
os.chmod(tmp_path, 0o400)
|
||||||
with pytest.raises(errors.UnwriteableOutputDirException) as e:
|
with pytest.raises(errors.UnwriteableOutputDirException):
|
||||||
d = Document(sample_pdf, sample_pdf_safe)
|
Document(sample_pdf, sample_pdf_safe)
|
||||||
|
|
||||||
|
|
||||||
def test_output(tmp_path: Path) -> None:
|
def test_output(tmp_path: Path) -> None:
|
||||||
|
@ -67,7 +67,7 @@ def test_output_file_none() -> None:
|
||||||
Attempts to read a document's filename when no doc has been set
|
Attempts to read a document's filename when no doc has been set
|
||||||
"""
|
"""
|
||||||
d = Document()
|
d = Document()
|
||||||
with pytest.raises(errors.NotSetOutputFilenameException) as e:
|
with pytest.raises(errors.NotSetOutputFilenameException):
|
||||||
d.output_filename
|
d.output_filename
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ def test_output_file_not_pdf(tmp_path: Path) -> None:
|
||||||
docx_file = str(tmp_path / "document.docx")
|
docx_file = str(tmp_path / "document.docx")
|
||||||
d = Document()
|
d = Document()
|
||||||
|
|
||||||
with pytest.raises(errors.NonPDFOutputFileException) as e:
|
with pytest.raises(errors.NonPDFOutputFileException):
|
||||||
d.output_filename = docx_file
|
d.output_filename = docx_file
|
||||||
|
|
||||||
assert not os.path.exists(docx_file)
|
assert not os.path.exists(docx_file)
|
||||||
|
@ -90,7 +90,7 @@ def test_archive_unwriteable_dir(tmp_path: Path) -> None:
|
||||||
# make archive directory unreadable
|
# make archive directory unreadable
|
||||||
os.chmod(tmp_path, 0o400)
|
os.chmod(tmp_path, 0o400)
|
||||||
|
|
||||||
with pytest.raises(errors.UnwriteableArchiveDirException) as e:
|
with pytest.raises(errors.UnwriteableArchiveDirException):
|
||||||
d.validate_default_archive_dir()
|
d.validate_default_archive_dir()
|
||||||
|
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ def test_set_output_filename_suffix(sample_pdf: str) -> None:
|
||||||
assert d.output_filename.endswith(safe_extension)
|
assert d.output_filename.endswith(safe_extension)
|
||||||
|
|
||||||
d.output_filename = "something_else.pdf"
|
d.output_filename = "something_else.pdf"
|
||||||
with pytest.raises(errors.SuffixNotApplicableException) as e:
|
with pytest.raises(errors.SuffixNotApplicableException):
|
||||||
d.suffix = "-new-trusted.pdf"
|
d.suffix = "-new-trusted.pdf"
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue