on master help sign in
[enh] plugin support basics ++ self ip plugin
commit00cc4dcbf44d9ecea89befb08cae4ee5561c4247 authorAdam Tauber <asciimoo@gmail.com> dateTue 10 Mar 2015, 19:55 (12 years ago) parent8d1d481
3 files changed+80−12
searx/plugins/__init__.py+46 searx/plugins/self_ip.py+17 searx/webapp.py+17−12
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
2727import os
2828import hashlib
2929
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+
3042from datetime import datetime, timedelta
3143from urllib import urlencode
3244from werkzeug.contrib.fixers import ProxyFix
@@ -51,19 +63,9 @@ from searx.https_rewrite import https_url_rewrite
5163from searx.search import Search
5264from searx.query import Query
5365from 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
6367
6468
65−logger = logger.getChild('webapp')
66−
6769static_path, templates_path, themes =\
6870 get_themes(settings['themes_path']
6971 if settings.get('themes_path')
@@ -323,7 +325,10 @@ def index():
323325 'index.html',
324326 )
325327
326− search.search(request)
328+ if plugins.call('pre_search', request, locals()):
329+ search.search(request)
330+
331+ plugins.call('post_search', request, locals())
327332
328333 for result in search.results:
329334