__init__.py 2.5 KB

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