Skip to content

Commit

Permalink
Start of the plugin system, based on pluggy (#210)
Browse files Browse the repository at this point in the history
Uses https://pluggy.readthedocs.io/ originally created for the py.test project

We're starting with two plugin hooks:

prepare_connection(conn)

This is called when a new SQLite connection is created. It can be used to register custom SQL functions.

prepare_jinja2_environment(env)

This is called with the Jinja2 environment. It can be used to register custom template tags and filters.

An example plugin which uses these two hooks can be found at https://github.com/simonw/datasette-plugin-demos or installed using `pip install datasette-plugin-demos`

Refs #14
  • Loading branch information
Simon Willison authored Apr 16, 2018
1 parent efbb4e8 commit 33c7c53
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions datasette/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from datasette.version import __version_info__, __version__ # noqa
from .hookspecs import hookimpl # noqa
from .hookspecs import hookspec # noqa
9 changes: 9 additions & 0 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import hashlib
import time
import pint
import pluggy
import traceback
from .utils import (
Filters,
Expand All @@ -38,6 +39,7 @@
urlsafe_components,
validate_sql_select,
)
from . import hookspecs
from .version import __version__

app_root = Path(__file__).parent.parent
Expand All @@ -49,6 +51,11 @@
ureg = pint.UnitRegistry()


pm = pluggy.PluginManager('datasette')
pm.add_hookspecs(hookspecs)
pm.load_setuptools_entrypoints('datasette')


class DatasetteError(Exception):
def __init__(self, message, title=None, error_dict=None, status=500, template=None):
self.message = message
Expand Down Expand Up @@ -1100,6 +1107,7 @@ def prepare_connection(self, conn):
conn.enable_load_extension(True)
for extension in self.sqlite_extensions:
conn.execute("SELECT load_extension('{}')".format(extension))
pm.hook.prepare_connection(conn=conn)

def inspect(self):
if not self._inspect:
Expand Down Expand Up @@ -1226,6 +1234,7 @@ def app(self):
self.jinja_env.filters['quote_plus'] = lambda u: urllib.parse.quote_plus(u)
self.jinja_env.filters['escape_sqlite'] = escape_sqlite
self.jinja_env.filters['to_css_class'] = to_css_class
pm.hook.prepare_jinja2_environment(env=self.jinja_env)
app.add_route(IndexView.as_view(self), '/<as_json:(\.jsono?)?$>')
# TODO: /favicon.ico and /-/static/ deserve far-future cache expires
app.add_route(favicon, '/favicon.ico')
Expand Down
15 changes: 15 additions & 0 deletions datasette/hookspecs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pluggy import HookimplMarker
from pluggy import HookspecMarker

hookspec = HookspecMarker('datasette')
hookimpl = HookimplMarker('datasette')


@hookspec
def prepare_connection(conn):
"Modify SQLite connection in some way e.g. register custom SQL functions"


@hookspec
def prepare_jinja2_environment(env):
"Modify Jinja2 template environment e.g. register custom template tags"
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from setuptools import setup, find_packages
from datasette import __version__
from datasette.version import __version__
import os


Expand Down Expand Up @@ -28,7 +28,8 @@ def get_long_description():
'Sanic==0.7.0',
'Jinja2==2.10',
'hupper==1.0',
'pint==0.8.1'
'pint==0.8.1',
'pluggy>=0.1.0,<1.0',
],
entry_points='''
[console_scripts]
Expand Down

0 comments on commit 33c7c53

Please sign in to comment.