on master help sign in
[enh] add external plugin support
1f2dc6cAdam Tauber
docs/dev/plugins.rst+7
@@ -30,6 +30,13 @@ Example plugin
30 ctx['search'].suggestions.add('example')
30 ctx['search'].suggestions.add('example')
31 return True
31 return True
32
32
33+External plugins
34+================
35+
36+External plugins are standard python modules implementing all the requirements of the standard plugins.
37+Plugins can be enabled by adding them to :ref:`settings.yml`'s ``plugins`` section.
38+Example external plugin can be found `here <https://github.com/asciimoo/searx_external_plugin_example>`_.
39+
33Register your plugin
40Register your plugin
34====================
41====================
35
42
searx/__init__.py+1
@@ -30,6 +30,7 @@ except:
30
30
31searx_dir = abspath(dirname(__file__))
31searx_dir = abspath(dirname(__file__))
32engine_dir = dirname(realpath(__file__))
32engine_dir = dirname(realpath(__file__))
33+static_path = abspath(join(dirname(__file__), 'static'))
33
34
34
35
35def check_settings_yml(file_name):
36def check_settings_yml(file_name):
searx/plugins/__init__.py+61−2
@@ -14,8 +14,14 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
14
14
15(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
15(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
16'''
16'''
17+
18+from importlib import import_module
19+from os.path import abspath, basename, dirname, exists, join
20+from shutil import copyfile
17from sys import exit, version_info
21from sys import exit, version_info
22+from traceback import print_exc
23+
18−from searx import logger
24+from searx import logger, settings, static_path
19
25
20if version_info[0] == 3:
26if version_info[0] == 3:
21 unicode = str
27 unicode = str
@@ -54,7 +60,9 @@ class PluginStore():
54 for plugin in self.plugins:
60 for plugin in self.plugins:
55 yield plugin
61 yield plugin
56
62
57− def register(self, *plugins):
63+ def register(self, *plugins, external=False):
64+ if external:
65+ plugins = load_external_plugins(plugins)
58 for plugin in plugins:
66 for plugin in plugins:
59 for plugin_attr, plugin_attr_type in required_attrs:
67 for plugin_attr, plugin_attr_type in required_attrs:
60 if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
68 if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
@@ -77,6 +85,54 @@ class PluginStore():
77 return ret
85 return ret
78
86
79
87
88+def load_external_plugins(plugin_names):
89+ plugins = []
90+ for name in plugin_names:
91+ logger.debug('loading plugin: {0}'.format(name))
92+ try:
93+ pkg = import_module(name)
94+ except Exception as e:
95+ logger.critical('failed to load plugin module {0}: {1}'.format(name, e))
96+ exit(3)
97+
98+ pkg.__base_path = dirname(abspath(pkg.__file__))
99+
100+ fix_package_resources(pkg, name)
101+
102+ plugins.append(pkg)
103+ logger.debug('plugin "{0}" loaded'.format(name))
104+ return plugins
105+
106+
107+def check_resource(base_path, resource_path, name, dir_prefix):
108+ dep_path = join(base_path, resource_path)
109+ file_name = basename(dep_path)
110+ resource_name = '{0}_{1}'.format('_'.join(name.split()), file_name)
111+ resource_path = join(static_path, 'plugins', dir_prefix, resource_name)
112+ if not exists(resource_path):
113+ try:
114+ copyfile(dep_path, resource_path)
115+ except:
116+ logger.critical('failed to copy plugin resource {0} for plugin {1}'.format(resource_name, name))
117+ exit(3)
118+
119+ # returning with the web path of the resource
120+ return join('plugins', dir_prefix, resource_name)
121+
122+
123+def fix_package_resources(pkg, name):
124+ if hasattr(pkg, 'js_dependencies'):
125+ pkg.js_dependencies = tuple([
126+ check_resource(pkg.__base_path, x, name, 'js')
127+ for x in pkg.js_dependencies
128+ ])
129+ if hasattr(pkg, 'css_dependencies'):
130+ pkg.css_dependencies = tuple([
131+ check_resource(pkg.__base_path, x, name, 'css')
132+ for x in pkg.css_dependencies
133+ ])
134+
135+
80plugins = PluginStore()
136plugins = PluginStore()
81plugins.register(oa_doi_rewrite)
137plugins.register(oa_doi_rewrite)
82plugins.register(https_rewrite)
138plugins.register(https_rewrite)
@@ -86,3 +142,6 @@ plugins.register(self_info)
86plugins.register(search_on_category_select)
142plugins.register(search_on_category_select)
87plugins.register(tracker_url_remover)
143plugins.register(tracker_url_remover)
88plugins.register(vim_hotkeys)
144plugins.register(vim_hotkeys)
145+# load external plugins
146+if 'plugins' in settings:
147+ plugins.register(*settings['plugins'], external=True)
searx/settings.yml+8
@@ -57,6 +57,14 @@ outgoing: # communication with search engines
57# - 1.1.1.1
57# - 1.1.1.1
58# - 1.1.1.2
58# - 1.1.1.2
59
59
60+# External plugin configuration
61+# See http://asciimoo.github.io/searx/dev/plugins.html for more details
62+#
63+# plugins:
64+# - plugin1
65+# - plugin2
66+# - ...
67+
60engines:
68engines:
61 - name: apk mirror
69 - name: apk mirror
62 engine: apkmirror
70 engine: apkmirror
searx/webapp.py+1−1
@@ -58,7 +58,7 @@ import flask_babel
58from flask_babel import Babel, gettext, format_date, format_decimal
58from flask_babel import Babel, gettext, format_date, format_decimal
59from flask.ctx import has_request_context
59from flask.ctx import has_request_context
60from flask.json import jsonify
60from flask.json import jsonify
61−from searx import brand
61+from searx import brand, static_path
62from searx import settings, searx_dir, searx_debug
62from searx import settings, searx_dir, searx_debug
63from searx.exceptions import SearxParameterException
63from searx.exceptions import SearxParameterException
64from searx.engines import (
64from searx.engines import (