README.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ====================================================
  2. pluggy - A minimalist production ready plugin system
  3. ====================================================
  4. |pypi| |conda-forge| |versions| |github-actions| |gitter| |black| |codecov|
  5. This is the core framework used by the `pytest`_, `tox`_, and `devpi`_ projects.
  6. Please `read the docs`_ to learn more!
  7. A definitive example
  8. ====================
  9. .. code-block:: python
  10. import pluggy
  11. hookspec = pluggy.HookspecMarker("myproject")
  12. hookimpl = pluggy.HookimplMarker("myproject")
  13. class MySpec:
  14. """A hook specification namespace."""
  15. @hookspec
  16. def myhook(self, arg1, arg2):
  17. """My special little hook that you can customize."""
  18. class Plugin_1:
  19. """A hook implementation namespace."""
  20. @hookimpl
  21. def myhook(self, arg1, arg2):
  22. print("inside Plugin_1.myhook()")
  23. return arg1 + arg2
  24. class Plugin_2:
  25. """A 2nd hook implementation namespace."""
  26. @hookimpl
  27. def myhook(self, arg1, arg2):
  28. print("inside Plugin_2.myhook()")
  29. return arg1 - arg2
  30. # create a manager and add the spec
  31. pm = pluggy.PluginManager("myproject")
  32. pm.add_hookspecs(MySpec)
  33. # register plugins
  34. pm.register(Plugin_1())
  35. pm.register(Plugin_2())
  36. # call our ``myhook`` hook
  37. results = pm.hook.myhook(arg1=1, arg2=2)
  38. print(results)
  39. Running this directly gets us::
  40. $ python docs/examples/toy-example.py
  41. inside Plugin_2.myhook()
  42. inside Plugin_1.myhook()
  43. [-1, 3]
  44. .. badges
  45. .. |pypi| image:: https://img.shields.io/pypi/v/pluggy.svg
  46. :target: https://pypi.org/pypi/pluggy
  47. .. |versions| image:: https://img.shields.io/pypi/pyversions/pluggy.svg
  48. :target: https://pypi.org/pypi/pluggy
  49. .. |github-actions| image:: https://github.com/pytest-dev/pluggy/workflows/main/badge.svg
  50. :target: https://github.com/pytest-dev/pluggy/actions
  51. .. |conda-forge| image:: https://img.shields.io/conda/vn/conda-forge/pluggy.svg
  52. :target: https://anaconda.org/conda-forge/pytest
  53. .. |gitter| image:: https://badges.gitter.im/pytest-dev/pluggy.svg
  54. :alt: Join the chat at https://gitter.im/pytest-dev/pluggy
  55. :target: https://gitter.im/pytest-dev/pluggy?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
  56. .. |black| image:: https://img.shields.io/badge/code%20style-black-000000.svg
  57. :target: https://github.com/ambv/black
  58. .. |codecov| image:: https://codecov.io/gh/pytest-dev/pluggy/branch/master/graph/badge.svg
  59. :target: https://codecov.io/gh/pytest-dev/pluggy
  60. :alt: Code coverage Status
  61. .. links
  62. .. _pytest:
  63. http://pytest.org
  64. .. _tox:
  65. https://tox.readthedocs.org
  66. .. _devpi:
  67. http://doc.devpi.net
  68. .. _read the docs:
  69. https://pluggy.readthedocs.io/en/latest/