.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.search.processors.abstract.EngineProcessor Class Reference
Inheritance diagram for searx.search.processors.abstract.EngineProcessor:
Collaboration diagram for searx.search.processors.abstract.EngineProcessor:

Public Member Functions

 __init__ (self, "Engine|types.ModuleType" engine)
 initialize (self, t.Callable[["EngineProcessor", bool], bool] callback)
bool init_engine (self)
 handle_exception (self, "ResultContainer" result_container, BaseException|str exception_or_message, bool suspend=False)
 extend_container (self, "ResultContainer" result_container, float start_time, "list[Result | LegacyResult]|None" search_results)
bool extend_container_if_suspended (self, "ResultContainer" result_container)
RequestParams|None get_params (self, "SearchQuery" search_query, str engine_category)
 search (self, str query, RequestParams params, "ResultContainer" result_container, float start_time, float timeout_limit)
 get_tests (self)
 get_default_tests (self)

Public Attributes

 engine = engine
logging.Logger logger = engines[engine.name].logger
SuspendedStatus suspended_status = SUSPENDED_STATUS.setdefault(key, SuspendedStatus())

Protected Member Functions

 _extend_container_basic (self, "ResultContainer" result_container, float start_time, "list[Result | LegacyResult]" search_results)

Detailed Description

Base classes used for all types of request processors.

Definition at line 111 of file abstract.py.

Constructor & Destructor Documentation

◆ __init__()

searx.search.processors.abstract.EngineProcessor.__init__ ( self,
"Engine|types.ModuleType" engine )

Definition at line 116 of file abstract.py.

116 def __init__(self, engine: "Engine|types.ModuleType"):
117 self.engine: "Engine" = engine # pyright: ignore[reportAttributeAccessIssue]
118 self.logger: logging.Logger = engines[engine.name].logger
119 key = get_network(self.engine.name)
120 key = id(key) if key else self.engine.name
121 self.suspended_status: SuspendedStatus = SUSPENDED_STATUS.setdefault(key, SuspendedStatus())
122

Member Function Documentation

◆ _extend_container_basic()

searx.search.processors.abstract.EngineProcessor._extend_container_basic ( self,
"ResultContainer" result_container,
float start_time,
"list[Result | LegacyResult]" search_results )
protected

Definition at line 193 of file abstract.py.

198 ):
199 # update result_container
200 result_container.extend(self.engine.name, search_results)
201 engine_time = default_timer() - start_time
202 page_load_time = get_time_for_thread()
203 result_container.add_timing(self.engine.name, engine_time, page_load_time)
204 # metrics
205 counter_inc('engine', self.engine.name, 'search', 'count', 'successful')
206 histogram_observe(engine_time, 'engine', self.engine.name, 'time', 'total')
207 if page_load_time is not None:
208 histogram_observe(page_load_time, 'engine', self.engine.name, 'time', 'http')
209

References searx.result_types._base.LegacyResult.engine, searx.result_types._base.MainResult.engine, searx.result_types._base.Result.engine, and engine.

Referenced by extend_container().

Here is the caller graph for this function:

◆ extend_container()

searx.search.processors.abstract.EngineProcessor.extend_container ( self,
"ResultContainer" result_container,
float start_time,
"list[Result | LegacyResult]|None" search_results )

Definition at line 210 of file abstract.py.

215 ):
216 if getattr(threading.current_thread(), '_timeout', False):
217 # the main thread is not waiting anymore
218 self.handle_exception(result_container, 'timeout', False)
219 else:
220 # check if the engine accepted the request
221 if search_results is not None:
222 self._extend_container_basic(result_container, start_time, search_results)
223 self.suspended_status.resume()
224

References _extend_container_basic(), handle_exception(), and suspended_status.

Referenced by searx.search.processors.offline.OfflineProcessor.search(), and searx.search.processors.online.OnlineProcessor.search().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extend_container_if_suspended()

bool searx.search.processors.abstract.EngineProcessor.extend_container_if_suspended ( self,
"ResultContainer" result_container )

Definition at line 225 of file abstract.py.

225 def extend_container_if_suspended(self, result_container: "ResultContainer") -> bool:
226 if self.suspended_status.is_suspended:
227 result_container.add_unresponsive_engine(
228 self.engine.name, self.suspended_status.suspend_reason, suspended=True
229 )
230 return True
231 return False
232

