diff options
Diffstat (limited to 'searx/search/__init__.py')
| -rw-r--r-- | searx/search/__init__.py | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/searx/search/__init__.py b/searx/search/__init__.py index d8d3e1e1c..69d7ffb25 100644 --- a/searx/search/__init__.py +++ b/searx/search/__init__.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: AGPL-3.0-or-later # lint: pylint -# pylint: disable=missing-module-docstring +# pylint: disable=missing-module-docstring, too-few-public-methods import typing import threading @@ -39,7 +39,7 @@ class Search: __slots__ = "search_query", "result_container", "start_time", "actual_timeout" - def __init__(self, search_query): + def __init__(self, search_query: SearchQuery): # init vars super().__init__() self.search_query = search_query @@ -163,7 +163,7 @@ class Search: return True # do search-request - def search(self): + def search(self) -> ResultContainer: self.start_time = default_timer() if not self.search_external_bang(): if not self.search_answerers(): @@ -172,24 +172,32 @@ class Search: class SearchWithPlugins(Search): - """Similar to the Search class but call the plugins.""" + """Inherit from the Search class, add calls to the plugins.""" __slots__ = 'ordered_plugin_list', 'request' - def __init__(self, search_query, ordered_plugin_list, request): + def __init__(self, search_query: SearchQuery, ordered_plugin_list, request: "flask.Request"): super().__init__(search_query) self.ordered_plugin_list = ordered_plugin_list - self.request = request - - def search(self): + self.result_container.on_result = self._on_result + # pylint: disable=line-too-long + # get the "real" request to use it outside the Flask context. + # see + # * https://github.com/pallets/flask/blob/d01d26e5210e3ee4cbbdef12f05c886e08e92852/src/flask/globals.py#L55 + # * https://github.com/pallets/werkzeug/blob/3c5d3c9bd0d9ce64590f0af8997a38f3823b368d/src/werkzeug/local.py#L548-L559 + # * https://werkzeug.palletsprojects.com/en/2.0.x/local/#werkzeug.local.LocalProxy._get_current_object + # pylint: enable=line-too-long + self.request = request._get_current_object() + + def _on_result(self, result): + return plugins.call(self.ordered_plugin_list, 'on_result', self.request, self, result) + + def search(self) -> ResultContainer: if plugins.call(self.ordered_plugin_list, 'pre_search', self.request, self): super().search() plugins.call(self.ordered_plugin_list, 'post_search', self.request, self) - results = self.result_container.get_ordered_results() - - for result in results: - plugins.call(self.ordered_plugin_list, 'on_result', self.request, self, result) + self.result_container.close() return self.result_container |