{"id":18620498,"url":"https://github.com/simonsobs/apluggy","last_synced_at":"2025-08-20T19:08:17.717Z","repository":{"id":65949024,"uuid":"603187197","full_name":"simonsobs/apluggy","owner":"simonsobs","description":"A wrapper of \"pluggy\" to support asyncio and context managers","archived":false,"fork":false,"pushed_at":"2025-08-12T13:26:36.000Z","size":469,"stargazers_count":11,"open_issues_count":3,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-08-12T14:29:25.365Z","etag":null,"topics":["asyncio","nextline","pluggy"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simonsobs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-02-17T19:58:39.000Z","updated_at":"2025-08-12T12:16:47.000Z","dependencies_parsed_at":"2024-01-18T19:25:46.557Z","dependency_job_id":"46b00a00-33dd-439b-88b4-5e899250ae1a","html_url":"https://github.com/simonsobs/apluggy","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"purl":"pkg:github/simonsobs/apluggy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonsobs%2Fapluggy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonsobs%2Fapluggy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonsobs%2Fapluggy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonsobs%2Fapluggy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonsobs","download_url":"https://codeload.github.com/simonsobs/apluggy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonsobs%2Fapluggy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271369768,"owners_count":24747800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","nextline","pluggy"],"created_at":"2024-11-07T04:06:36.543Z","updated_at":"2025-08-20T19:08:17.645Z","avatar_url":"https://github.com/simonsobs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# apluggy\n\n[![PyPI - Version](https://img.shields.io/pypi/v/apluggy.svg)](https://pypi.org/project/apluggy)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/apluggy.svg)](https://pypi.org/project/apluggy)\n\n[![Test Status](https://github.com/simonsobs/apluggy/actions/workflows/unit-test.yml/badge.svg)](https://github.com/simonsobs/apluggy/actions/workflows/unit-test.yml)\n[![Test Status](https://github.com/simonsobs/apluggy/actions/workflows/type-check.yml/badge.svg)](https://github.com/simonsobs/apluggy/actions/workflows/type-check.yml)\n[![codecov](https://codecov.io/gh/simonsobs/apluggy/branch/main/graph/badge.svg)](https://codecov.io/gh/simonsobs/apluggy)\n\nA wrapper of [pluggy](https://pluggy.readthedocs.io/) to support asyncio and context managers.\n\nThis package provides a subclass of\n[`pluggy.PluginManager`](https://pluggy.readthedocs.io/en/stable/api_reference.html#pluggy.PluginManager)\nwhich\n\n- allows async functions, context managers, and async context managers to be hooks\n- and accepts plugin factories in addition to plugin instances for registration.\n\n---\n\n**Table of Contents**\n\n- [Installation](#installation)\n- [How to use](#how-to-use)\n  - [Start Python](#start-python)\n  - [Import packages](#import-packages)\n  - [Create hook specification and implementation decorators](#create-hook-specification-and-implementation-decorators)\n  - [Define hook specifications](#define-hook-specifications)\n  - [Define plugins](#define-plugins)\n  - [Create a plugin manager and register plugins](#create-a-plugin-manager-and-register-plugins)\n  - [Call hooks](#call-hooks)\n    - [Async function](#async-function)\n    - [Context manager](#context-manager)\n    - [Async context manager](#async-context-manager)\n- [Links](#links)\n- [License](#license)\n\n---\n\n## Installation\n\nYou can install apluggy with pip:\n\n```console\npip install apluggy\n```\n\n---\n\n## How to use\n\nHere, we show a simple example of how to use apluggy.\n\nWe only describe the usage of additional features provided by apluggy. For the\nusage of pluggy itself, please refer to the [pluggy\ndocumentation](https://pluggy.readthedocs.io/).\n\n### Start Python\n\nYou can try this example in a Python interpreter.\n\n```console\n$ python\nPython 3.10.13 (...)\n...\n...\n\u003e\u003e\u003e\n```\n\n### Import packages\n\nImport necessary packages of this example.\n\n```python\n\u003e\u003e\u003e import asyncio\n\u003e\u003e\u003e from contextlib import asynccontextmanager, contextmanager\n\u003e\u003e\u003e import apluggy as pluggy\n\n```\n\nIn this example, `apluggy` is imported with the alias `pluggy`.\n\n### Create hook specification and implementation decorators\n\n```python\n\u003e\u003e\u003e hookspec = pluggy.HookspecMarker('project')\n\u003e\u003e\u003e hookimpl = pluggy.HookimplMarker('project')\n\n```\n\n### Define hook specifications\n\nIn this example, we define three hooks: async function, context manager, and\nasync context manager.\n\n```python\n\u003e\u003e\u003e class Spec:\n...     \"\"\"A hook specification namespace.\"\"\"\n...\n...     @hookspec\n...     async def afunc(self, arg1, arg2):\n...         pass\n...\n...     @hookspec\n...     @contextmanager\n...     def context(self, arg1, arg2):\n...         pass\n...\n...     @hookspec\n...     @asynccontextmanager\n...     async def acontext(self, arg1, arg2):\n...         pass\n\n```\n\n### Define plugins\n\nWe define two plugins as classes. Each plugin implements the three hooks\ndefined above.\n\n```python\n\u003e\u003e\u003e class Plugin_1:\n...     \"\"\"A hook implementation namespace.\"\"\"\n...\n...     @hookimpl\n...     async def afunc(self, arg1, arg2):\n...         print('inside Plugin_1.afunc()')\n...         return arg1 + arg2\n...\n...     @hookimpl\n...     @contextmanager\n...     def context(self, arg1, arg2):\n...         print('inside Plugin_1.context(): before')\n...         yield arg1 + arg2\n...         print('inside Plugin_1.context(): after')\n...\n...     @hookimpl\n...     @asynccontextmanager\n...     async def acontext(self, arg1, arg2):\n...         print('inside Plugin_1.acontext(): before')\n...         yield arg1 + arg2\n...         print('inside Plugin_1.acontext(): after')\n\n\u003e\u003e\u003e class Plugin_2:\n...     \"\"\"A 2nd hook implementation namespace.\"\"\"\n...\n...     @hookimpl\n...     async def afunc(self, arg1, arg2):\n...         print('inside Plugin_2.afunc()')\n...         return arg1 - arg2\n...\n...     @hookimpl\n...     @contextmanager\n...     def context(self, arg1, arg2):\n...         print('inside Plugin_2.context(): before')\n...         yield arg1 - arg2\n...         print('inside Plugin_2.context(): after')\n...\n...     @hookimpl\n...     @asynccontextmanager\n...     async def acontext(self, arg1, arg2):\n...         print('inside Plugin_2.acontext(): before')\n...         yield arg1 - arg2\n...         print('inside Plugin_2.acontext(): after')\n\n```\n\n### Create a plugin manager and register plugins\n\nPlugins can be registered as instances or factories. In the following\nexample, we register two plugins: `Plugin_1` as an instance, and `Plugin_2`\nas a factory.\n\n```python\n\u003e\u003e\u003e pm = pluggy.PluginManager('project')\n\u003e\u003e\u003e pm.add_hookspecs(Spec)\n\u003e\u003e\u003e _ = pm.register(Plugin_1())  # instantiation is optional.\n\u003e\u003e\u003e _ = pm.register(Plugin_2)  # callable is considered a plugin factory.\n\n```\n\n[Pluggy accepts a class or\nmodule](https://pluggy.readthedocs.io/en/stable/#define-and-collect-hooks) as a\nplugin. However, it actually accepts a class instance, not a class itself.\nConsequently, when plugins are loaded with\n[`load_setuptools_entrypoints()`](https://pluggy.readthedocs.io/en/stable/api_reference.html#pluggy.PluginManager.load_setuptools_entrypoints),\nthe entry points must be class instances or modules. Classes themselves cannot\nbe used as entry points (if understood correctly).\n\nSo that classes themselves can be entry points, apluggy accepts a class itself for\na plugin registration. When apluggy receives a callable object, apluggy considers\nthe object as a plugin factory.\n\n### Call hooks\n\nThe following example shows how to call hooks.\n\n#### Async function\n\n```python\n\u003e\u003e\u003e async def call_afunc():\n...     results = await pm.ahook.afunc(arg1=1, arg2=2)  # ahook instead of hook\n...     print(results)\n\n\u003e\u003e\u003e asyncio.run(call_afunc())\ninside Plugin_2.afunc()\ninside Plugin_1.afunc()\n[-1, 3]\n\n```\n\n#### Context manager\n\n```python\n\u003e\u003e\u003e with pm.with_.context(arg1=1, arg2=2) as y:  # with_ instead of hook\n...     print(y)\ninside Plugin_2.context(): before\ninside Plugin_1.context(): before\n[-1, 3]\ninside Plugin_1.context(): after\ninside Plugin_2.context(): after\n\n```\n\nIn the reverse order:\n\n```python\n\u003e\u003e\u003e with pm.with_reverse.context(arg1=1, arg2=2) as y:  # with_reverse instead of hook\n...     print(y)\ninside Plugin_1.context(): before\ninside Plugin_2.context(): before\n[3, -1]\ninside Plugin_2.context(): after\ninside Plugin_1.context(): after\n\n```\n\n#### Async context manager\n\n```python\n\u003e\u003e\u003e async def call_acontext():\n...     async with pm.awith.acontext(arg1=1, arg2=2) as y:  # awith instead of hook\n...         print(y)\n\n\u003e\u003e\u003e asyncio.run(call_acontext())\ninside Plugin_2.acontext(): before\ninside Plugin_1.acontext(): before\n[-1, 3]\ninside Plugin_1.acontext(): after\ninside Plugin_2.acontext(): after\n\n```\n\nIn the reverse order:\n\n```python\n\u003e\u003e\u003e async def call_acontext():\n...     async with pm.awith_reverse.acontext(arg1=1, arg2=2) as y:  # awith_reverse instead of hook\n...         print(y)\n\n\u003e\u003e\u003e asyncio.run(call_acontext())\ninside Plugin_1.acontext(): before\ninside Plugin_2.acontext(): before\n[3, -1]\ninside Plugin_2.acontext(): after\ninside Plugin_1.acontext(): after\n\n```\n\n---\n\n## Links\n\n- [pluggy](https://pluggy.readthedocs.io/)\n\n---\n\n## License\n\n- _apluggy_ is licensed under the [MIT](https://spdx.org/licenses/MIT.html) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonsobs%2Fapluggy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonsobs%2Fapluggy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonsobs%2Fapluggy/lists"}