1+ from __future__ import annotations
2+
13import logging
24import math
35import os
46import re
57import subprocess
68import tempfile
79from io import BytesIO
8- from typing import Dict , Optional
910
1011import magic
1112from sentry_sdk import add_breadcrumb , capture_exception
@@ -32,14 +33,15 @@ class InvalidInputPrintingError(Exception):
3233 """An error occurred while printing, but it was due to invalid input from the user and is not worthy of a ``CRITICAL`` log message."""
3334
3435
35- def get_printers () -> Dict [str , str ]:
36+ def get_printers () -> dict [str , str ] | list :
3637 """Returns a dictionary mapping name:description for available printers.
3738
3839 This requires that a CUPS client be configured on the server.
3940 Otherwise, this returns an empty dictionary.
4041
4142 Returns:
42- A dictionary mapping name:description for available printers.
43+ A dictionary mapping name:description for available printers, or
44+ an empty list if cups isn't installed or lpstat fails
4345 """
4446
4547 key = "printing:printers"
@@ -86,7 +88,7 @@ def get_printers() -> Dict[str, str]:
8688 return printers
8789
8890
89- def convert_soffice (tmpfile_name : str ) -> Optional [ str ] :
91+ def convert_soffice (tmpfile_name : str ) -> str | None :
9092 """Converts a doc or docx to a PDF with soffice.
9193
9294 Args:
@@ -117,7 +119,7 @@ def convert_soffice(tmpfile_name: str) -> Optional[str]:
117119 return None
118120
119121
120- def convert_pdf (tmpfile_name : str , cmdname : str = "ps2pdf" ) -> Optional [ str ] :
122+ def convert_pdf (tmpfile_name : str , cmdname : str = "ps2pdf" ) -> str | None :
121123 new_name = f"{ tmpfile_name } .pdf"
122124 try :
123125 output = subprocess .check_output ([cmdname , tmpfile_name , new_name ], stderr = subprocess .STDOUT , universal_newlines = True )
@@ -179,7 +181,7 @@ def get_mimetype(tmpfile_name: str) -> str:
179181 return mimetype
180182
181183
182- def convert_file (tmpfile_name : str , orig_fname : str ) -> Optional [ str ] :
184+ def convert_file (tmpfile_name : str , orig_fname : str ) -> str | None :
183185 detected = get_mimetype (tmpfile_name )
184186
185187 add_breadcrumb (category = "printing" , message = f"Detected file type { detected } " , level = "debug" )
@@ -211,7 +213,7 @@ def convert_file(tmpfile_name: str, orig_fname: str) -> Optional[str]:
211213 raise InvalidInputPrintingError (f"Invalid file type { detected } " )
212214
213215
214- def check_page_range (page_range : str , max_pages : int ) -> Optional [ int ] :
216+ def check_page_range (page_range : str , max_pages : int ) -> int | None :
215217 """Returns the number of pages included in the range, or None if it is an invalid range.
216218
217219 Args:
0 commit comments