README.rst 3.0 KB

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