plugin.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Cement core plugins module."""
  2. from ..core import backend, exc, interface, handler
  3. from ..utils.misc import minimal_logger
  4. LOG = minimal_logger(__name__)
  5. def plugin_validator(klass, obj):
  6. """Validates an handler implementation against the IPlugin interface."""
  7. members = [
  8. '_setup',
  9. 'load_plugin',
  10. 'load_plugins',
  11. 'get_loaded_plugins',
  12. 'get_enabled_plugins',
  13. 'get_disabled_plugins',
  14. ]
  15. interface.validate(IPlugin, obj, members)
  16. class IPlugin(interface.Interface):
  17. """
  18. This class defines the Plugin Handler Interface. Classes that
  19. implement this handler must provide the methods and attributes defined
  20. below.
  21. Implementations do *not* subclass from interfaces.
  22. Usage:
  23. .. code-block:: python
  24. from cement.core import plugin
  25. class MyPluginHandler(object):
  26. class Meta:
  27. interface = plugin.IPlugin
  28. label = 'my_plugin_handler'
  29. ...
  30. """
  31. # pylint: disable=W0232, C0111, R0903
  32. class IMeta:
  33. label = 'plugin'
  34. validator = plugin_validator
  35. # Must be provided by the implementation
  36. Meta = interface.Attribute('Handler meta-data')
  37. def _setup(app_obj):
  38. """
  39. The _setup function is called during application initialization and
  40. must 'setup' the handler object making it ready for the framework
  41. or the application to make further calls to it.
  42. :param app_obj: The application object.
  43. """
  44. def load_plugin(plugin_name):
  45. """
  46. Load a plugin whose name is 'plugin_name'.
  47. :param plugin_name: The name of the plugin to load.
  48. """
  49. def load_plugins(plugin_list):
  50. """
  51. Load all plugins from plugin_list.
  52. :param plugin_list: A list of plugin names to load.
  53. """
  54. def get_loaded_plugins():
  55. """Returns a list of plugins that have been loaded."""
  56. def get_enabled_plugins():
  57. """Returns a list of plugins that are enabled in the config."""
  58. def get_disabled_plugins():
  59. """Returns a list of plugins that are disabled in the config."""
  60. class CementPluginHandler(handler.CementBaseHandler):
  61. """
  62. Base class that all Plugin Handlers should sub-class from.
  63. """
  64. class Meta:
  65. """
  66. Handler meta-data (can be passed as keyword arguments to the parent
  67. class).
  68. """
  69. label = None
  70. """The string identifier of this handler."""
  71. interface = IPlugin
  72. """The interface that this class implements."""
  73. def __init__(self, *args, **kw):
  74. super(CementPluginHandler, self).__init__(*args, **kw)