/-/plugins now shows details of hooks, closes #794

Also added /-/plugins?all=1 parameter to see default plugins.
This commit is contained in:
Simon Willison 2020-06-02 14:49:28 -07:00
commit 69aa0277f5
4 changed files with 64 additions and 12 deletions

View file

@ -626,9 +626,9 @@ class Datasette:
}, },
} }
def _plugins(self, show_all=False): def _plugins(self, request):
ps = list(get_plugins()) ps = list(get_plugins())
if not show_all: if not request.args.get("all"):
ps = [p for p in ps if p["name"] not in DEFAULT_PLUGINS] ps = [p for p in ps if p["name"] not in DEFAULT_PLUGINS]
return [ return [
{ {
@ -636,6 +636,7 @@ class Datasette:
"static": p["static_path"] is not None, "static": p["static_path"] is not None,
"templates": p["templates_path"] is not None, "templates": p["templates_path"] is not None,
"version": p.get("version"), "version": p.get("version"),
"hooks": p["hooks"],
} }
for p in ps for p in ps
] ]
@ -823,7 +824,9 @@ class Datasette:
r"/-/versions(?P<as_format>(\.json)?)$", r"/-/versions(?P<as_format>(\.json)?)$",
) )
add_route( add_route(
JsonDataView.as_asgi(self, "plugins.json", self._plugins), JsonDataView.as_asgi(
self, "plugins.json", self._plugins, needs_request=True
),
r"/-/plugins(?P<as_format>(\.json)?)$", r"/-/plugins(?P<as_format>(\.json)?)$",
) )
add_route( add_route(

View file

@ -49,6 +49,7 @@ def get_plugins():
"name": plugin.__name__, "name": plugin.__name__,
"static_path": static_path, "static_path": static_path,
"templates_path": templates_path, "templates_path": templates_path,
"hooks": [h.name for h in pm.get_hookcallers(plugin)],
} }
distinfo = plugin_to_distinfo.get(plugin) distinfo = plugin_to_distinfo.get(plugin)
if distinfo: if distinfo:

View file

@ -78,10 +78,13 @@ Shows a list of currently installed plugins and their versions. `Plugins example
"name": "datasette_cluster_map", "name": "datasette_cluster_map",
"static": true, "static": true,
"templates": false, "templates": false,
"version": "0.4" "version": "0.10",
"hooks": ["extra_css_urls", "extra_js_urls", "extra_body_script"]
} }
] ]
Add ``?all=1`` to include details of the default plugins baked into Datasette.
.. _JsonDataView_config: .. _JsonDataView_config:
/-/config /-/config

View file

@ -1260,14 +1260,59 @@ def test_threads_json(app_client):
def test_plugins_json(app_client): def test_plugins_json(app_client):
response = app_client.get("/-/plugins.json") response = app_client.get("/-/plugins.json")
expected = [ expected = [
{"name": name, "static": False, "templates": False, "version": None} {
for name in ( "name": "messages_output_renderer.py",
"messages_output_renderer.py", "static": False,
"my_plugin.py", "templates": False,
"my_plugin_2.py", "version": None,
"register_output_renderer.py", "hooks": ["register_output_renderer"],
"view_name.py", },
) {
"name": "my_plugin.py",
"static": False,
"templates": False,
"version": None,
"hooks": [
"actor_from_request",
"extra_body_script",
"extra_css_urls",
"extra_js_urls",
"extra_template_vars",
"permission_allowed",
"prepare_connection",
"prepare_jinja2_environment",
"register_facet_classes",
"render_cell",
],
},
{
"name": "my_plugin_2.py",
"static": False,
"templates": False,
"version": None,
"hooks": [
"actor_from_request",
"asgi_wrapper",
"extra_js_urls",
"extra_template_vars",
"permission_allowed",
"render_cell",
],
},
{
"name": "register_output_renderer.py",
"static": False,
"templates": False,
"version": None,
"hooks": ["register_output_renderer"],
},
{
"name": "view_name.py",
"static": False,
"templates": False,
"version": None,
"hooks": ["extra_template_vars"],
},
] ]
assert expected == sorted(response.json, key=lambda p: p["name"]) assert expected == sorted(response.json, key=lambda p: p["name"])