[enh] introduce Engine class

Previously engines were defined in modules, which where then
stapled with logger, supported_languages & language_aliases
via monkey patching in searx/engines/__init__.py.

Monkey patching is obviously a bad practice since it confuses
humans as well as static type checkers. But there were more problems:

* there were no type hints for the method input and output types

* the request method did not even make a clear distinction between input
  and output (abusing a single "params" dictionary for both)

This commit introduces a new class-based architecture for engines,
in a backwards-compatiable manner: allowing old-style module engines
to be used along witn new-style class-based engines.
This commit is contained in:
Martin Fischer 2022-01-28 10:38:37 +01:00
parent 73e71508e4
commit 5dd28ff04b
5 changed files with 147 additions and 7 deletions

View file

@ -17,6 +17,7 @@ from searx.exceptions import (
SearxEngineTooManyRequestsException,
)
from searx.metrics.error_recorder import count_error
from searx.engine import OnlineEngine, QueryContext
from .abstract import EngineProcessor
@ -114,7 +115,21 @@ class OnlineProcessor(EngineProcessor):
def _search_basic(self, query, params):
# update request parameters dependent on
# search-engine (contained in engines folder)
self.engine.request(query, params)
if isinstance(self.engine, OnlineEngine):
params.update(
self.engine.request(
query,
QueryContext(
category=params['category'],
safesearch=params['safesearch'],
time_range=params['time_range'],
pageno=params['pageno'],
language=params['language'],
),
).__dict__
)
else:
self.engine.request(query, params)
# ignoring empty urls
if params['url'] is None: