Skip to content

Commit

Permalink
Handle printer discovery failure through exceptions
Browse files Browse the repository at this point in the history
Incapsulate printer search and possible failure in the respective
printer search methods.
  • Loading branch information
deeplow committed Jan 17, 2025
1 parent 3e72356 commit 66eb231
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
9 changes: 9 additions & 0 deletions export/securedrop_export/print/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from securedrop_export.exceptions import ExportException

from .status import Status


class PrinterNotFoundException(ExportException):
def __init__(self, sderror=None):
super().__init__()
self.sdstatus = Status.ERROR_PRINTER_NOT_FOUND
32 changes: 19 additions & 13 deletions export/securedrop_export/print/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from securedrop_export.directory import safe_mkdir
from securedrop_export.exceptions import ExportException

from .exceptions import PrinterNotFoundException
from .print_dialog import open_print_dialog
from .status import Status

Expand Down Expand Up @@ -120,9 +121,9 @@ def _check_printer_setup(self) -> None:
legacy_printers = False
logger.info("Searching for printer")

printers = self._get_printers_ipp()
if not printers:
# look for legacy printers after no IPP ones are detected
try:
printers = self._get_printers_ipp()
except PrinterNotFoundException:
printers = self._get_printers_legacy()
legacy_printers = True

Expand All @@ -147,13 +148,18 @@ def _get_printers_legacy(self) -> list[str]:
raise ExportException(sdstatus=Status.ERROR_UNKNOWN)

discovered_printers = [x for x in output.decode("utf-8").split() if "usb://" in x]

supported_printers = [
p for p in discovered_printers if any(sub in p for sub in self.SUPPORTED_PRINTERS)
]
unsupported_printers = [p for p in discovered_printers if p not in supported_printers]

if not supported_printers:
logger.info(f"{discovered_printers} are unsupported printers")
raise ExportException(sdstatus=Status.ERROR_PRINTER_NOT_SUPPORTED)
if unsupported_printers:
logger.info(f"{', '.join(unsupported_printers)} are unsupported printers")
raise ExportException(sdstatus=Status.ERROR_PRINTER_NOT_SUPPORTED)
else:
logger.info("No legacy (PDP) printers were found")
raise PrinterNotFoundException()

return supported_printers

Expand All @@ -163,14 +169,14 @@ def _get_printers_ipp(self) -> list[str]:
discovered_printers = subprocess.check_output(
["ippfind"], universal_newlines=True
).split()
except subprocess.CalledProcessError as e:
logger.error(e)
raise ExportException(sdstatus=Status.ERROR_UNKNOWN)

if discovered_printers:
logger.debug(f"Found IPP printers: {', '.join(discovered_printers)}")
else:
logger.debug("No IPP were found")
except subprocess.CalledProcessError as ex:
if ex.returncode == 1: # Did not find any match
logger.debug("No IPP printers were found")
raise PrinterNotFoundException()
else:
logger.error("'ippfind' command failed")
raise ExportException(sdstatus=Status.ERROR_PRINTER_URI)

return discovered_printers

Expand Down

0 comments on commit 66eb231

Please sign in to comment.