5from typing
import Dict, List, Optional, Tuple
23 engineref_list: List[EngineRef], preferences: Preferences
24) -> Tuple[List[EngineRef], List[EngineRef], List[EngineRef]]:
25 """Validate query_engines according to the preferences
28 List[EngineRef]: list of existing engines with a validated token
29 List[EngineRef]: list of unknown engine
30 List[EngineRef]: list of engine with invalid token according to the preferences
35 for engineref
in engineref_list:
36 if engineref.name
not in engines:
37 unknown.append(engineref)
40 engine = engines[engineref.name]
41 if not preferences.validate_token(engine):
42 no_token.append(engineref)
45 valid.append(engineref)
46 return valid, unknown, no_token
56def parse_lang(preferences: Preferences, form: Dict[str, str], raw_text_query: RawTextQuery) -> str:
57 if is_locked(
'language'):
58 return preferences.get_value(
'language')
62 if len(raw_text_query.languages):
63 query_lang = raw_text_query.languages[-1]
64 elif 'language' in form:
65 query_lang = form.get(
'language')
67 query_lang = preferences.get_value(
'language')
70 if not VALID_LANGUAGE_CODE.match(query_lang)
and query_lang !=
'auto':
77 if is_locked(
'safesearch'):
78 return preferences.get_value(
'safesearch')
80 if 'safesearch' in form:
81 query_safesearch = form.get(
'safesearch')
83 if not query_safesearch.isdigit():
85 query_safesearch = int(query_safesearch)
87 query_safesearch = preferences.get_value(
'safesearch')
90 if query_safesearch < 0
or query_safesearch > 2:
93 return query_safesearch
119 if name ==
'categories':
120 query_categories.extend(categ
for categ
in map(str.strip, value.split(
','))
if categ
in categories)
121 elif name.startswith(
'category_'):
125 if category
not in categories:
130 query_categories.append(category)
131 elif category
in query_categories:
133 query_categories.remove(category)
137 selected_categories = []
139 if not is_locked(
'categories')
and form
is not None:
140 for name, value
in form.items():
146 if not selected_categories:
147 cookie_categories = preferences.get_value(
'categories')
148 for ccateg
in cookie_categories:
149 selected_categories.append(ccateg)
153 if not selected_categories:
154 selected_categories = [
'general']
156 return selected_categories
173def parse_generic(preferences: Preferences, form: Dict[str, str], disabled_engines: List[str]) -> List[EngineRef]:
174 query_engineref_list = []
175 query_categories = []
178 explicit_engine_list =
False
179 if not is_locked(
'categories'):
181 for pd_name, pd
in form.items():
182 if pd_name ==
'engines':
184 EngineRef(engine_name, engines[engine_name].categories[0])
185 for engine_name
in map(str.strip, pd.split(
','))
186 if engine_name
in engines
189 query_engineref_list.extend(pd_engines)
190 explicit_engine_list =
True
194 if explicit_engine_list:
201 if not query_categories:
204 query_categories = get_selected_categories(preferences,
None)
210 return query_engineref_list
223 preferences: Preferences, form: Dict[str, str]
224) -> Tuple[SearchQuery, RawTextQuery, List[EngineRef], List[EngineRef], str]:
225 """Assemble data from preferences and request.form (from the HTML form) needed
228 The returned tuple consits of:
230 1. instance of :py:obj:`searx.search.SearchQuery`
231 2. instance of :py:obj:`searx.query.RawTextQuery`
232 3. list of :py:obj:`searx.search.EngineRef` instances
233 4. string with the *selected locale* of the query
235 About language/locale: if the client selects the alias ``auto`` the
236 ``SearchQuery`` object is build up by the :py:obj:`detected language
237 <searx.utils.detect_language>`. If language recognition does not have a
238 match the language preferred by the :py:obj:`Preferences.client` is used.
239 If client does not have a preference, the default ``all`` is used.
241 The *selected locale* in the tuple always represents the selected
242 language/locale and might differ from the language recognition.
246 if not form.get(
'q'):
250 disabled_engines = preferences.engines.get_disabled()
254 raw_text_query =
RawTextQuery(form[
'q'], disabled_engines)
257 query = raw_text_query.getQuery()
262 external_bang = raw_text_query.external_bang
263 redirect_to_first_result = raw_text_query.redirect_to_first_result
266 query_lang = parse_lang(preferences, form, raw_text_query)
267 selected_locale = query_lang
269 if query_lang ==
'auto':
270 query_lang = detect_language(query, threshold=0.8, only_search_languages=
True)
271 query_lang = query_lang
or preferences.client.locale_tag
or 'all'
273 if not is_locked(
'categories')
and raw_text_query.specific:
276 query_engineref_list = raw_text_query.enginerefs
280 query_engineref_list =
parse_generic(preferences, form, disabled_engines)
284 query_engineref_list, preferences
290 query_engineref_list,
296 external_bang=external_bang,
297 engine_data=engine_data,
298 redirect_to_first_result=redirect_to_first_result,
301 query_engineref_list_unknown,
302 query_engineref_list_notoken,
Tuple[SearchQuery, RawTextQuery, List[EngineRef], List[EngineRef], str] get_search_query_from_webapp(Preferences preferences, Dict[str, str] form)