__init__.py 2.8KB

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