137 words
1 minute
Registering new Datasette plugin hooks by defining them in other plugins
Anubhav Gain
2024-08-13

Registering new Datasette plugin hooks by defining them in other plugins#

I’m experimenting with a Datasette plugin that itself adds new plugin hooks which other plugins can then interact with.

It’s called datasette-low-disk-space-hook, and it adds a new plugin hook called low_disk_space(datasette), defined in the datasette_low_disk_space_hook/hookspecs.py module.

The hook is registered by this code in datasette_low_disk_space_hook/__init__.py

from datasette.utils import await_me_maybe
from datasette.plugins import pm
from . import hookspecs
pm.add_hookspecs(hookspecs)

This imports the plugin manager directly from Datasette and uses it to add the new hooks.

I was worried that the pm.add_hookspects(hookspecs) line was not guaranteed to be executed if that module had not been imported.

It turns out that having this entrpoints= line in setup.py is enough to ensure that the module is imported and the pm.add_hookspecs() line is executed:

from setuptools import setup
setup(
name="datasette-low-disk-space-hook",
# ...
entry_points={"datasette": ["low_disk_space_hook = datasette_low_disk_space_hook"]},
# ...
)
Registering new Datasette plugin hooks by defining them in other plugins
https://mranv.pages.dev/posts/registering-new-datasette-plugin-hooks-by-defining-them-in-other-plugins/
Author
Anubhav Gain
Published at
2024-08-13
License
CC BY-NC-SA 4.0