Experimental load_metadata() hook, refs #357

plugin-load-metadata
Simon Willison 2019-11-25 20:57:52 -08:00
rodzic df2879ee2a
commit d11fd2cbaa
5 zmienionych plików z 20 dodań i 8 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -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"

Wyświetl plik

@ -8,6 +8,7 @@ DEFAULT_PLUGINS = (
"datasette.publish.now",
"datasette.publish.cloudrun",
"datasette.facets",
"datasette.default_metadata",
)
pm = pluggy.PluginManager("datasette")

Wyświetl plik

@ -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/...

Wyświetl plik

@ -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.