on master help sign in
[enh] plugin support basics ++ self ip plugin
00cc4dcAdam Tauber
searx/plugins/__init__.pynew file+46
@@ -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)
searx/plugins/self_ip.pynew file+17
@@ -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
searx/webapp.py+17−12
@@ -27,6 +27,18 @@ import cStringIO
27import os
27import os
28import hashlib
28import 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+
30from datetime import datetime, timedelta
42from datetime import datetime, timedelta
31from urllib import urlencode
43from urllib import urlencode
32from werkzeug.contrib.fixers import ProxyFix
44from werkzeug.contrib.fixers import ProxyFix
@@ -51,19 +63,9 @@ from searx.https_rewrite import https_url_rewrite
51from searx.search import Search
63from searx.search import Search
52from searx.query import Query
64from searx.query import Query
53from searx.autocomplete import searx_bang, backends as autocomplete_backends
65from searx.autocomplete import searx_bang, backends as autocomplete_backends
54−from searx import logger
66+from searx.plugins import plugins
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)
63
67
64
68
65−logger = logger.getChild('webapp')
66−
67static_path, templates_path, themes =\
69static_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,7 +325,10 @@ def index():
323 'index.html',
325 'index.html',
324 )
326 )
325
327
328+ if plugins.call('pre_search', request, locals()):
326− search.search(request)
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