__init__.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from searx.plugins import self_ip
  2. from searx import logger
  3. from sys import exit
  4. logger = logger.getChild('plugins')
  5. required_attrs = (('name', str),
  6. ('description', str),
  7. ('default_on', bool))
  8. class Plugin():
  9. default_on = False
  10. name = 'Default plugin'
  11. description = 'Default plugin description'
  12. class PluginStore():
  13. def __init__(self):
  14. self.plugins = []
  15. def __iter__(self):
  16. for plugin in self.plugins:
  17. yield plugin
  18. def register(self, *plugins):
  19. for plugin in plugins:
  20. for plugin_attr, plugin_attr_type in required_attrs:
  21. if not hasattr(plugin, plugin_attr) or not isinstance(getattr(plugin, plugin_attr), plugin_attr_type):
  22. logger.critical('missing attribute "{0}", cannot load plugin: {1}'.format(plugin_attr, plugin))
  23. exit(3)
  24. plugin.id = plugin.name.replace(' ', '_')
  25. self.plugins.append(plugin)
  26. def call(self, plugin_type, request, *args, **kwargs):
  27. ret = True
  28. for plugin in request.user_plugins:
  29. if hasattr(plugin, plugin_type):
  30. ret = getattr(plugin, plugin_type)(request, *args, **kwargs)
  31. if not ret:
  32. break
  33. return ret
  34. plugins = PluginStore()
  35. plugins.register(self_ip)