__init__.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. '''
  2. searx is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Affero General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. searx is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Affero General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with searx. If not, see < http://www.gnu.org/licenses/ >.
  12. (C) 2015 by Adam Tauber, <asciimoo@gmail.com>
  13. '''
  14. from sys import exit
  15. from searx import logger
  16. logger = logger.getChild('plugins')
  17. from searx.plugins import (doai_rewrite,
  18. https_rewrite,
  19. open_results_on_new_tab,
  20. self_info,
  21. search_on_category_select,
  22. tracker_url_remover,
  23. vim_hotkeys)
  24. required_attrs = (('name', (str, unicode)),
  25. ('description', (str, unicode)),
  26. ('default_on', bool))
  27. optional_attrs = (('js_dependencies', tuple),
  28. ('css_dependencies', tuple))
  29. class Plugin():
  30. default_on = False
  31. name = 'Default plugin'
  32. description = 'Default plugin description'
  33. class PluginStore():
  34. def __init__(self):
  35. self.plugins = []
  36. def __iter__(self):
  37. for plugin in self.plugins:
  38. yield plugin
  39. def register(self, *plugins):
  40. for plugin in plugins:
  41. for plugin_attr, plugin_attr_type in required_attrs:
  42. if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
  43. logger.critical('missing attribute "{0}", cannot load plugin: {1}'.format(plugin_attr, plugin))
  44. exit(3)
  45. for plugin_attr, plugin_attr_type in optional_attrs:
  46. if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
  47. setattr(plugin, plugin_attr, plugin_attr_type())
  48. plugin.id = plugin.name.replace(' ', '_')
  49. self.plugins.append(plugin)
  50. def call(self, plugin_type, request, *args, **kwargs):
  51. ret = True
  52. for plugin in request.user_plugins:
  53. if hasattr(plugin, plugin_type):
  54. ret = getattr(plugin, plugin_type)(request, *args, **kwargs)
  55. if not ret:
  56. break
  57. return ret
  58. plugins = PluginStore()
  59. plugins.register(doai_rewrite)
  60. plugins.register(https_rewrite)
  61. plugins.register(open_results_on_new_tab)
  62. plugins.register(self_info)
  63. plugins.register(search_on_category_select)
  64. plugins.register(tracker_url_remover)
  65. plugins.register(vim_hotkeys)