References searx.result_types._base.LegacyResult.engine, searx.result_types._base.MainResult.engine, searx.result_types._base.Result.engine, engine, and suspended_status.

◆ get_default_tests()

searx.search.processors.abstract.EngineProcessor.get_default_tests ( self)

Definition at line 296 of file abstract.py.

296 def get_default_tests(self):
297 # deprecated!
298 return {}

◆ get_params()

RequestParams | None searx.search.processors.abstract.EngineProcessor.get_params ( self,
"SearchQuery" search_query,
str engine_category )
Returns a dictionary with the :ref:`request parameters <engine
request arguments>` (:py:obj:`RequestParams`), if the search condition
is not supported by the engine, ``None`` is returned:

- *time range* filter in search conditions, but the engine does not have
   a corresponding filter
- page number > 1 when engine does not support paging
- page number > ``max_page``

Reimplemented in searx.search.processors.online.OnlineProcessor, searx.search.processors.online_currency.OnlineCurrencyProcessor, searx.search.processors.online_dictionary.OnlineDictionaryProcessor, and searx.search.processors.online_url_search.OnlineUrlSearchProcessor.

Definition at line 233 of file abstract.py.

233 def get_params(self, search_query: "SearchQuery", engine_category: str) -> RequestParams | None:
234 """Returns a dictionary with the :ref:`request parameters <engine
235 request arguments>` (:py:obj:`RequestParams`), if the search condition
236 is not supported by the engine, ``None`` is returned:
237
238 - *time range* filter in search conditions, but the engine does not have
239 a corresponding filter
240 - page number > 1 when engine does not support paging
241 - page number > ``max_page``
242
243 """
244 # if paging is not supported, skip
245 if search_query.pageno > 1 and not self.engine.paging:
246 return None
247
248 # if max page is reached, skip
249 max_page = self.engine.max_page or get_setting("search.max_page")
250 if max_page and max_page < search_query.pageno:
251 return None
252
253 # if time_range is not supported, skip
254 if search_query.time_range and not self.engine.time_range_support:
255 return None
256
257 params: RequestParams = {
258 "query": search_query.query,
259 "category": engine_category,
260 "pageno": search_query.pageno,
261 "safesearch": search_query.safesearch,
262 "time_range": search_query.time_range,
263 "engine_data": search_query.engine_data.get(self.engine.name, {}),
264 "searxng_locale": search_query.lang,
265 }
266
267 # deprecated / vintage --> use params["searxng_locale"]
268 #
269 # Conditions related to engine's traits are implemented in engine.traits
270 # module. Don't do "locale" decisions here in the abstract layer of the
271 # search processor, just pass the value from user's choice unchanged to
272 # the engine request.
273
274 if hasattr(self.engine, "language") and self.engine.language:
275 params["language"] = self.engine.language # pyright: ignore[reportGeneralTypeIssues]
276 else:
277 params["language"] = search_query.lang # pyright: ignore[reportGeneralTypeIssues]
278
279 return params
280

References searx.result_types._base.LegacyResult.engine, searx.result_types._base.MainResult.engine, searx.result_types._base.Result.engine, engine, and searx.get_setting().

Here is the call graph for this function:

◆ get_tests()

searx.search.processors.abstract.EngineProcessor.get_tests ( self)

Definition at line 292 of file abstract.py.

292 def get_tests(self):
293 # deprecated!
294 return {}
295

◆ handle_exception()

searx.search.processors.abstract.EngineProcessor.handle_exception ( self,
"ResultContainer" result_container,
BaseException | str exception_or_message,
bool suspend = False )

Definition at line 165 of file abstract.py.

170 ):
171 # update result_container
172 if isinstance(exception_or_message, BaseException):
173 exception_class = exception_or_message.__class__
174 module_name = getattr(exception_class, '__module__', 'builtins')
175 module_name = '' if module_name == 'builtins' else module_name + '.'
176 error_message = module_name + exception_class.__qualname__
177 else:
178 error_message = exception_or_message
179 result_container.add_unresponsive_engine(self.engine.name, error_message)
180 # metrics
181 counter_inc('engine', self.engine.name, 'search', 'count', 'error')
182 if isinstance(exception_or_message, BaseException):
183 count_exception(self.engine.name, exception_or_message)
184 else:
185 count_error(self.engine.name, exception_or_message)
186 # suspend the engine ?
187 if suspend:
188 suspended_time = None
189 if isinstance(exception_or_message, SearxEngineAccessDeniedException):
190 suspended_time = exception_or_message.suspended_time
191 self.suspended_status.suspend(suspended_time, error_message) # pylint: disable=no-member
192

