|
6 | 6 | import subprocess
|
7 | 7 | import tempfile
|
8 | 8 | from io import BytesIO
|
9 |
| -from typing import Dict, Optional, Tuple |
| 9 | +from typing import Dict, List, Optional, Tuple |
10 | 10 |
|
11 | 11 | import magic
|
12 | 12 | from django.conf import settings
|
@@ -88,14 +88,14 @@ def parse_alerts(alerts: str) -> Tuple[str, str]:
|
88 | 88 | return alerts_text, printer_class
|
89 | 89 |
|
90 | 90 |
|
91 |
| -def get_printers() -> Dict[str, str]: |
| 91 | +def get_printers() -> Dict[str, List[str]]: |
92 | 92 | """Returns a dictionary mapping name:description for available printers.
|
93 | 93 |
|
94 | 94 | This requires that a CUPS client be configured on the server.
|
95 | 95 | Otherwise, this returns an empty dictionary.
|
96 | 96 |
|
97 | 97 | Returns:
|
98 |
| - A dictionary mapping name:description for available printers. |
| 98 | + A dictionary mapping name:[description,alerts] for available printers. |
99 | 99 | """
|
100 | 100 |
|
101 | 101 | key = "printing:printers"
|
@@ -130,17 +130,20 @@ def get_printers() -> Dict[str, str]:
|
130 | 130 | # extended description we know which printer it's referring to.
|
131 | 131 | last_name = name
|
132 | 132 | elif last_name is not None:
|
| 133 | + if line.strip() == "The printer is not responding.": |
| 134 | + printers[last_name].append("not-responding") |
133 | 135 | description_match = DESCRIPTION_LINE_RE.match(line)
|
134 | 136 | if description_match is not None:
|
135 | 137 | # Pull out the description
|
136 | 138 | description = description_match.group(1)
|
137 | 139 | # And make sure we don't set an empty description
|
138 | 140 | if description:
|
139 |
| - printers[last_name] = [description] |
| 141 | + printers[last_name][0] = description |
140 | 142 | alerts_match = ALERTS_LINE_RE.match(line)
|
141 | 143 | if alerts_match is not None:
|
142 | 144 | alerts = alerts_match.group(1)
|
143 |
| - printers[last_name].append(alerts) |
| 145 | + if len(printers[last_name]) == 1: # If already marked as not responding, ignore alerts |
| 146 | + printers[last_name].append(alerts) |
144 | 147 | last_name = None
|
145 | 148 |
|
146 | 149 | cache.set(key, printers, timeout=settings.CACHE_AGE["printers_list"])
|
|
0 commit comments