浏览代码

[enh] plugin support basics ++ self ip plugin

Adam Tauber 10 年前
父节点
当前提交
00cc4dcbf4
共有 3 个文件被更改,包括 80 次插入12 次删除
  1. 46
    0
      searx/plugins/__init__.py
  2. 17
    0
      searx/plugins/self_ip.py
  3. 17
    12
      searx/webapp.py

+ 46
- 0
searx/plugins/__init__.py 查看文件

1
+from searx.plugins import self_ip
2
+from searx import logger
3
+from sys import exit
4
+
5
+logger = logger.getChild('plugins')
6
+
7
+required_attrs = ('name',
8
+                  'description',
9
+                  'default_on')
10
+
11
+
12
+class Plugin():
13
+    default_on = False
14
+    name = 'Default plugin'
15
+
16
+
17
+class PluginStore():
18
+
19
+    def __init__(self):
20
+        self.plugins = []
21
+
22
+    def __iter__(self):
23
+        for plugin in plugins:
24
+            yield plugin
25
+
26
+    def register(self, *plugins):
27
+        for plugin in plugins:
28
+            for plugin_attr in required_attrs:
29
+                if not hasattr(plugin, plugin_attr):
30
+                    logger.critical('missing attribute "{0}", cannot load plugin: {1}'.format(plugin_attr, plugin))
31
+                    exit(3)
32
+            self.plugins.append(plugin)
33
+
34
+    def call(self, plugin_type, request, *args, **kwargs):
35
+        ret = True
36
+        for plugin in self.plugins:
37
+            if hasattr(plugin, plugin_type):
38
+                ret = getattr(plugin, plugin_type)(request, *args, **kwargs)
39
+                if not ret:
40
+                    break
41
+
42
+        return ret
43
+
44
+
45
+plugins = PluginStore()
46
+plugins.register(self_ip)

+ 17
- 0
searx/plugins/self_ip.py 查看文件

1
+
2
+name = "Self IP"
3
+description = ""
4
+default_on = True
5
+
6
+
7
+def pre_search(request, ctx):
8
+    if ctx['search'].query == 'ip':
9
+        x_forwarded_for = request.headers.getlist("X-Forwarded-For")
10
+        if x_forwarded_for:
11
+            ip = x_forwarded_for[0]
12
+        else:
13
+            ip = request.remote_addr
14
+        ctx['search'].answers.clear()
15
+        ctx['search'].answers.add(ip)
16
+        return False
17
+    return True

+ 17
- 12
searx/webapp.py 查看文件

27
 import os
27
 import os
28
 import hashlib
28
 import hashlib
29
 
29
 
30
+from searx import logger
31
+logger = logger.getChild('webapp')
32
+
33
+try:
34
+    from pygments import highlight
35
+    from pygments.lexers import get_lexer_by_name
36
+    from pygments.formatters import HtmlFormatter
37
+except:
38
+    logger.critical("cannot import dependency: pygments")
39
+    from sys import exit
40
+    exit(1)
41
+
30
 from datetime import datetime, timedelta
42
 from datetime import datetime, timedelta
31
 from urllib import urlencode
43
 from urllib import urlencode
32
 from werkzeug.contrib.fixers import ProxyFix
44
 from werkzeug.contrib.fixers import ProxyFix
51
 from searx.search import Search
63
 from searx.search import Search
52
 from searx.query import Query
64
 from searx.query import Query
53
 from searx.autocomplete import searx_bang, backends as autocomplete_backends
65
 from searx.autocomplete import searx_bang, backends as autocomplete_backends
54
-from searx import logger
55
-try:
56
-    from pygments import highlight
57
-    from pygments.lexers import get_lexer_by_name
58
-    from pygments.formatters import HtmlFormatter
59
-except:
60
-    logger.critical("cannot import dependency: pygments")
61
-    from sys import exit
62
-    exit(1)
66
+from searx.plugins import plugins
63
 
67
 
64
 
68
 
65
-logger = logger.getChild('webapp')
66
-
67
 static_path, templates_path, themes =\
69
 static_path, templates_path, themes =\
68
     get_themes(settings['themes_path']
70
     get_themes(settings['themes_path']
69
                if settings.get('themes_path')
71
                if settings.get('themes_path')
323
             'index.html',
325
             'index.html',
324
         )
326
         )
325
 
327
 
326
-    search.search(request)
328
+    if plugins.call('pre_search', request, locals()):
329
+        search.search(request)
330
+
331
+    plugins.call('post_search', request, locals())
327
 
332
 
328
     for result in search.results:
333
     for result in search.results:
329
 
334