__init__.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 (https_rewrite,
  18. open_results_on_new_tab,
  19. self_info,
  20. search_on_category_select,
  21. tracker_url_remover)
  22. required_attrs = (('name', str),
  23. ('description', str),
  24. ('default_on', bool))
  25. optional_attrs = (('js_dependencies', tuple),
  26. ('css_dependencies', tuple))
  27. class Plugin():
  28. default_on = False
  29. name = 'Default plugin'
  30. description = 'Default plugin description'
  31. class PluginStore():
  32. def __init__(self):
  33. self.plugins = []
  34. def __iter__(self):
  35. for plugin in self.plugins:
  36. yield plugin
  37. def register(self, *plugins):
  38. for plugin in plugins:
  39. for plugin_attr, plugin_attr_type in required_attrs:
  40. if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
  41. logger.critical('missing attribute "{0}", cannot load plugin: {1}'.format(plugin_attr, plugin))
  42. exit(3)
  43. for plugin_attr, plugin_attr_type in optional_attrs:
  44. if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
  45. setattr(plugin, plugin_attr, plugin_attr_type())
  46. plugin.id = plugin.name.replace(' ', '_')
  47. self.plugins.append(plugin)
  48. def call(self, plugin_type, request, *args, **kwargs):
  49. ret = True
  50. for plugin in request.user_plugins:
  51. if hasattr(plugin, plugin_type):
  52. ret = getattr(plugin, plugin_type)(request, *args, **kwargs)
  53. if not ret:
  54. break
  55. return ret
  56. plugins = PluginStore()
  57. plugins.register(https_rewrite)
  58. plugins.register(open_results_on_new_tab)
  59. plugins.register(self_info)
  60. plugins.register(search_on_category_select)
  61. plugins.register(tracker_url_remover)