Coder Social home page Coder Social logo

Comments (3)

simonw avatar simonw commented on June 21, 2024

Prototype:

diff --git a/datasette/plugins.py b/datasette/plugins.py
index f7a1905f..d7f73e7d 100644
--- a/datasette/plugins.py
+++ b/datasette/plugins.py
@@ -1,6 +1,7 @@
 import importlib
 import os
 import pluggy
+from pprint import pprint
 import sys
 from . import hookspecs
 
@@ -33,6 +34,29 @@ DEFAULT_PLUGINS = (
 pm = pluggy.PluginManager("datasette")
 pm.add_hookspecs(hookspecs)
 
+DATASETTE_PLUGGY_TRACING = os.environ.get("DATASETTE_PLUGGY_TRACING", None)
+
+
+def before(hook_name, hook_impls, kwargs):
+    print()
+    print(f"{hook_name}:")
+    pprint(kwargs, width=40, indent=4)
+    print("Hook implementations:")
+    pprint(hook_impls, width=40, indent=4)
+
+
+def after(outcome, hook_name, hook_impls, kwargs):
+    results = outcome.get_result()
+    if not isinstance(results, list):
+        results = [results]
+    print(f"Results:")
+    pprint(results, width=40, indent=4)
+
+
+if DATASETTE_PLUGGY_TRACING:
+    pm.add_hookcall_monitoring(before, after)
+
+
 DATASETTE_LOAD_PLUGINS = os.environ.get("DATASETTE_LOAD_PLUGINS", None)
 
 if not hasattr(sys, "_called_from_test") and DATASETTE_LOAD_PLUGINS is None:

Produces output like this:

actor_from_request:
{   'datasette': <datasette.app.Datasette object at 0x104d8bc70>,
    'request': <asgi.Request method="GET" url="http://127.0.0.1:4433/fixtures/sortable">}
Hook implementations:
[   <HookImpl plugin_name='codespaces', plugin=<module 'datasette_codespaces' from '/Users/simon/.local/share/virtualenvs/datasette-AWNrQs95/lib/python3.10/site-packages/datasette_codespaces/__init__.py'>>,
    <HookImpl plugin_name='datasette.actor_auth_cookie', plugin=<module 'datasette.actor_auth_cookie' from '/Users/simon/Dropbox/Development/datasette/datasette/actor_auth_cookie.py'>>,
    <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>]
Results:
[]

permission_allowed:
{   'action': 'view-table',
    'actor': None,
    'datasette': <datasette.app.Datasette object at 0x104d8bc70>,
    'resource': (   'fixtures',
                    'sortable')}
Hook implementations:
[   <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>,
    <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>]
Results:
[   <function permission_allowed_default.<locals>.inner at 0x1081332e0>]

permission_allowed:
{   'action': 'view-database',
    'actor': None,
    'datasette': <datasette.app.Datasette object at 0x104d8bc70>,
    'resource': 'fixtures'}
Hook implementations:
[   <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>,
    <HookImpl plugin_name='datasette.default_permissions', plugin=<module 'datasette.default_permissions' from '/Users/simon/Dropbox/Development/datasette/datasette/default_permissions.py'>>]
Results:
[   <function permission_allowed_default.<locals>.inner at 0x108132cb0>]

from datasette.

simonw avatar simonw commented on June 21, 2024

It's a bit annoying that the await_me_maybe pattern produces results like this:

Results:
[   <function permission_allowed_default.<locals>.inner at 0x1081332e0>]

I tried to display the result of await_me_maybe on them but the before and after hook functions aren't async and my attempts to execute that using loop.run_until_complete() didn't work.

from datasette.

simonw avatar simonw commented on June 21, 2024

Documentation: https://docs.datasette.io/en/latest/writing_plugins.html#tracing-plugin-hooks

from datasette.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.