mirror of
https://github.com/simonw/datasette.git
synced 2026-09-29 05:14:21 +02:00
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:
parent
6fede23a2e
commit
1a7b027b60
13 changed files with 90 additions and 76 deletions
|
|
@ -27,7 +27,7 @@ def prepare_connection(conn, database, datasette):
|
|||
|
||||
def prepare_connection_args():
|
||||
return 'database={}, datasette.plugin_config("name-of-plugin")={}'.format(
|
||||
database, datasette.plugin_config("name-of-plugin")
|
||||
database, datasette._plugin_config_sync("name-of-plugin")
|
||||
)
|
||||
|
||||
conn.create_function("prepare_connection_args", 0, prepare_connection_args)
|
||||
|
|
@ -84,7 +84,7 @@ def extra_body_script(
|
|||
"template": template,
|
||||
"database": database,
|
||||
"table": table,
|
||||
"config": datasette.plugin_config(
|
||||
"config": await datasette.plugin_config(
|
||||
"name-of-plugin",
|
||||
database=database,
|
||||
table=table,
|
||||
|
|
@ -113,7 +113,7 @@ def render_cell(row, value, column, table, database, datasette, request):
|
|||
"column": column,
|
||||
"table": table,
|
||||
"database": database,
|
||||
"config": datasette.plugin_config(
|
||||
"config": await datasette.plugin_config(
|
||||
"name-of-plugin",
|
||||
database=database,
|
||||
table=table,
|
||||
|
|
@ -453,8 +453,8 @@ def skip_csrf(scope):
|
|||
|
||||
@hookimpl
|
||||
def register_actions(datasette):
|
||||
extras_old = datasette.plugin_config("datasette-register-permissions") or {}
|
||||
extras_new = datasette.plugin_config("datasette-register-actions") or {}
|
||||
extras_old = datasette._plugin_config_sync("datasette-register-permissions") or {}
|
||||
extras_new = datasette._plugin_config_sync("datasette-register-actions") or {}
|
||||
|
||||
actions = [
|
||||
Action(
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ def table_actions(datasette, database, table, actor, request):
|
|||
|
||||
@hookimpl
|
||||
def register_routes(datasette):
|
||||
config = datasette.plugin_config("register-route-demo")
|
||||
config = datasette._plugin_config_sync("register-route-demo")
|
||||
if not config:
|
||||
return
|
||||
path = config["path"]
|
||||
|
|
|
|||
|
|
@ -47,8 +47,9 @@ def test_sign_unsign(datasette, value, namespace):
|
|||
("allow_csv_stream", True),
|
||||
),
|
||||
)
|
||||
def test_datasette_setting(datasette, setting, expected):
|
||||
assert datasette.setting(setting) == expected
|
||||
@pytest.mark.asyncio
|
||||
async def test_datasette_setting(datasette, setting, expected):
|
||||
assert await datasette.setting(setting) == expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue