Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start of the plugin system, based on pluggy #210

Merged
merged 3 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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