hook.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. from .named import NamedExtensionManager
  13. class HookManager(NamedExtensionManager):
  14. """Coordinate execution of multiple extensions using a common name.
  15. :param namespace: The namespace for the entry points.
  16. :type namespace: str
  17. :param name: The name of the hooks to load.
  18. :type name: str
  19. :param invoke_on_load: Boolean controlling whether to invoke the
  20. object returned by the entry point after the driver is loaded.
  21. :type invoke_on_load: bool
  22. :param invoke_args: Positional arguments to pass when invoking
  23. the object returned by the entry point. Only used if invoke_on_load
  24. is True.
  25. :type invoke_args: tuple
  26. :param invoke_kwds: Named arguments to pass when invoking
  27. the object returned by the entry point. Only used if invoke_on_load
  28. is True.
  29. :type invoke_kwds: dict
  30. :param on_load_failure_callback: Callback function that will be called when
  31. an entrypoint can not be loaded. The arguments that will be provided
  32. when this is called (when an entrypoint fails to load) are
  33. (manager, entrypoint, exception)
  34. :type on_load_failure_callback: function
  35. :param verify_requirements: Use setuptools to enforce the
  36. dependencies of the plugin(s) being loaded. Defaults to False.
  37. :type verify_requirements: bool
  38. :type on_missing_entrypoints_callback: function
  39. :param warn_on_missing_entrypoint: Flag to control whether failing
  40. to load a plugin is reported via a log mess. Only applies if
  41. on_missing_entrypoints_callback is None.
  42. :type warn_on_missing_entrypoint: bool
  43. """
  44. def __init__(self, namespace, name,
  45. invoke_on_load=False, invoke_args=(), invoke_kwds={},
  46. on_load_failure_callback=None,
  47. verify_requirements=False,
  48. on_missing_entrypoints_callback=None,
  49. # NOTE(dhellmann): This default is different from the
  50. # base class because for hooks it is less likely to
  51. # be an error to have no entry points present.
  52. warn_on_missing_entrypoint=False):
  53. super(HookManager, self).__init__(
  54. namespace,
  55. [name],
  56. invoke_on_load=invoke_on_load,
  57. invoke_args=invoke_args,
  58. invoke_kwds=invoke_kwds,
  59. on_load_failure_callback=on_load_failure_callback,
  60. on_missing_entrypoints_callback=on_missing_entrypoints_callback,
  61. verify_requirements=verify_requirements,
  62. warn_on_missing_entrypoint=warn_on_missing_entrypoint,
  63. )
  64. def _init_attributes(self, namespace, names, name_order=False,
  65. propagate_map_exceptions=False,
  66. on_load_failure_callback=None):
  67. super(HookManager, self)._init_attributes(
  68. namespace, names,
  69. propagate_map_exceptions=propagate_map_exceptions,
  70. on_load_failure_callback=on_load_failure_callback)
  71. self._name = names[0]
  72. def __getitem__(self, name):
  73. """Return the named extensions.
  74. Accessing a HookManager as a dictionary (``em['name']``)
  75. produces a list of the :class:`Extension` instance(s) with the
  76. specified name, in the order they would be invoked by map().
  77. """
  78. if name != self._name:
  79. raise KeyError(name)
  80. return self.extensions