Change plugin API :

- pre_search(request, search)
- post_search(request, search)
- on_result(request, search, result)

with
- request is the Flask request
- search a searx.Search instance
- result a searx result as usual
This commit is contained in:
dalf 2016-10-22 14:01:53 +02:00 committed by Alexandre Flament
parent 67e11c42b9
commit fbb080f358
6 changed files with 54 additions and 67 deletions

View file

@ -20,12 +20,12 @@ def extract_doi(url):
return None return None
def on_result(request, ctx): def on_result(request, search, result):
doi = extract_doi(ctx['result']['parsed_url']) doi = extract_doi(result['parsed_url'])
if doi and len(doi) < 50: if doi and len(doi) < 50:
for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'): for suffix in ('/', '.pdf', '/full', '/meta', '/abstract'):
if doi.endswith(suffix): if doi.endswith(suffix):
doi = doi[:-len(suffix)] doi = doi[:-len(suffix)]
ctx['result']['url'] = 'http://doai.io/' + doi result['url'] = 'http://doai.io/' + doi
ctx['result']['parsed_url'] = urlparse(ctx['result']['url']) result['parsed_url'] = urlparse(ctx['result']['url'])
return True return True

View file

@ -220,8 +220,7 @@ def https_url_rewrite(result):
return result return result
def on_result(request, ctx): def on_result(request, search, result):
result = ctx['result']
if result['parsed_url'].scheme == 'http': if result['parsed_url'].scheme == 'http':
https_url_rewrite(result) https_url_rewrite(result)
return True return True

View file

@ -28,19 +28,19 @@ p = re.compile('.*user[ -]agent.*', re.IGNORECASE)
# attach callback to the post search hook # attach callback to the post search hook
# request: flask request object # request: flask request object
# ctx: the whole local context of the pre search hook # ctx: the whole local context of the pre search hook
def post_search(request, ctx): def post_search(request, search):
if ctx['search'].pageno > 1: if search.search_query.pageno > 1:
return True return True
if ctx['search'].query == 'ip': if search.search_query.query == 'ip':
x_forwarded_for = request.headers.getlist("X-Forwarded-For") x_forwarded_for = request.headers.getlist("X-Forwarded-For")
if x_forwarded_for: if x_forwarded_for:
ip = x_forwarded_for[0] ip = x_forwarded_for[0]
else: else:
ip = request.remote_addr ip = request.remote_addr
ctx['result_container'].answers.clear() search.result_container.answers.clear()
ctx['result_container'].answers.add(ip) search.result_container.answers.add(ip)
elif p.match(ctx['search'].query): elif p.match(search.search_query.query):
ua = request.user_agent ua = request.user_agent
ctx['result_container'].answers.clear() search.result_container.answers.clear()
ctx['result_container'].answers.add(ua) search.result_container.answers.add(ua)
return True return True

View file

@ -28,8 +28,8 @@ description = gettext('Remove trackers arguments from the returned URL')
default_on = True default_on = True
def on_result(request, ctx): def on_result(request, search, result):
query = ctx['result']['parsed_url'].query query = result['parsed_url'].query
if query == "": if query == "":
return True return True
@ -37,8 +37,8 @@ def on_result(request, ctx):
for reg in regexes: for reg in regexes:
query = reg.sub('', query) query = reg.sub('', query)
if query != ctx['result']['parsed_url'].query: if query != result['parsed_url'].query:
ctx['result']['parsed_url'] = ctx['result']['parsed_url']._replace(query=query) result['parsed_url'] = result['parsed_url']._replace(query=query)
ctx['result']['url'] = urlunparse(ctx['result']['parsed_url']) result['url'] = urlunparse(result['parsed_url'])
return True return True

View file

