@@ -14,8 +14,14 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
15(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
15(C) 2015 by Adam Tauber, <asciimoo@gmail.com>
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
18−from searx import logger
24+from searx import logger, settings, static_path
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
57− def register(self, *plugins):
63+ def register(self, *plugins, external=False):
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
88+def load_external_plugins(plugin_names):
90+ for name in plugin_names:
91+ logger.debug('loading plugin: {0}'.format(name))
93+ pkg = import_module(name)
94+ except Exception as e:
95+ logger.critical('failed to load plugin module {0}: {1}'.format(name, e))
98+ pkg.__base_path = dirname(abspath(pkg.__file__))
100+ fix_package_resources(pkg, name)
103+ logger.debug('plugin "{0}" loaded'.format(name))
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):
114+ copyfile(dep_path, resource_path)
116+ logger.critical('failed to copy plugin resource {0} for plugin {1}'.format(resource_name, name))
119+ # returning with the web path of the resource
120+ return join('plugins', dir_prefix, resource_name)
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
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
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)