Make setting(), settings_dict(), and plugin_config() async

This change makes all settings and plugin config access asynchronous in
preparation for a future plugin hook that will allow fetching settings
from a database or via HTTP.

Changes:
- Datasette.setting() is now async
- Datasette.settings_dict() is now async
- Datasette.plugin_config() is now async
- Added _setting_sync() for contexts that cannot await (threads, sync hooks)
- Added _plugin_config_sync() for contexts that cannot await
- Updated all callers throughout the codebase to use await
- Updated tests to work with async methods
This commit is contained in:
Claude 2025-12-30 16:21:16 +00:00
commit 1a7b027b60
No known key found for this signature in database
13 changed files with 90 additions and 76 deletions

View file

@ -224,26 +224,26 @@ async def test_hook_render_cell_async(ds_client, path):
@pytest.mark.asyncio
async def test_plugin_config(ds_client):
assert {"depth": "table"} == ds_client.ds.plugin_config(
assert {"depth": "table"} == await ds_client.ds.plugin_config(
"name-of-plugin", database="fixtures", table="sortable"
)
assert {"depth": "database"} == ds_client.ds.plugin_config(
assert {"depth": "database"} == await ds_client.ds.plugin_config(
"name-of-plugin", database="fixtures", table="unknown_table"
)
assert {"depth": "database"} == ds_client.ds.plugin_config(
assert {"depth": "database"} == await ds_client.ds.plugin_config(
"name-of-plugin", database="fixtures"
)
assert {"depth": "root"} == ds_client.ds.plugin_config(
assert {"depth": "root"} == await ds_client.ds.plugin_config(
"name-of-plugin", database="unknown_database"
)
assert {"depth": "root"} == ds_client.ds.plugin_config("name-of-plugin")
assert None is ds_client.ds.plugin_config("unknown-plugin")
assert {"depth": "root"} == await ds_client.ds.plugin_config("name-of-plugin")
assert None is await ds_client.ds.plugin_config("unknown-plugin")
@pytest.mark.asyncio
async def test_plugin_config_env(ds_client, monkeypatch):
monkeypatch.setenv("FOO_ENV", "FROM_ENVIRONMENT")
assert ds_client.ds.plugin_config("env-plugin") == {"foo": "FROM_ENVIRONMENT"}
assert await ds_client.ds.plugin_config("env-plugin") == {"foo": "FROM_ENVIRONMENT"}
@pytest.mark.asyncio
@ -252,13 +252,13 @@ async def test_plugin_config_env_from_config(monkeypatch):
datasette = Datasette(
config={"plugins": {"env-plugin": {"setting": {"$env": "FOO_ENV"}}}}
)
assert datasette.plugin_config("env-plugin") == {"setting": "FROM_ENVIRONMENT_2"}
assert await datasette.plugin_config("env-plugin") == {"setting": "FROM_ENVIRONMENT_2"}
@pytest.mark.asyncio
async def test_plugin_config_env_from_list(ds_client):
os.environ["FOO_ENV"] = "FROM_ENVIRONMENT"
assert [{"in_a_list": "FROM_ENVIRONMENT"}] == ds_client.ds.plugin_config(
assert [{"in_a_list": "FROM_ENVIRONMENT"}] == await ds_client.ds.plugin_config(
"env-plugin-list"
)
del os.environ["FOO_ENV"]
@ -268,7 +268,7 @@ async def test_plugin_config_env_from_list(ds_client):
async def test_plugin_config_file(ds_client):
with open(TEMP_PLUGIN_SECRET_FILE, "w") as fp:
fp.write("FROM_FILE")
assert {"foo": "FROM_FILE"} == ds_client.ds.plugin_config("file-plugin")
assert {"foo": "FROM_FILE"} == await ds_client.ds.plugin_config("file-plugin")
os.remove(TEMP_PLUGIN_SECRET_FILE)