@ -357,35 +357,23 @@ class Search(object):
return self.result_container return self.result_container
def search_with_plugins(do_search, search_query, request, request_data, result_container):
"""Search using the do_search function and with plugins filtering.
Standalone function to have a well define locals().
result_container contains the results after the function call.
"""
search = search_query
if plugins.call('pre_search', request, locals()):
do_search()
plugins.call('post_search', request, locals())
results = result_container.get_ordered_results()
for result in results:
plugins.call('on_result', request, locals())
class SearchWithPlugins(Search): class SearchWithPlugins(Search):
"""Similar to the Search class but call the plugins."""
def __init__(self, search_query, request): def __init__(self, search_query, request):
super(SearchWithPlugins, self).__init__(search_query) super(SearchWithPlugins, self).__init__(search_query)
self.request = request self.request = request
self.request_data = request.request_data
def search(self): def search(self):
if plugins.call('pre_search', self.request, self):
def do_search():
super(SearchWithPlugins, self).search() super(SearchWithPlugins, self).search()
search_with_plugins(do_search, self.search_query, self.request, self.request_data, self.result_container) plugins.call('post_search', self.request, self)
results = self.result_container.get_ordered_results()
for result in results:
plugins.call('on_result', self.request, self, result)
return self.result_container return self.result_container

View file

@ -6,8 +6,8 @@ from mock import Mock
def get_search_mock(query, **kwargs): def get_search_mock(query, **kwargs):
return {'search': Mock(query=query, **kwargs), return Mock(search_query=Mock(query=query, **kwargs),
'result_container': Mock(answers=set())} result_container=Mock(answers=set()))
class PluginStoreTest(SearxTestCase): class PluginStoreTest(SearxTestCase):
@ -51,39 +51,39 @@ class SelfIPTest(SearxTestCase):
request = Mock(user_plugins=store.plugins, request = Mock(user_plugins=store.plugins,
remote_addr='127.0.0.1') remote_addr='127.0.0.1')
request.headers.getlist.return_value = [] request.headers.getlist.return_value = []
ctx = get_search_mock(query='ip', pageno=1) search = get_search_mock(query='ip', pageno=1)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertTrue('127.0.0.1' in ctx['result_container'].answers) self.assertTrue('127.0.0.1' in search.result_container.answers)
ctx = get_search_mock(query='ip', pageno=2) search = get_search_mock(query='ip', pageno=2)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertFalse('127.0.0.1' in ctx['result_container'].answers) self.assertFalse('127.0.0.1' in search.result_container.answers)
# User agent test # User agent test
request = Mock(user_plugins=store.plugins, request = Mock(user_plugins=store.plugins,
user_agent='Mock') user_agent='Mock')
request.headers.getlist.return_value = [] request.headers.getlist.return_value = []
ctx = get_search_mock(query='user-agent', pageno=1) search = get_search_mock(query='user-agent', pageno=1)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertTrue('Mock' in ctx['result_container'].answers) self.assertTrue('Mock' in search.result_container.answers)
ctx = get_search_mock(query='user-agent', pageno=2) search = get_search_mock(query='user-agent', pageno=2)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertFalse('Mock' in ctx['result_container'].answers) self.assertFalse('Mock' in search.result_container.answers)
ctx = get_search_mock(query='user-agent', pageno=1) search = get_search_mock(query='user-agent', pageno=1)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertTrue('Mock' in ctx['result_container'].answers) self.assertTrue('Mock' in search.result_container.answers)
ctx = get_search_mock(query='user-agent', pageno=2) search = get_search_mock(query='user-agent', pageno=2)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertFalse('Mock' in ctx['result_container'].answers) self.assertFalse('Mock' in search.result_container.answers)
ctx = get_search_mock(query='What is my User-Agent?', pageno=1) search = get_search_mock(query='What is my User-Agent?', pageno=1)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertTrue('Mock' in ctx['result_container'].answers) self.assertTrue('Mock' in search.result_container.answers)
ctx = get_search_mock(query='What is my User-Agent?', pageno=2) search = get_search_mock(query='What is my User-Agent?', pageno=2)
store.call('post_search', request, ctx) store.call('post_search', request, search)
self.assertFalse('Mock' in ctx['result_container'].answers) self.assertFalse('Mock' in search.result_container.answers)