From 485a502b886bc7fb8a806ffc576b0eec99990a5c Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Thu, 10 Sep 2020 18:08:14 +0200 Subject: [PATCH] [mod] add typing and __slots__ --- searx/search.py | 26 ++++++++++++++++++++++---- searx/webadapter.py | 20 ++++++++++++-------- tests/unit/test_search.py | 1 + utils/standalone_searx.py | 1 + 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/searx/search.py b/searx/search.py index a99ca6b16..3dfdd7213 100644 --- a/searx/search.py +++ b/searx/search.py @@ -15,6 +15,7 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >. (C) 2013- by Adam Tauber, ''' +import typing import gc import threading from time import time @@ -49,7 +50,9 @@ else: class EngineRef: - def __init__(self, name, category, from_bang=False): + __slots__ = 'name', 'category', 'from_bang' + + def __init__(self, name: str, category: str, from_bang: bool=False): self.name = name self.category = category self.from_bang = from_bang @@ -61,8 +64,19 @@ class EngineRef: class SearchQuery: """container for all the search parameters (query, language, etc...)""" - def __init__(self, query, engineref_list, categories, lang, safesearch, pageno, time_range, - timeout_limit=None, external_bang=None): + __slots__ = 'query', 'engineref_list', 'categories', 'lang', 'safesearch', 'pageno', 'time_range',\ + 'timeout_limit', 'external_bang' + + def __init__(self, + query: str, + engineref_list: typing.List[EngineRef], + categories: typing.List[str], + lang: str, + safesearch: bool, + pageno: int, + time_range: typing.Optional[str], + timeout_limit: typing.Optional[float]=None, + external_bang: typing.Optional[str]=False): self.query = query self.engineref_list = engineref_list self.categories = categories @@ -274,6 +288,8 @@ def default_request_params(): class Search: """Search information container""" + __slots__ = "search_query", "result_container", "start_time", "actual_timeout" + def __init__(self, search_query): # init vars super().__init__() @@ -396,7 +412,7 @@ class Search: actual_timeout = min(query_timeout, max_request_timeout) logger.debug("actual_timeout={0} (default_timeout={1}, ?timeout_limit={2}, max_request_timeout={3})" - .format(self.actual_timeout, default_timeout, query_timeout, max_request_timeout)) + .format(actual_timeout, default_timeout, query_timeout, max_request_timeout)) return requests, actual_timeout @@ -428,6 +444,8 @@ class Search: class SearchWithPlugins(Search): """Similar to the Search class but call the plugins.""" + __slots__ = 'ordered_plugin_list', 'request' + def __init__(self, search_query, ordered_plugin_list, request): super().__init__(search_query) self.ordered_plugin_list = ordered_plugin_list diff --git a/searx/webadapter.py b/searx/webadapter.py index 982517eaa..1ec84bbdc 100644 --- a/searx/webadapter.py +++ b/searx/webadapter.py @@ -1,19 +1,22 @@ +import typing from searx.exceptions import SearxParameterException from searx.query import RawTextQuery, VALID_LANGUAGE_CODE from searx.engines import categories, engines from searx.search import SearchQuery, EngineRef +from searx.preferences import Preferences # remove duplicate queries. # FIXME: does not fix "!music !soundcloud", because the categories are 'none' and 'music' -def deduplicate_engineref_list(engineref_list): +def deduplicate_engineref_list(engineref_list: typing.List[EngineRef]) -> typing.List[EngineRef]: engineref_dict = {q.category + '|' + q.name: q for q in engineref_list} return engineref_dict.values() -def validate_engineref_list(engineref_list, preferences): +def validate_engineref_list(engineref_list: typing.List[EngineRef], preferences: Preferences): """ Validate query_engines according to the preferences + Returns: list of existing engines with a validated token list of unknown engine @@ -36,14 +39,14 @@ def validate_engineref_list(engineref_list, preferences): return valid, unknown, no_token -def parse_pageno(form): +def parse_pageno(form: typing.Dict[str, str]) -> int: pageno_param = form.get('pageno', '1') if not pageno_param.isdigit() or int(pageno_param) < 1: raise SearxParameterException('pageno', pageno_param) return int(pageno_param) -def parse_lang(raw_text_query, form, preferences): +def parse_lang(raw_text_query: RawTextQuery, form: typing.Dict[str, str], preferences: Preferences) -> str: # get language # set specific language if set on request, query or preferences # TODO support search with multible languages @@ -61,7 +64,7 @@ def parse_lang(raw_text_query, form, preferences): return query_lang -def parse_safesearch(form, preferences): +def parse_safesearch(form: typing.Dict[str, str], preferences: Preferences) -> int: if 'safesearch' in form: query_safesearch = form.get('safesearch') # first check safesearch @@ -78,7 +81,7 @@ def parse_safesearch(form, preferences): return query_safesearch -def parse_time_range(form): +def parse_time_range(form: typing.Dict[str, str]) -> str: query_time_range = form.get('time_range') # check time_range query_time_range = None if query_time_range in ('', 'None') else query_time_range @@ -87,7 +90,7 @@ def parse_time_range(form): return query_time_range -def parse_timeout(raw_text_query, form): +def parse_timeout(raw_text_query: RawTextQuery, form: typing.Dict[str, str]) -> typing.Optional[float]: query_timeout = raw_text_query.timeout_limit if query_timeout is None and 'timeout_limit' in form: raw_time_limit = form.get('timeout_limit') @@ -187,7 +190,8 @@ def parse_generic(form, preferences, disabled_engines): return query_engineref_list, query_categories -def get_search_query_from_webapp(preferences, form): +def get_search_query_from_webapp(preferences: Preferences, form: typing.Dict[str, str])\ + -> typing.Tuple[SearchQuery, RawTextQuery, typing.List[EngineRef], typing.List[EngineRef]]: # no text for the query ? if not form.get('q'): raise SearxParameterException('q', '') diff --git a/tests/unit/test_search.py b/tests/unit/test_search.py index bd302c741..36135913c 100644 --- a/tests/unit/test_search.py +++ b/tests/unit/test_search.py @@ -3,6 +3,7 @@ from searx.testing import SearxTestCase from searx.search import SearchQuery, EngineRef import searx.search +import searx.engines SAFESEARCH = 0 diff --git a/utils/standalone_searx.py b/utils/standalone_searx.py index d969c398d..3aab7a6cc 100755 --- a/utils/standalone_searx.py +++ b/utils/standalone_searx.py @@ -30,6 +30,7 @@ import codecs import searx.query import searx.search import searx.engines +import searx.webapdater import searx.preferences import searx.webadapter import argparse