__init__.py 3.1KB

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