Skip to content

Commit

Permalink
Async support for prepare_jinja2_environment, closes #1809
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 17, 2022
1 parent 2ebcffe commit ddc999a
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 9 deletions.
22 changes: 19 additions & 3 deletions datasette/app.py
Expand Up @@ -208,6 +208,7 @@ def __init__(
crossdb=False,
nolock=False,
):
self._startup_invoked = False
assert config_dir is None or isinstance(
config_dir, Path
), "config_dir= should be a pathlib.Path"
Expand Down Expand Up @@ -344,9 +345,6 @@ def __init__(
self.jinja_env.filters["quote_plus"] = urllib.parse.quote_plus
self.jinja_env.filters["escape_sqlite"] = escape_sqlite
self.jinja_env.filters["to_css_class"] = to_css_class
# pylint: disable=no-member
pm.hook.prepare_jinja2_environment(env=self.jinja_env, datasette=self)

self._register_renderers()
self._permission_checks = collections.deque(maxlen=200)
self._root_token = secrets.token_hex(32)
Expand Down Expand Up @@ -389,8 +387,16 @@ def urls(self):
return Urls(self)

async def invoke_startup(self):
# This must be called for Datasette to be in a usable state
if self._startup_invoked:
return
for hook in pm.hook.prepare_jinja2_environment(
env=self.jinja_env, datasette=self
):
await await_me_maybe(hook)
for hook in pm.hook.startup(datasette=self):
await await_me_maybe(hook)
self._startup_invoked = True

def sign(self, value, namespace="default"):
return URLSafeSerializer(self._secret, namespace).dumps(value)
Expand Down Expand Up @@ -933,6 +939,8 @@ def _register_renderers(self):
async def render_template(
self, templates, context=None, request=None, view_name=None
):
if not self._startup_invoked:
raise Exception("render_template() called before await ds.invoke_startup()")
context = context or {}
if isinstance(templates, Template):
template = templates
Expand Down Expand Up @@ -1495,34 +1503,42 @@ def _fix(self, path, avoid_path_rewrites=False):
return path

async def get(self, path, **kwargs):
await self.ds.invoke_startup()
async with httpx.AsyncClient(app=self.app) as client:
return await client.get(self._fix(path), **kwargs)

async def options(self, path, **kwargs):
await self.ds.invoke_startup()
async with httpx.AsyncClient(app=self.app) as client:
return await client.options(self._fix(path), **kwargs)

async def head(self, path, **kwargs):
await self.ds.invoke_startup()
async with httpx.AsyncClient(app=self.app) as client:
return await client.head(self._fix(path), **kwargs)

async def post(self, path, **kwargs):
await self.ds.invoke_startup()
async with httpx.AsyncClient(app=self.app) as client:
return await client.post(self._fix(path), **kwargs)

async def put(self, path, **kwargs):
await self.ds.invoke_startup()
async with httpx.AsyncClient(app=self.app) as client:
return await client.put(self._fix(path), **kwargs)

async def patch(self, path, **kwargs):
await self.ds.invoke_startup()
async with httpx.AsyncClient(app=self.app) as client:
return await client.patch(self._fix(path), **kwargs)

async def delete(self, path, **kwargs):
await self.ds.invoke_startup()
async with httpx.AsyncClient(app=self.app) as client:
return await client.delete(self._fix(path), **kwargs)

async def request(self, method, path, **kwargs):
await self.ds.invoke_startup()
avoid_path_rewrites = kwargs.pop("avoid_path_rewrites", None)
async with httpx.AsyncClient(app=self.app) as client:
return await client.request(
Expand Down
1 change: 1 addition & 0 deletions datasette/utils/testing.py
Expand Up @@ -147,6 +147,7 @@ async def _request(
content_type=None,
if_none_match=None,
):
await self.ds.invoke_startup()
headers = headers or {}
if content_type:
headers["content-type"] = content_type
Expand Down
2 changes: 2 additions & 0 deletions docs/plugin_hooks.rst
Expand Up @@ -88,6 +88,8 @@ You can now use this filter in your custom templates like so::

Table name: {{ table|uppercase }}

This function can return an awaitable function if it needs to run any async code.

Examples: `datasette-edit-templates <https://datasette.io/plugins/datasette-edit-templates>`_

.. _plugin_hook_extra_template_vars:
Expand Down
30 changes: 30 additions & 0 deletions docs/testing_plugins.rst
Expand Up @@ -52,6 +52,36 @@ Then run the tests using pytest like so::

pytest

.. _testing_plugins_datasette_test_instance:

Setting up a Datasette test instance
------------------------------------

The above example shows the easiest way to start writing tests against a Datasette instance:

.. code-block:: python
from datasette.app import Datasette
import pytest
@pytest.mark.asyncio
async def test_plugin_is_installed():
datasette = Datasette(memory=True)
response = await datasette.client.get("/-/plugins.json")
assert response.status_code == 200
Creating a ``Datasette()`` instance like this as useful shortcut in tests, but there is one detail you need to be aware of. It's important to ensure that the async method ``.invoke_startup()`` is called on that instance. You can do that like this:

.. code-block:: python
datasette = Datasette(memory=True)
await datasette.invoke_startup()
This method registers any :ref:`plugin_hook_startup` or :ref:`plugin_hook_prepare_jinja2_environment` plugins that might themselves need to make async calls.

If you are using ``await datasette.client.get()`` and similar methods then you don't need to worry about this - those method calls ensure that ``.invoke_startup()`` has been called for you.

.. _testing_plugins_pdb:

Using pdb for errors thrown inside Datasette
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.py
Expand Up @@ -71,6 +71,7 @@
"handle_exception",
"menu_links",
"permission_allowed",
"prepare_jinja2_environment",
"register_routes",
"render_cell",
"startup",
Expand Down
10 changes: 8 additions & 2 deletions tests/plugins/my_plugin.py
Expand Up @@ -143,8 +143,14 @@ def extra_template_vars(

@hookimpl
def prepare_jinja2_environment(env, datasette):
env.filters["format_numeric"] = lambda s: f"{float(s):,.0f}"
env.filters["to_hello"] = lambda s: datasette._HELLO
async def select_times_three(s):
db = datasette.get_database()
return (await db.execute("select 3 * ?", [int(s)])).first()[0]

async def inner():
env.filters["select_times_three"] = select_times_three

return inner


@hookimpl
Expand Down
6 changes: 6 additions & 0 deletions tests/plugins/my_plugin_2.py
Expand Up @@ -126,6 +126,12 @@ async def inner():
return inner


@hookimpl
def prepare_jinja2_environment(env, datasette):
env.filters["format_numeric"] = lambda s: f"{float(s):,.0f}"
env.filters["to_hello"] = lambda s: datasette._HELLO


@hookimpl
def startup(datasette):
async def inner():
Expand Down
6 changes: 4 additions & 2 deletions tests/test_internals_datasette_client.py
@@ -1,10 +1,12 @@
from .fixtures import app_client
import httpx
import pytest
import pytest_asyncio


@pytest.fixture
def datasette(app_client):
@pytest_asyncio.fixture
async def datasette(app_client):
await app_client.ds.invoke_startup()
return app_client.ds


Expand Down
6 changes: 4 additions & 2 deletions tests/test_plugins.py
Expand Up @@ -546,11 +546,13 @@ def test_hook_register_output_renderer_can_render(app_client):
@pytest.mark.asyncio
async def test_hook_prepare_jinja2_environment(app_client):
app_client.ds._HELLO = "HI"
await app_client.ds.invoke_startup()
template = app_client.ds.jinja_env.from_string(
"Hello there, {{ a|format_numeric }}, {{ a|to_hello }}", {"a": 3412341}
"Hello there, {{ a|format_numeric }}, {{ a|to_hello }}, {{ b|select_times_three }}",
{"a": 3412341, "b": 5},
)
rendered = await app_client.ds.render_template(template)
assert "Hello there, 3,412,341, HI" == rendered
assert "Hello there, 3,412,341, HI, 15" == rendered


def test_hook_publish_subcommand():
Expand Down
1 change: 1 addition & 0 deletions tests/test_routes.py
Expand Up @@ -59,6 +59,7 @@ def test_routes(routes, path, expected_class, expected_matches):
@pytest_asyncio.fixture
async def ds_with_route():
ds = Datasette()
await ds.invoke_startup()
ds.remove_database("_memory")
db = Database(ds, is_memory=True, memory_name="route-name-db")
ds.add_database(db, name="original-name", route="custom-route-name")
Expand Down

0 comments on commit ddc999a

Please sign in to comment.