on master help sign in
[enh] add external plugin support
commit1f2dc6c64785ab962efbeed0cfc093fdd3f41bf0 authorAdam Tauber <asciimoo@gmail.com> dateTue 14 Jul 2020, 18:56 (6 years ago) parent1185c06
5 files changed+78−3
docs/dev/plugins.rst+7 searx/__init__.py+1 searx/plugins/__init__.py+61−2 searx/settings.yml+8 searx/webapp.py+1−1
docs/dev/plugins.rst+7
@@ -30,6 +30,13 @@ Example plugin
3030 ctx['search'].suggestions.add('example')
3131 return True
3232
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+
3340Register your plugin
3441====================
3542
searx/__init__.py+1
@@ -30,6 +30,7 @@ except:
3030
3131searx_dir = abspath(dirname(__file__))
3232engine_dir = dirname(realpath(__file__))
33+static_path = abspath(join(dirname(__file__), 'static'))
3334
3435
3536def 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/ >.
1414
1515(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
1616'''
17+
18+from importlib import import_module
19+from os.path import abspath, basename, dirname, exists, join
20+from shutil import copyfile
1721from sys import exit, version_info
18−from searx import logger
22+from traceback import print_exc
23+
24+from searx import logger, settings, static_path
1925
2026if version_info[0] == 3:
2127 unicode = str
@@ -54,7 +60,9 @@ class PluginStore():
5460 for plugin in self.plugins:
5561 yield plugin
5662
57− def register(self, *plugins):
63+ def register(self, *plugins, external=False):
64+ if external:
65+ plugins = load_external_plugins(plugins)
5866 for plugin in plugins:
5967 for plugin_attr, plugin_attr_type in required_attrs:
6068 if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
@@ -77,6 +85,54 @@ class PluginStore():
7785 return ret
7886
7987
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+
80136plugins = PluginStore()
81137plugins.register(oa_doi_rewrite)
82138plugins.register(https_rewrite)
@@ -86,3 +142,6 @@ plugins.register(self_info)
86142plugins.register(search_on_category_select)
87143plugins.register(tracker_url_remover)
88144plugins.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
5757# - 1.1.1.1
5858# - 1.1.1.2
5959
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+
6068engines:
6169 - name: apk mirror
6270 engine: apkmirror
searx/webapp.py+1−1
@@ -58,7 +58,7 @@ import flask_babel
5858from flask_babel import Babel, gettext, format_date, format_decimal
5959from flask.ctx import has_request_context
6060from flask.json import jsonify
61−from searx import brand
61+from searx import brand, static_path
6262from searx import settings, searx_dir, searx_debug
6363from searx.exceptions import SearxParameterException
6464from searx.engines import (