enabled.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import logging
  13. from .extension import ExtensionManager
  14. LOG = logging.getLogger(__name__)
  15. class EnabledExtensionManager(ExtensionManager):
  16. """Loads only plugins that pass a check function.
  17. The check_func argument should return a boolean, with ``True``
  18. indicating that the extension should be loaded and made available
  19. and ``False`` indicating that the extension should be ignored.
  20. :param namespace: The namespace for the entry points.
  21. :type namespace: str
  22. :param check_func: Function to determine which extensions to load.
  23. :type check_func: callable, taking an :class:`Extension`
  24. instance as argument
  25. :param invoke_on_load: Boolean controlling whether to invoke the
  26. object returned by the entry point after the driver is loaded.
  27. :type invoke_on_load: bool
  28. :param invoke_args: Positional arguments to pass when invoking
  29. the object returned by the entry point. Only used if invoke_on_load
  30. is True.
  31. :type invoke_args: tuple
  32. :param invoke_kwds: Named arguments to pass when invoking
  33. the object returned by the entry point. Only used if invoke_on_load
  34. is True.
  35. :type invoke_kwds: dict
  36. :param propagate_map_exceptions: Boolean controlling whether exceptions
  37. are propagated up through the map call or whether they are logged and
  38. then ignored
  39. :type propagate_map_exceptions: bool
  40. :param on_load_failure_callback: Callback function that will be called when
  41. an entrypoint can not be loaded. The arguments that will be provided
  42. when this is called (when an entrypoint fails to load) are
  43. (manager, entrypoint, exception)
  44. :type on_load_failure_callback: function
  45. :param verify_requirements: Use setuptools to enforce the
  46. dependencies of the plugin(s) being loaded. Defaults to False.
  47. :type verify_requirements: bool
  48. """
  49. def __init__(self, namespace, check_func, invoke_on_load=False,
  50. invoke_args=(), invoke_kwds={},
  51. propagate_map_exceptions=False,
  52. on_load_failure_callback=None,
  53. verify_requirements=False,):
  54. self.check_func = check_func
  55. super(EnabledExtensionManager, self).__init__(
  56. namespace,
  57. invoke_on_load=invoke_on_load,
  58. invoke_args=invoke_args,
  59. invoke_kwds=invoke_kwds,
  60. propagate_map_exceptions=propagate_map_exceptions,
  61. on_load_failure_callback=on_load_failure_callback,
  62. verify_requirements=verify_requirements,
  63. )
  64. def _load_one_plugin(self, ep, invoke_on_load, invoke_args, invoke_kwds,
  65. verify_requirements):
  66. ext = super(EnabledExtensionManager, self)._load_one_plugin(
  67. ep, invoke_on_load, invoke_args, invoke_kwds,
  68. verify_requirements,
  69. )
  70. if ext and not self.check_func(ext):
  71. LOG.debug('ignoring extension %r', ep.name)
  72. return None
  73. return ext