1
+ from __future__ import annotations
2
+
1
3
import logging
2
4
import math
3
5
import os
4
6
import re
5
7
import subprocess
6
8
import tempfile
7
9
from io import BytesIO
8
- from typing import Dict , Optional
9
10
10
11
import magic
11
12
from sentry_sdk import add_breadcrumb , capture_exception
@@ -32,14 +33,15 @@ class InvalidInputPrintingError(Exception):
32
33
"""An error occurred while printing, but it was due to invalid input from the user and is not worthy of a ``CRITICAL`` log message."""
33
34
34
35
35
- def get_printers () -> Dict [str , str ]:
36
+ def get_printers () -> dict [str , str ] | list :
36
37
"""Returns a dictionary mapping name:description for available printers.
37
38
38
39
This requires that a CUPS client be configured on the server.
39
40
Otherwise, this returns an empty dictionary.
40
41
41
42
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
43
45
"""
44
46
45
47
key = "printing:printers"
@@ -88,7 +90,7 @@ def get_printers() -> Dict[str, str]:
88
90
return printers
89
91
90
92
91
- def convert_soffice (tmpfile_name : str ) -> Optional [ str ] :
93
+ def convert_soffice (tmpfile_name : str ) -> str | None :
92
94
"""Converts a doc or docx to a PDF with soffice.
93
95
94
96
Args:
@@ -119,7 +121,7 @@ def convert_soffice(tmpfile_name: str) -> Optional[str]:
119
121
return None
120
122
121
123
122
- def convert_pdf (tmpfile_name : str , cmdname : str = "ps2pdf" ) -> Optional [ str ] :
124
+ def convert_pdf (tmpfile_name : str , cmdname : str = "ps2pdf" ) -> str | None :
123
125
new_name = "{}.pdf" .format (tmpfile_name )
124
126
try :
125
127
output = subprocess .check_output ([cmdname , tmpfile_name , new_name ], stderr = subprocess .STDOUT , universal_newlines = True )
@@ -181,7 +183,7 @@ def get_mimetype(tmpfile_name: str) -> str:
181
183
return mimetype
182
184
183
185
184
- def convert_file (tmpfile_name : str , orig_fname : str ) -> Optional [ str ] :
186
+ def convert_file (tmpfile_name : str , orig_fname : str ) -> str | None :
185
187
detected = get_mimetype (tmpfile_name )
186
188
187
189
add_breadcrumb (category = "printing" , message = "Detected file type {}" .format (detected ), level = "debug" )
@@ -213,7 +215,7 @@ def convert_file(tmpfile_name: str, orig_fname: str) -> Optional[str]:
213
215
raise InvalidInputPrintingError ("Invalid file type {}" .format (detected ))
214
216
215
217
216
- def check_page_range (page_range : str , max_pages : int ) -> Optional [ int ] :
218
+ def check_page_range (page_range : str , max_pages : int ) -> int | None :
217
219
"""Returns the number of pages included in the range, or None if it is an invalid range.
218
220
219
221
Args:
0 commit comments