Browse Source

[enh] plugin support basics ++ self ip plugin

Adam Tauber 10 years ago
parent
commit
00cc4dcbf4
3 changed files with 80 additions and 12 deletions
  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 View File

@@ -0,0 +1,46 @@
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 View File

@@ -0,0 +1,17 @@
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 View File

@@ -27,6 +27,18 @@ import cStringIO
27 27
 import os
28 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 42
 from datetime import datetime, timedelta
31 43
 from urllib import urlencode
32 44
 from werkzeug.contrib.fixers import ProxyFix
@@ -51,19 +63,9 @@ from searx.https_rewrite import https_url_rewrite
51 63
 from searx.search import Search
52 64
 from searx.query import Query
53 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 69
 static_path, templates_path, themes =\
68 70
     get_themes(settings['themes_path']
69 71
                if settings.get('themes_path')
@@ -323,7 +325,10 @@ def index():
323 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 333
     for result in search.results:
329 334