From 193b0efd12a78172fec1fef03bfc2f7929c705a3 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Mon, 17 Jan 2022 11:11:39 +0100 Subject: [PATCH] [typing] add results.UnresponsiveEngine --- searx/results.py | 14 ++++++++++---- searx/webapp.py | 14 +++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/searx/results.py b/searx/results.py index 5e1b46ed5..1ac6a5ab5 100644 --- a/searx/results.py +++ b/searx/results.py @@ -2,7 +2,7 @@ import re from collections import defaultdict from operator import itemgetter from threading import RLock -from typing import List, NamedTuple +from typing import List, NamedTuple, Set from urllib.parse import urlparse, unquote from searx import logger @@ -145,6 +145,12 @@ class Timing(NamedTuple): load: float +class UnresponsiveEngine(NamedTuple): + engine: str + error_type: str + suspended: bool + + class ResultContainer: """docstring for ResultContainer""" @@ -176,7 +182,7 @@ class ResultContainer: self.engine_data = defaultdict(dict) self._closed = False self.paging = False - self.unresponsive_engines = set() + self.unresponsive_engines: Set[UnresponsiveEngine] = set() self.timings: List[Timing] = [] self.redirect_url = None self.on_result = lambda _: True @@ -409,9 +415,9 @@ class ResultContainer: return 0 return resultnum_sum / len(self._number_of_results) - def add_unresponsive_engine(self, engine_name, error_type, suspended=False): + def add_unresponsive_engine(self, engine_name: str, error_type: str, suspended: bool = False): if engines[engine_name].display_error_messages: - self.unresponsive_engines.add((engine_name, error_type, suspended)) + self.unresponsive_engines.add(UnresponsiveEngine(engine_name, error_type, suspended)) def add_timing(self, engine_name: str, engine_time: float, page_load_time: float): self.timings.append(Timing(engine_name, total=engine_time, load=page_load_time)) diff --git a/searx/webapp.py b/searx/webapp.py index bb6e74b25..196bfb0d2 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -15,7 +15,7 @@ from timeit import default_timer from html import escape from io import StringIO import typing -from typing import List, Dict +from typing import List, Dict, Iterable import urllib from urllib.parse import urlencode @@ -56,7 +56,7 @@ from searx import ( searx_debug, ) from searx.data import ENGINE_DESCRIPTIONS -from searx.results import Timing +from searx.results import Timing, UnresponsiveEngine from searx.settings_defaults import OUTPUT_FORMATS from searx.settings_loader import get_default_settings_path from searx.exceptions import SearxParameterException @@ -844,21 +844,21 @@ def search(): ) -def __get_translated_errors(unresponsive_engines): +def __get_translated_errors(unresponsive_engines: Iterable[UnresponsiveEngine]): translated_errors = [] # make a copy unresponsive_engines to avoid "RuntimeError: Set changed size # during iteration" it happens when an engine modifies the ResultContainer # after the search_multiple_requests method has stopped waiting - for unresponsive_engine in list(unresponsive_engines): - error_user_text = exception_classname_to_text.get(unresponsive_engine[1]) + for unresponsive_engine in unresponsive_engines: + error_user_text = exception_classname_to_text.get(unresponsive_engine.error_type) if not error_user_text: error_user_text = exception_classname_to_text[None] error_msg = gettext(error_user_text) - if unresponsive_engine[2]: + if unresponsive_engine.suspended: error_msg = gettext('Suspended') + ': ' + error_msg - translated_errors.append((unresponsive_engine[0], error_msg)) + translated_errors.append((unresponsive_engine.engine, error_msg)) return sorted(translated_errors, key=lambda e: e[0])