@@ -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)