A wrapper of "pluggy" to support asyncio and context managers
Project description
apluggy
A wrapper of pluggy to support asyncio and context managers.
This package provides a subclass of
pluggy.PluginManager
that
- allows async functions, context managers, and async context managers to be hooks
- and accepts plugin factories in addition to plugin instances for registration.
The package also provides asynccontextmanager
decorator, which is a wrapper
of
contextlib.asynccontextmanager
to preserve the signature of the decorated function. This decorator is
implemented in the same way as
contextmanager
from the decorator package is
implemented.
Table of Contents
Installation
pip install apluggy
How to use
Import packages
>>> import asyncio
>>> import apluggy as pluggy
>>> from apluggy import asynccontextmanager, contextmanager
(contextmanager
is imported from the decorator package.)
Create hook specification and implementation decorators
>>> hookspec = pluggy.HookspecMarker('project')
>>> hookimpl = pluggy.HookimplMarker('project')
(These makers are imported from the pluggy package.)
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.
Call hooks
The following example shows how to call hooks.
>>> 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]
>>> 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 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file apluggy-0.0.3.tar.gz
.
File metadata
- Download URL: apluggy-0.0.3.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39a62d5bb55f355b670e0225d5745495cde123130f1048e9c19a704a9f0ce164 |
|
MD5 | d95854b05a8d11cd8a46a053b2842928 |
|
BLAKE2b-256 | 577f669aef9a19d9892f23f3c0c55796198d10a8515418c35e9b5ede62289640 |
File details
Details for the file apluggy-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: apluggy-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70ba1d663d72047e2d74d52e6b469cf41ccb4d6bbeb204f604033e74919efa77 |
|
MD5 | 72c9fe4a3bd65070e170abbba4b9a0e4 |
|
BLAKE2b-256 | 5d12a237b4c5e2a93acbc902eae08ce9691f1ab3558190b743072c450e75a91e |