Browse Source

[mod] searx uses flask framework only in webapp.py. Make migration to another framework easier.

Alexandre Flament 8 years ago
parent
commit
84a2c97a65
4 changed files with 21 additions and 24 deletions
  1. 2
    2
      searx/plugins/__init__.py
  2. 5
    4
      searx/search.py
  3. 1
    1
      searx/webapp.py
  4. 13
    17
      tests/unit/test_plugins.py

+ 2
- 2
searx/plugins/__init__.py View File

@@ -63,9 +63,9 @@ class PluginStore():
63 63
             plugin.id = plugin.name.replace(' ', '_')
64 64
             self.plugins.append(plugin)
65 65
 
66
-    def call(self, plugin_type, request, *args, **kwargs):
66
+    def call(self, ordered_plugin_list, plugin_type, request, *args, **kwargs):
67 67
         ret = True
68
-        for plugin in request.user_plugins:
68
+        for plugin in ordered_plugin_list:
69 69
             if hasattr(plugin, plugin_type):
70 70
                 ret = getattr(plugin, plugin_type)(request, *args, **kwargs)
71 71
                 if not ret:

+ 5
- 4
searx/search.py View File

@@ -395,19 +395,20 @@ class SearchWithPlugins(Search):
395 395
 
396 396
     """Similar to the Search class but call the plugins."""
397 397
 
398
-    def __init__(self, search_query, request):
398
+    def __init__(self, search_query, ordered_plugin_list, request):
399 399
         super(SearchWithPlugins, self).__init__(search_query)
400
+        self.ordered_plugin_list = ordered_plugin_list
400 401
         self.request = request
401 402
 
402 403
     def search(self):
403
-        if plugins.call('pre_search', self.request, self):
404
+        if plugins.call(self.ordered_plugin_list, 'pre_search', self.request, self):
404 405
             super(SearchWithPlugins, self).search()
405 406
 
406
-        plugins.call('post_search', self.request, self)
407
+        plugins.call(self.ordered_plugin_list, 'post_search', self.request, self)
407 408
 
408 409
         results = self.result_container.get_ordered_results()
409 410
 
410 411
         for result in results:
411
-            plugins.call('on_result', self.request, self, result)
412
+            plugins.call(self.ordered_plugin_list, 'on_result', self.request, self, result)
412 413
 
413 414
         return self.result_container

+ 1
- 1
searx/webapp.py View File

@@ -419,7 +419,7 @@ def index():
419 419
     try:
420 420
         search_query = get_search_query_from_webapp(request.preferences, request.form)
421 421
         # search = Search(search_query) #  without plugins
422
-        search = SearchWithPlugins(search_query, request)
422
+        search = SearchWithPlugins(search_query, request.user_plugins, request)
423 423
         result_container = search.search()
424 424
     except:
425 425
         request.errors.append(gettext('search error'))

+ 13
- 17
tests/unit/test_plugins.py View File

@@ -28,14 +28,12 @@ class PluginStoreTest(SearxTestCase):
28 28
         testplugin = plugins.Plugin()
29 29
         store.register(testplugin)
30 30
         setattr(testplugin, 'asdf', Mock())
31
-        request = Mock(user_plugins=[])
32
-        store.call('asdf', request, Mock())
31
+        request = Mock()
32
+        store.call([], 'asdf', request, Mock())
33 33
 
34 34
         self.assertFalse(testplugin.asdf.called)
35 35
 
36
-        request.user_plugins.append(testplugin)
37
-        store.call('asdf', request, Mock())
38
-
36
+        store.call([testplugin], 'asdf', request, Mock())
39 37
         self.assertTrue(testplugin.asdf.called)
40 38
 
41 39
 
@@ -48,42 +46,40 @@ class SelfIPTest(SearxTestCase):
48 46
         self.assertTrue(len(store.plugins) == 1)
49 47
 
50 48
         # IP test
51
-        request = Mock(user_plugins=store.plugins,
52
-                       remote_addr='127.0.0.1')
49
+        request = Mock(remote_addr='127.0.0.1')
53 50
         request.headers.getlist.return_value = []
54 51
         search = get_search_mock(query='ip', pageno=1)
55
-        store.call('post_search', request, search)
52
+        store.call(store.plugins, 'post_search', request, search)
56 53
         self.assertTrue('127.0.0.1' in search.result_container.answers)
57 54
 
58 55
         search = get_search_mock(query='ip', pageno=2)
59
-        store.call('post_search', request, search)
56
+        store.call(store.plugins, 'post_search', request, search)
60 57
         self.assertFalse('127.0.0.1' in search.result_container.answers)
61 58
 
62 59
         # User agent test
63
-        request = Mock(user_plugins=store.plugins,
64
-                       user_agent='Mock')
60
+        request = Mock(user_agent='Mock')
65 61
         request.headers.getlist.return_value = []
66 62
 
67 63
         search = get_search_mock(query='user-agent', pageno=1)
68
-        store.call('post_search', request, search)
64
+        store.call(store.plugins, 'post_search', request, search)
69 65
         self.assertTrue('Mock' in search.result_container.answers)
70 66
 
71 67
         search = get_search_mock(query='user-agent', pageno=2)
72
-        store.call('post_search', request, search)
68
+        store.call(store.plugins, 'post_search', request, search)
73 69
         self.assertFalse('Mock' in search.result_container.answers)
74 70
 
75 71
         search = get_search_mock(query='user-agent', pageno=1)
76
-        store.call('post_search', request, search)
72
+        store.call(store.plugins, 'post_search', request, search)
77 73
         self.assertTrue('Mock' in search.result_container.answers)
78 74
 
79 75
         search = get_search_mock(query='user-agent', pageno=2)
80
-        store.call('post_search', request, search)
76
+        store.call(store.plugins, 'post_search', request, search)
81 77
         self.assertFalse('Mock' in search.result_container.answers)
82 78
 
83 79
         search = get_search_mock(query='What is my User-Agent?', pageno=1)
84
-        store.call('post_search', request, search)
80
+        store.call(store.plugins, 'post_search', request, search)
85 81
         self.assertTrue('Mock' in search.result_container.answers)
86 82
 
87 83
         search = get_search_mock(query='What is my User-Agent?', pageno=2)
88
-        store.call('post_search', request, search)
84
+        store.call(store.plugins, 'post_search', request, search)
89 85
         self.assertFalse('Mock' in search.result_container.answers)