References searx.result_types._base.LegacyResult.engine, searx.result_types._base.MainResult.engine, searx.result_types._base.Result.engine, engine, and suspended_status.

Referenced by extend_container(), searx.search.processors.offline.OfflineProcessor.search(), and searx.search.processors.online.OnlineProcessor.search().

Here is the caller graph for this function:

◆ init_engine()

bool searx.search.processors.abstract.EngineProcessor.init_engine ( self)

Reimplemented in searx.search.processors.online.OnlineProcessor.

Definition at line 151 of file abstract.py.

151 def init_engine(self) -> bool:
152 eng_setting = get_engine_from_settings(self.engine.name)
153 init_ok: bool | None = False
154 try:
155 init_ok = self.engine.init(eng_setting)
156 except Exception: # pylint: disable=broad-except
157 logger.exception("Init method of engine %s failed due to an exception.", self.engine.name)
158 init_ok = False
159 # In older engines, None is returned from the init method, which is
160 # equivalent to indicating that the initialization was successful.
161 if init_ok is None:
162 init_ok = True
163 return init_ok
164

References searx.result_types._base.LegacyResult.engine, searx.result_types._base.MainResult.engine, searx.result_types._base.Result.engine, and engine.

Referenced by initialize().

Here is the caller graph for this function:

◆ initialize()

searx.search.processors.abstract.EngineProcessor.initialize ( self,
t.Callable[["EngineProcessor", bool], bool] callback )
Initialization of *this* :py:obj:`EngineProcessor`.

If processor's engine has an ``init`` method, it is called first.
Engine's ``init`` method is executed in a thread, meaning that the
*registration* (the ``callback``) may occur later and is not already
established by the return from this registration method.

Registration only takes place if the ``init`` method is not available or
is successfully run through.

Reimplemented in searx.search.processors.online_currency.OnlineCurrencyProcessor.

Definition at line 123 of file abstract.py.

123 def initialize(self, callback: t.Callable[["EngineProcessor", bool], bool]):
124 """Initialization of *this* :py:obj:`EngineProcessor`.
125
126 If processor's engine has an ``init`` method, it is called first.
127 Engine's ``init`` method is executed in a thread, meaning that the
128 *registration* (the ``callback``) may occur later and is not already
129 established by the return from this registration method.
130
131 Registration only takes place if the ``init`` method is not available or
132 is successfully run through.
133 """
134
135 if not hasattr(self.engine, "init"):
136 callback(self, True)
137 return
138
139 if not callable(self.engine.init):
140 logger.error("Engine's init method isn't a callable (is of type: %s).", type(self.engine.init))
141 callback(self, False)
142 return
143
144 def __init_processor_thread():
145 eng_ok = self.init_engine()
146 callback(self, eng_ok)
147
148 # set up and start a thread
149 threading.Thread(target=__init_processor_thread, daemon=True).start()
150

References searx.result_types._base.LegacyResult.engine, searx.result_types._base.MainResult.engine, searx.result_types._base.Result.engine, engine, and init_engine().

Here is the call graph for this function:

◆ search()

searx.search.processors.abstract.EngineProcessor.search ( self,
str query,
RequestParams params,
"ResultContainer" result_container,
float start_time,
float timeout_limit )

Reimplemented in searx.search.processors.offline.OfflineProcessor, and searx.search.processors.online.OnlineProcessor.

Definition at line 282 of file abstract.py.

289 ):
290 pass
291

Member Data Documentation

◆ engine

◆ logger

logging.Logger searx.search.processors.abstract.EngineProcessor.logger = engines[engine.name].logger

◆ suspended_status

SuspendedStatus searx.search.processors.abstract.EngineProcessor.suspended_status = SUSPENDED_STATUS.setdefault(key, SuspendedStatus())

Definition at line 121 of file abstract.py.

Referenced by extend_container(), extend_container_if_suspended(), and handle_exception().


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/search/processors/abstract.py