mirror of
https://github.com/searxng/searxng.git
synced 2024-11-17 18:00:12 +01:00
[mod] add typing and __slots__
This commit is contained in:
parent
678699beaf
commit
485a502b88
@ -15,6 +15,7 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
|
||||
(C) 2013- by Adam Tauber, <asciimoo@gmail.com>
|
||||
'''
|
||||
|
||||
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
|
||||
|
@ -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', '')
|
||||
|
@ -3,6 +3,7 @@
|
||||
from searx.testing import SearxTestCase
|
||||
from searx.search import SearchQuery, EngineRef
|
||||
import searx.search
|
||||
import searx.engines
|
||||
|
||||
|
||||
SAFESEARCH = 0
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user