plugins.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. from straight.plugin import load
  9. from pagure.lib.model_base import BASE
  10. def get_plugin_names(blacklist=None, without_backref=False):
  11. """ Return the list of plugins names.
  12. :arg blacklist: name or list of names to not return
  13. :type blacklist: string or list of strings
  14. :arg without_backref: whether or not to include hooks that
  15. have backref "None"
  16. :type without_backref: bool
  17. :return: list of plugin names (strings)
  18. """
  19. from pagure.hooks import BaseHook
  20. plugins = load("pagure.hooks", subclasses=BaseHook)
  21. if not blacklist:
  22. blacklist = []
  23. elif not isinstance(blacklist, list):
  24. blacklist = [blacklist]
  25. output = [
  26. plugin.name
  27. for plugin in plugins
  28. if plugin.name not in blacklist and (plugin.backref or without_backref)
  29. ]
  30. # The default hook is not one we show
  31. if "default" in output:
  32. output.remove("default")
  33. return sorted(output)
  34. def get_plugin_tables():
  35. """ Return the list of all plugins. """
  36. plugins = load("pagure.hooks", subclasses=BASE)
  37. return plugins
  38. def get_plugin(plugin_name):
  39. """ Return the list of plugins names. """
  40. from pagure.hooks import BaseHook
  41. plugins = load("pagure.hooks", subclasses=BaseHook)
  42. for plugin in plugins:
  43. if plugin.name == plugin_name:
  44. return plugin
  45. def get_enabled_plugins(project):
  46. """ Returns a list of plugins enabled for a specific project.
  47. Args:
  48. project (model.Project): The project to look for.
  49. Returns: (list): A list of tuples (pluginclass, dbobj) with the plugin
  50. classess and dbobjects for plugins enabled for the project.
  51. """
  52. from pagure.hooks import BaseHook
  53. enabled = []
  54. for plugin in load("pagure.hooks", subclasses=BaseHook):
  55. if plugin.backref is None:
  56. if plugin.is_enabled_for(project):
  57. enabled.append((plugin, None))
  58. else:
  59. plugin.db_object()
  60. if hasattr(project, plugin.backref):
  61. dbobj = getattr(project, plugin.backref)
  62. if dbobj and dbobj.active:
  63. enabled.append((plugin, dbobj))
  64. return enabled