logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

__init__.py (3064B)


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