__init__.py 2.9 KB

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