Metadata-Version: 2.5
Name: plushy
Version: 0.1.1
Summary: Typed plugins for Python
Author-email: Stefane Fermigier <sf@abilian.com>
License: Apache-2.0
License-File: LICENSE
Requires-Python: >=3.11
Requires-Dist: pluggy>=1.5
Provides-Extra: dishka
Description-Content-Type: text/markdown

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="docs/src/assets/lockup-tagline-dark.svg">
  <img alt="Plushy: Typed plugins for Python" src="docs/src/assets/lockup-tagline.svg" width="340">
</picture>

Plushy is a plugin framework for Python applications. It does four jobs:

- **Discover** plugins shipped as installed packages, from entry points or a build-time index, without importing them until they are loaded.
- **Load** them with a predictable lifecycle: declared requirements, teardown on unload, per-plugin error isolation.
- **Dispatch** to them through typed hooks, sync and async.
- **Gate** them with `typing.Protocol` contracts and host-controlled enable/disable.

[Pluggy](https://github.com/pytest-dev/pluggy) is embedded as the sync dispatch engine. Discovery, lifecycle, contracts, enablement and the async caller are Plushy's own.

## Install

```bash
uv add plushy
```

Requires Python 3.11+. Installs the `plushy` library and the `plushy index` command. Plushy is not on PyPI yet; until it is, see [Installation](docs/src/get-started/installation.md).

## Hooks

```python
from plushy import Plugin, PluginManager, hookimpl, hookspec

class Spec:
    @hookspec
    def render_block(self, block: dict) -> str | None: ...

class Charts(Plugin):
    namespace = "acme.renderers"
    name = "charts"

    @hookimpl
    def render_block(self, block):
        if block["type"] == "chart":
            return f"<figure data-chart='{block['spec']}'></figure>"
        return None

pm = PluginManager("acme.renderers", hookspec=Spec)
pm.register(Charts)
pm.load_all()

pm.hook.render_block(block={"type": "chart", "spec": "x"})
# ["<figure data-chart='x'></figure>"]

# in async hosts: await pm.hook.render_block.aio(block={"type": "chart", "spec": "x"})
```

A plugin's hookimpls take part in dispatch only while it is loaded and enabled. `pm.disable("charts")` takes it out at once; `pm.enable("charts")` puts it back without reloading it.

## Distributing a plugin

Instead of calling `register()`, a plugin package declares an entry point in the namespace's group:

```toml
[project.entry-points."acme.renderers"]
charts = "acme_charts:Charts"
```

Any host running `PluginManager("acme.renderers")` finds it after installation, with no change to the host.

## Lifecycle: requirements and teardown

`load()` may return a value (pull-style: `backend = pm.load("s3")`), or be a generator whose code after the `yield` runs when the plugin is unloaded. `requires` names plugins to load first.

<!-- docs-test: skip -->
```python
class Db(Plugin):
    namespace = "acme.app"
    name = "db"

    def load(self):
        conn = connect()
        yield conn          # the load result
        conn.close()        # runs on pm.unload("db") or pm.refresh()

class Cache(Plugin):
    namespace = "acme.app"
    name = "cache"
    requires = ("db",)      # loaded first; cycles raise

    def load(self):
        ...
```

`load_all()` loads in name order (requirements pulled forward), isolates failures, and keeps each one: `pm.error(name)` returns the exception.

## Documentation

The documentation site is built from `docs/` with Zensical (`cd docs && make serve`):

- [Quick Start](docs/src/get-started/quickstart.md): a host and a plugin package in five minutes.
- [Guides](docs/src/guides/index.md): writing plugins, hooks, async, enablement, contracts, errors, discovery, testing.
- [Python API](docs/src/reference/api.md) and [Known Limitations](docs/src/reference/limitations.md).
- [Pluggy vs. Plushy](docs/src/design/pluggy-vs-plushy.md): what Plushy adds, and when Pluggy alone is enough.

## Status

0.1.0, the first release. Tested on Python 3.11 to 3.15. Before 1.0, minor releases may change the API; the changelog says how. Known limits: no async hook wrappers, no filesystem watching, no dependency-injection container (instantiation goes through a single factory reserved for one).

## License

Apache License 2.0. See [LICENSE](LICENSE).
