dispatch.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 .enabled import EnabledExtensionManager
  14. from .exception import NoMatches
  15. LOG = logging.getLogger(__name__)
  16. class DispatchExtensionManager(EnabledExtensionManager):
  17. """Loads all plugins and filters on execution.
  18. This is useful for long-running processes that need to pass
  19. different inputs to different extensions.
  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
  24. :param invoke_on_load: Boolean controlling whether to invoke the
  25. object returned by the entry point after the driver is loaded.
  26. :type invoke_on_load: bool
  27. :param invoke_args: Positional arguments to pass when invoking
  28. the object returned by the entry point. Only used if invoke_on_load
  29. is True.
  30. :type invoke_args: tuple
  31. :param invoke_kwds: Named arguments to pass when invoking
  32. the object returned by the entry point. Only used if invoke_on_load
  33. is True.
  34. :type invoke_kwds: dict
  35. :param propagate_map_exceptions: Boolean controlling whether exceptions
  36. are propagated up through the map call or whether they are logged and
  37. then ignored
  38. :type invoke_on_load: bool
  39. """
  40. def map(self, filter_func, func, *args, **kwds):
  41. """Iterate over the extensions invoking func() for any where
  42. filter_func() returns True.
  43. The signature of filter_func() should be::
  44. def filter_func(ext, *args, **kwds):
  45. pass
  46. The first argument to filter_func(), 'ext', is the
  47. :class:`~stevedore.extension.Extension`
  48. instance. filter_func() should return True if the extension
  49. should be invoked for the input arguments.
  50. The signature for func() should be::
  51. def func(ext, *args, **kwds):
  52. pass
  53. The first argument to func(), 'ext', is the
  54. :class:`~stevedore.extension.Extension` instance.
  55. Exceptions raised from within func() are propagated up and
  56. processing stopped if self.propagate_map_exceptions is True,
  57. otherwise they are logged and ignored.
  58. :param filter_func: Callable to test each extension.
  59. :param func: Callable to invoke for each extension.
  60. :param args: Variable arguments to pass to func()
  61. :param kwds: Keyword arguments to pass to func()
  62. :returns: List of values returned from func()
  63. """
  64. if not self.extensions:
  65. # FIXME: Use a more specific exception class here.
  66. raise NoMatches('No %s extensions found' % self.namespace)
  67. response = []
  68. for e in self.extensions:
  69. if filter_func(e, *args, **kwds):
  70. self._invoke_one_plugin(response.append, func, e, args, kwds)
  71. return response
  72. def map_method(self, filter_func, method_name, *args, **kwds):
  73. """Iterate over the extensions invoking each one's object method called
  74. `method_name` for any where filter_func() returns True.
  75. This is equivalent of using :meth:`map` with func set to
  76. `lambda x: x.obj.method_name()`
  77. while being more convenient.
  78. Exceptions raised from within the called method are propagated up
  79. and processing stopped if self.propagate_map_exceptions is True,
  80. otherwise they are logged and ignored.
  81. .. versionadded:: 0.12
  82. :param filter_func: Callable to test each extension.
  83. :param method_name: The extension method name to call
  84. for each extension.
  85. :param args: Variable arguments to pass to method
  86. :param kwds: Keyword arguments to pass to method
  87. :returns: List of values returned from methods
  88. """
  89. return self.map(filter_func, self._call_extension_method,
  90. method_name, *args, **kwds)
  91. class NameDispatchExtensionManager(DispatchExtensionManager):
  92. """Loads all plugins and filters on execution.
  93. This is useful for long-running processes that need to pass
  94. different inputs to different extensions and can predict the name
  95. of the extensions before calling them.
  96. The check_func argument should return a boolean, with ``True``
  97. indicating that the extension should be loaded and made available
  98. and ``False`` indicating that the extension should be ignored.
  99. :param namespace: The namespace for the entry points.
  100. :type namespace: str
  101. :param check_func: Function to determine which extensions to load.
  102. :type check_func: callable
  103. :param invoke_on_load: Boolean controlling whether to invoke the
  104. object returned by the entry point after the driver is loaded.
  105. :type invoke_on_load: bool
  106. :param invoke_args: Positional arguments to pass when invoking
  107. the object returned by the entry point. Only used if invoke_on_load
  108. is True.
  109. :type invoke_args: tuple
  110. :param invoke_kwds: Named arguments to pass when invoking
  111. the object returned by the entry point. Only used if invoke_on_load
  112. is True.
  113. :type invoke_kwds: dict
  114. :param propagate_map_exceptions: Boolean controlling whether exceptions
  115. are propagated up through the map call or whether they are logged and
  116. then ignored
  117. :type invoke_on_load: bool
  118. :param on_load_failure_callback: Callback function that will be called when
  119. an entrypoint can not be loaded. The arguments that will be provided
  120. when this is called (when an entrypoint fails to load) are
  121. (manager, entrypoint, exception)
  122. :type on_load_failure_callback: function
  123. :param verify_requirements: Use setuptools to enforce the
  124. dependencies of the plugin(s) being loaded. Defaults to False.
  125. :type verify_requirements: bool
  126. """
  127. def __init__(self, namespace, check_func, invoke_on_load=False,
  128. invoke_args=(), invoke_kwds={},
  129. propagate_map_exceptions=False,
  130. on_load_failure_callback=None,
  131. verify_requirements=False):
  132. super(NameDispatchExtensionManager, self).__init__(
  133. namespace=namespace,
  134. check_func=check_func,
  135. invoke_on_load=invoke_on_load,
  136. invoke_args=invoke_args,
  137. invoke_kwds=invoke_kwds,
  138. propagate_map_exceptions=propagate_map_exceptions,
  139. on_load_failure_callback=on_load_failure_callback,
  140. verify_requirements=verify_requirements,
  141. )
  142. def _init_plugins(self, extensions):
  143. super(NameDispatchExtensionManager, self)._init_plugins(extensions)
  144. self.by_name = dict((e.name, e) for e in self.extensions)
  145. def map(self, names, func, *args, **kwds):
  146. """Iterate over the extensions invoking func() for any where
  147. the name is in the given list of names.
  148. The signature for func() should be::
  149. def func(ext, *args, **kwds):
  150. pass
  151. The first argument to func(), 'ext', is the
  152. :class:`~stevedore.extension.Extension` instance.
  153. Exceptions raised from within func() are propagated up and
  154. processing stopped if self.propagate_map_exceptions is True,
  155. otherwise they are logged and ignored.
  156. :param names: List or set of name(s) of extension(s) to invoke.
  157. :param func: Callable to invoke for each extension.
  158. :param args: Variable arguments to pass to func()
  159. :param kwds: Keyword arguments to pass to func()
  160. :returns: List of values returned from func()
  161. """
  162. response = []
  163. for name in names:
  164. try:
  165. e = self.by_name[name]
  166. except KeyError:
  167. LOG.debug('Missing extension %r being ignored', name)
  168. else:
  169. self._invoke_one_plugin(response.append, func, e, args, kwds)
  170. return response
  171. def map_method(self, names, method_name, *args, **kwds):
  172. """Iterate over the extensions invoking each one's object method called
  173. `method_name` for any where the name is in the given list of names.
  174. This is equivalent of using :meth:`map` with func set to
  175. `lambda x: x.obj.method_name()`
  176. while being more convenient.
  177. Exceptions raised from within the called method are propagated up
  178. and processing stopped if self.propagate_map_exceptions is True,
  179. otherwise they are logged and ignored.
  180. .. versionadded:: 0.12
  181. :param names: List or set of name(s) of extension(s) to invoke.
  182. :param method_name: The extension method name
  183. to call for each extension.
  184. :param args: Variable arguments to pass to method
  185. :param kwds: Keyword arguments to pass to method
  186. :returns: List of values returned from methods
  187. """
  188. return self.map(names, self._call_extension_method,
  189. method_name, *args, **kwds)