|
1 | 1 | import logging |
| 2 | +from typing import Union |
2 | 3 | from src.utils.common import make_request, extract_json_from_response |
3 | 4 | from src.utils.log_setup import logger |
4 | 5 |
|
@@ -199,21 +200,34 @@ def group_by_download_id(self, queue_items): |
199 | 200 |
|
200 | 201 | return grouped_dict |
201 | 202 |
|
202 | | - @staticmethod |
203 | | - def filter_queue_by_status(queue, statuses: list[str]) -> list[dict]: |
204 | | - """Filter queue items that match any of the given statuses.""" |
205 | | - return [item for item in queue if item.get("status") in statuses] |
206 | 203 |
|
207 | 204 | @staticmethod |
208 | | - def filter_queue_by_status_and_error_message( |
209 | | - queue, conditions: list[tuple[str, str]] |
| 205 | + def filter_queue( |
| 206 | + queue: list[dict], |
| 207 | + conditions: list[Union[str, tuple[str, str]]], |
210 | 208 | ) -> list[dict]: |
211 | | - """Filter queue items that match any given (status, errorMessage) pair.""" |
212 | | - queue_items = [] |
| 209 | + """ |
| 210 | + Filter queue items by status or (status, errorMessage) pairs. |
| 211 | +
|
| 212 | + - If an entry in `conditions` is a string, only status is checked. |
| 213 | + - If it's a (status, errorMessage) tuple, both must match. |
| 214 | + """ |
| 215 | + filtered_items = [] |
213 | 216 | for item in queue: |
214 | | - if "errorMessage" in item and "status" in item: |
215 | | - for status, message in conditions: |
216 | | - if item["status"] == status and item["errorMessage"] == message: |
217 | | - queue_items.append(item) |
218 | | - break # Stop checking other conditions once one matches |
219 | | - return queue_items |
| 217 | + item_status = item.get("status") |
| 218 | + item_error = item.get("errorMessage") |
| 219 | + |
| 220 | + for condition in conditions: |
| 221 | + if isinstance(condition, str): |
| 222 | + if item_status == condition: |
| 223 | + filtered_items.append(item) |
| 224 | + break |
| 225 | + elif ( |
| 226 | + isinstance(condition, tuple) |
| 227 | + and len(condition) == 2 |
| 228 | + and item_status == condition[0] |
| 229 | + and item_error == condition[1] |
| 230 | + ): |
| 231 | + filtered_items.append(item) |
| 232 | + break |
| 233 | + return filtered_items |
0 commit comments