Skip to main content

A wrapper of "pluggy" to support asyncio and context managers

Project description

apluggy

PyPI - Version PyPI - Python Version Test Status Test Status codecov

A wrapper of pluggy to support asyncio and context managers.

This package provides a subclass of pluggy.PluginManager which

  • allows async functions, context managers, and async context managers to be hooks
  • and accepts plugin factories in addition to plugin instances for registration.

Table of Contents


Installation

You can install apluggy with pip:

pip install apluggy

How to use

Here, we show a simple example of how to use apluggy.

We only describe the usage of additional features provided by apluggy. For the usage of pluggy itself, please refer to the pluggy documentation.

Start Python

You can try this example in a Python interpreter.

$ python
Python 3.8.16 (...)
...
...
>>>

Import packages

Import necessary packages of this example.

>>> import asyncio
>>> import apluggy as pluggy
>>> from apluggy import asynccontextmanager, contextmanager

In this example, apluggy is imported with the alias pluggy.

The decorators asynccontextmanager and contextmanager are imported from apluggy. They are wrappers of the decorators of the same names in the contextlib package. The wrappers preserve the signatures of decorated functions, which are necessary for pluggy to pass arguments to hook implementations correctly. (The decorator contextmanger in apluggy is the same object as the decorator contextmanager in the decorator package. The decorator package does not provide asynccontextmanager decorator as of version 5.1. The decorator asynccontextmanger in apluggy is implemented in a similar way as the decorator contextmanager in the decorator package.)

Create hook specification and implementation decorators

>>> hookspec = pluggy.HookspecMarker('project')
>>> hookimpl = pluggy.HookimplMarker('project')

Define hook specifications

In this example, we define three hooks: async function, context manager, and async context manager.

>>> class Spec:
...     """A hook specification namespace."""
...
...     @hookspec
...     async def afunc(self, arg1, arg2):
...         pass
...
...     @hookspec
...     @contextmanager
...     def context(self, arg1, arg2):
...         pass
...
...     @hookspec
...     @asynccontextmanager
...     async def acontext(self, arg1, arg2):
...         pass

Define plugins

We define two plugins as classes. Each plugin implements the three hooks defined above.

>>> class Plugin_1:
...     """A hook implementation namespace."""
...
...     @hookimpl
...     async def afunc(self, arg1, arg2):
...         print('inside Plugin_1.afunc()')
...         return arg1 + arg2
...
...     @hookimpl
...     @contextmanager
...     def context(self, arg1, arg2):
...         print('inside Plugin_1.context()')
...         yield arg1 + arg2
...
...     @hookimpl
...     @asynccontextmanager
...     async def acontext(self, arg1, arg2):
...         print('inside Plugin_1.acontext()')
...         yield arg1 + arg2

>>> class Plugin_2:
...     """A 2nd hook implementation namespace."""
...
...     @hookimpl
...     async def afunc(self, arg1, arg2):
...         print('inside Plugin_2.afunc()')
...         return arg1 - arg2
...
...     @hookimpl
...     @contextmanager
...     def context(self, arg1, arg2):
...         print('inside Plugin_2.context()')
...         yield arg1 - arg2
...
...     @hookimpl
...     @asynccontextmanager
...     async def acontext(self, arg1, arg2):
...         print('inside Plugin_2.acontext()')
...         yield arg1 - arg2

Create a plugin manager and register plugins

Plugins can be registered as instances or factories. In the following example, we register two plugins: Plugin_1 as an instance, and Plugin_2 as a factory.

>>> pm = pluggy.PluginManager('project')
>>> pm.add_hookspecs(Spec)
>>> _ = pm.register(Plugin_1())  # instantiation is optional.
>>> _ = pm.register(Plugin_2)  # callable is considered a plugin factory.

Pluggy accepts a class or module as a plugin. However, it actually accepts a class instance, not a class itself. Consequently, when plugins are loaded with load_setuptools_entrypoints(), the entry points must be class instances or modules. Classes themselves cannot be used as entry points (if understood correctly).

So that classes themselves can be entry points, apluggy accepts a class itself for a plugin registration. When apluggy receives a callable object, apluggy considers the object as a plugin factory.

Call hooks

The following example shows how to call hooks.

Async function

>>> async def call_afunc():
...     results = await pm.ahook.afunc(arg1=1, arg2=2)  # ahook instead of hook
...     print(results)

>>> asyncio.run(call_afunc())
inside Plugin_2.afunc()
inside Plugin_1.afunc()
[-1, 3]

Context manager

>>> with pm.with_.context(arg1=1, arg2=2) as y:  # with_ instead of hook
...     print(y)
inside Plugin_2.context()
inside Plugin_1.context()
[-1, 3]

Async context manager

>>> async def call_acontext():
...     async with pm.awith.acontext(arg1=1, arg2=2) as y:  # awith instead of hook
...         print(y)

>>> asyncio.run(call_acontext())
inside Plugin_2.acontext()
inside Plugin_1.acontext()
[-1, 3]

Links


License

  • apluggy is licensed under the MIT license.

Contact

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

apluggy-0.9.2.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

apluggy-0.9.2-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file apluggy-0.9.2.tar.gz.

File metadata

  • Download URL: apluggy-0.9.2.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for apluggy-0.9.2.tar.gz
Algorithm Hash digest
SHA256 86d7df2c3d815e6f7ff0dffa87b5ddd0858b95188eb7a1c68b54face788373aa
MD5 27654c70833c4bab72a42090bb1953fb
BLAKE2b-256 e2a26252ff8fe1209852d8fd23dc70f1c2880e6205f5d4914ddec4160d420965

See more details on using hashes here.

File details

Details for the file apluggy-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: apluggy-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for apluggy-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 39d995e16a9868da9f7df0f35f607f6590da70bbf6562081d9e664b8e59d7907
MD5 5e2e651667b200c4362d4cdbb01a4f56
BLAKE2b-256 11fa05ee25536655c3728e0ec26ab23b05a59b0de65a95a76ec9d1b4aca6cb3a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page