4from __future__
import annotations
5from abc
import abstractmethod, ABC
8from searx
import settings
10from searx.engines import categories, engines, engine_shortcuts
18 __slots__ =
"raw_text_query",
"enable_autocomplete"
23 """Check if raw_value can be parsed"""
25 def __init__(self, raw_text_query, enable_autocomplete):
31 """Try to parse raw_value: set the self.raw_text_query properties
33 return True if raw_value has been parsed
35 self.raw_text_query.autocomplete_list is also modified
36 if self.enable_autocomplete is True
40 if value
not in self.raw_text_query.autocomplete_list:
41 self.raw_text_query.autocomplete_list.append(value)
47 return raw_value[0] ==
'<'
51 found = self.
_parse(value)
if len(value) > 0
else False
57 if not value.isdigit():
59 raw_timeout_limit = int(value)
60 if raw_timeout_limit < 100:
69 for suggestion
in [
'<3',
'<850']:
76 return raw_value[0] ==
':'
79 value = raw_value[1:].lower().replace(
'_',
'-')
80 found = self.
_parse(value)
if len(value) > 0
else False
89 for lc
in sxng_locales:
90 lang_id, lang_name, country, english_name, _flag = map(str.lower, lc)
96 value == lang_id
or value == lang_name
or value == english_name
or value.replace(
'-',
' ') == country
99 lang_parts = lang_id.split(
'-')
100 if len(lang_parts) == 2:
101 self.
raw_text_query.languages.append(lang_parts[0] +
'-' + lang_parts[1].upper())
109 if VALID_LANGUAGE_CODE.match(value)
or value ==
'auto':
110 lang_parts = value.split(
'-')
111 if len(lang_parts) > 1:
112 value = lang_parts[0].lower() +
'-' + lang_parts[1].upper()
122 if len(settings[
'search'][
'languages']) < 10:
123 for lang
in settings[
'search'][
'languages']:
126 for lang
in [
":en",
":en_us",
":english",
":united_kingdom"]:
130 for lc
in sxng_locales:
131 if lc[0]
not in settings[
'search'][
'languages']:
133 lang_id, lang_name, country, english_name, _flag = map(str.lower, lc)
136 if lang_id.startswith(value):
143 if lang_name.startswith(value)
or english_name.startswith(value):
148 if country.startswith(value.replace(
'-',
' ')):
155 return raw_value.startswith(
'!!')
and len(raw_value) > 2
158 value = raw_value[2:]
159 found, bang_ac_list = self.
_parse(value)
if len(value) > 0
else (
False, [])
166 bang_definition, bang_ac_list = get_bang_definition_and_autocomplete(value)
167 if bang_definition
is not None:
170 return found, bang_ac_list
174 bang_ac_list = [
'g',
'ddg',
'bing']
175 for external_bang
in bang_ac_list:
183 return raw_value[0] ==
'!' and (len(raw_value) < 2
or raw_value[1] !=
'!')
186 value = raw_value[1:].replace(
'-',
' ').replace(
'_',
' ')
187 found = self.
_parse(value)
if len(value) > 0
else False
188 if found
and raw_value[0] ==
'!':
196 if value
in engine_shortcuts:
197 value = engine_shortcuts[value]
205 if value
in categories:
210 for engine
in categories[value]
211 if (engine.name, value)
not in self.
raw_text_query.disabled_engines
220 for suggestion
in [
'images',
'wikipedia',
'osm']:
221 if suggestion
not in self.
raw_text_query.disabled_engines
or suggestion
in categories:
226 for category
in categories:
227 if category.startswith(value):
231 for engine
in engines:
232 if engine.startswith(value):
236 for engine_shortcut
in engine_shortcuts:
237 if engine_shortcut.startswith(value):
244 return raw_value ==
'!!'
252 """parse raw text query (the value from the html input)"""
262 def __init__(self, query: str, disabled_engines: list):
263 assert isinstance(query, str)
283 parse self.query, if tags are set, which
284 change the search engine or search-language
288 raw_query_parts = re.split(
r'(\s+)', self.
query)
290 last_index_location =
None
291 autocomplete_index = len(raw_query_parts) - 1
293 for i, query_part
in enumerate(raw_query_parts):
295 if query_part.isspace()
or query_part ==
'':
300 for parser_class
in RawTextQuery.PARSER_CLASSES:
301 if parser_class.check(query_part):
302 special_part = parser_class(self, i == autocomplete_index)(query_part)
307 qlist.append(query_part)
308 last_index_location = (qlist, len(qlist) - 1)
314 qlist[position] = text
329 get full query including whitespaces
338 f
"<{self.__class__.__name__} "
339 + f
"query={self.query!r} "
340 + f
"disabled_engines={self.disabled_engines!r}\n "
341 + f
"languages={self.languages!r} "
342 + f
"timeout_limit={self.timeout_limit!r} "
343 + f
"external_bang={self.external_bang!r} "
344 + f
"specific={self.specific!r} "
345 + f
"enginerefs={self.enginerefs!r}\n "
346 + f
"autocomplete_list={self.autocomplete_list!r}\n "
347 + f
"query_parts={self.query_parts!r}\n "
348 + f
"user_query_parts={self.user_query_parts!r} >\n"
349 + f
"redirect_to_first_result={self.redirect_to_first_result!r}"
_autocomplete(self, first_char, value)
__call__(self, raw_value)
_autocomplete(self, bang_ac_list)
__call__(self, raw_value)
__call__(self, raw_value)
_autocomplete(self, value)
__call__(self, raw_value)
_add_autocomplete(self, value)
__call__(self, raw_value)
__init__(self, raw_text_query, enable_autocomplete)
tuple autocomplete_location
__init__(self, str query, list disabled_engines)
bool redirect_to_first_result
get_autocomplete_full_query(self, text)
__call__(self, raw_value)