From d11fd2cbaa6b31933b1319f81b5d1520726cb0b6 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 25 Nov 2019 20:57:52 -0800 Subject: [PATCH] Experimental load_metadata() hook, refs #357 --- datasette/cli.py | 10 +++------- datasette/hookspecs.py | 5 +++++ datasette/plugins.py | 1 + docs/datasette-serve-help.txt | 3 ++- docs/plugins.rst | 9 +++++++++ 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/datasette/cli.py b/datasette/cli.py index c2c03251..d8278d5d 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -265,8 +265,8 @@ def package( @click.option( "-m", "--metadata", - type=click.File(mode="r"), - help="Path to JSON file containing license/source metadata", + type=str, + help="Path to JSON file containing license/source metadata, or input to a metadata plugin", ) @click.option( "--template-dir", @@ -329,16 +329,12 @@ def serve( reloader = hupper.start_reloader("datasette.cli.serve") reloader.watch_files(files) - if metadata: - reloader.watch_files([metadata.name]) inspect_data = None if inspect_file: inspect_data = json.load(open(inspect_file)) - metadata_data = None - if metadata: - metadata_data = json.loads(metadata.read()) + metadata_data = pm.hook.load_metadata(metadata_value=metadata) click.echo( "Serve! files={} (immutables={}) on port {}".format(files, immutable, port) diff --git a/datasette/hookspecs.py b/datasette/hookspecs.py index 3c6726b7..c16ab4f9 100644 --- a/datasette/hookspecs.py +++ b/datasette/hookspecs.py @@ -58,3 +58,8 @@ def register_output_renderer(datasette): @hookspec def register_facet_classes(): "Register Facet subclasses" + + +@hookspec(firstresult=True) +def load_metadata(metadata_value): # , datasette): + "Return metadata dictionary loaded from metadata_value" diff --git a/datasette/plugins.py b/datasette/plugins.py index bf3735dc..de87cb4c 100644 --- a/datasette/plugins.py +++ b/datasette/plugins.py @@ -8,6 +8,7 @@ DEFAULT_PLUGINS = ( "datasette.publish.now", "datasette.publish.cloudrun", "datasette.facets", + "datasette.default_metadata", ) pm = pluggy.PluginManager("datasette") diff --git a/docs/datasette-serve-help.txt b/docs/datasette-serve-help.txt index e71e177d..30eba5aa 100644 --- a/docs/datasette-serve-help.txt +++ b/docs/datasette-serve-help.txt @@ -17,7 +17,8 @@ Options: --cors Enable CORS by serving Access-Control-Allow-Origin: * --load-extension PATH Path to a SQLite extension to load --inspect-file TEXT Path to JSON file created using "datasette inspect" - -m, --metadata FILENAME Path to JSON file containing license/source metadata + -m, --metadata TEXT Path to JSON file containing license/source metadata, or + input to a metadata plugin --template-dir DIRECTORY Path to directory containing custom templates --plugins-dir DIRECTORY Path to directory containing custom plugins --static MOUNT:DIRECTORY Serve static files from this directory at /MOUNT/... diff --git a/docs/plugins.rst b/docs/plugins.rst index 9bceb961..c2795870 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -813,3 +813,12 @@ This example plugin adds a ``x-databases`` HTTP header listing the currently att await app(scope, recieve, wrapped_send) return add_x_databases_header return wrap_with_databases_header + +.. _plugin_load_metadata: + +load_metadata(metadata_value) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Return a Python dictionary of metadata configuration values. The ``metadata_value`` argument will be the value passed to the ``-m`` or ``--metadata`` argument to ``datasette serve``. + +Only one plugin can implement this at a time. The first registered plugin will get to provide the metadata dictionary, while all other plugins will be ignored.