Skip to main content

Doing all of the boilerplate to create a Sanic Plugin, so you don't have to.

Project description

Build Status Latest Version Supported Python versions License

Welcome to the Sanic Plugins Framework.

The Sanic Plugins Framework (SPF) is a lightweight python library aimed at making it as simple as possible to build plugins for the Sanic Async HTTP Server.

The SPF provides a SanicPlugin python base object that your plugin can build upon. It is set up with all of the basic functionality that the majority of Sanic Plugins will need.

A SPF Sanic Plugin is implemented in a similar way to Sanic Blueprints. You can use convenience decorators to set up all of the routes, middleware, exception handlers, and listeners your plugin uses in the same way you would a blueprint, and any Application developer can import your plugin and register it into their application.

The Sanic Plugins Framework is more than just a Blueprints-like system for Plugins. It provides an enhanced middleware system, and manages Context objects.

Notice: Please update to SPF v0.9.0 if you need compatibility with Sanic v19.12+. See here for more details.

The Enhanced Middleware System

The Middleware system in the Sanic Plugins Framework both builds upon and extends the native Sanic middleware system. Rather than simply having two middleware queues (‘request’, and ‘response’), the middleware system in SPF uses five additional queues.

  • Request-Pre: These middleware run before the application’s own request middleware.

  • Request-Post: These middleware run after the application’s own request middleware.

  • Response-Pre: These middleware run before the application’s own response middleware.

  • Response-Post: These middleware run after the application’s own response middleware.

  • Cleanup: These middleware run after all of the above middleware, and are run after a response is sent, and are run even if response is None.

So as a plugin developer you can choose whether you need your middleware to be executed before or after the application’s own middleware.

You can also assign a priority to each of your plugin’s middleware so you can more precisely control the order in which your middleware is executed, especially when the application is using multiple plugins.

The Context Object Manager

One feature that many find missing from Sanic is a context object. SPF provides multiple context objects that can be used for different purposes.

  • A shared context: All plugins registered in the SPF have access to a shared, persistent context object, which anyone can read and write to.

  • A per-request context: All plugins get access to a shared temporary context object anyone can read and write to that is created at the start of a request, and deleted when a request is completed.

  • A per-plugin context: All plugins get their own private persistent context object that only that plugin can read and write to.

  • A per-plugin per-request context: All plugins get a temporary private context object that is created at the start of a request, and deleted when a request is completed.

Installation

Install the extension with using pip, or easy_install.

$ pip install -U sanic-plugins-framework

Usage

A simple plugin written using the Sanic Plugins Framework will look like this:

# Source: my_plugin.py
from spf import SanicPlugin
from sanic.response import text

class MyPlugin(SanicPlugin):
    def __init__(self, *args, **kwargs):
        super(MyPlugin, self).__init__(*args, **kwargs)
        # do pre-registration plugin init here.
        # Note, context objects are not accessible here.

    def on_registered(self, context, reg, *args, **kwargs):
        # do post-registration plugin init here
        # We have access to our context and the shared context now.
        context.my_private_var = "Private variable"
        shared = context.shared
        shared.my_shared_var = "Shared variable"

my_plugin = MyPlugin()

# You don't need to add any parameters to @middleware, for default behaviour
# This is completely compatible with native Sanic middleware behaviour
@my_plugin.middleware
def my_middleware(request)
    h = request.headers
    #Do request middleware things here

#You can tune the middleware priority, and add a context param like this
#Priority must be between 0 and 9 (inclusive). 0 is highest priority, 9 the lowest.
#If you don't specify an 'attach_to' parameter, it is a 'request' middleware
@my_plugin.middleware(priority=6, with_context=True)
def my_middleware2(request, context):
    context['test1'] = "test"
    print("Hello world")

#Add attach_to='response' to make this a response middleware
@my_plugin.middleware(attach_to='response', with_context=True)
def my_middleware3(request, response, context):
    # Do response middleware here
    return response

#Add relative='pre' to make this a response middleware run _before_ the
#application's own response middleware
@my_plugin.middleware(attach_to='response', relative='pre', with_context=True)
def my_middleware4(request, response, context):
    # Do response middleware here
    return response

#Add attach_to='cleanup' to make this run even when the Response is None.
#This queue is fired _after_ response is already sent to the client.
@my_plugin.middleware(attach_to='cleanup', with_context=True)
def my_middleware5(request, context):
    # Do per-request cleanup here.
    return None

#Add your plugin routes here. You can even choose to have your context passed in to the route.
@my_plugin.route('/test_plugin', with_context=True)
def t1(request, context):
    words = context['test1']
    return text('from plugin! {}'.format(words))

The Application developer can use your plugin in their code like this:

# Source: app.py
from sanic import Sanic
from spf import SanicPluginsFramework
from sanic.response import text
import my_plugin

app = Sanic(__name__)
spf = SanicPluginsFramework(app)
assoc = spf.register_plugin(my_plugin)

# ... rest of user app here

There is support for using a config file to define the list of plugins to load when SPF is added to an App.

# Source: spf.ini
[plugins]
MyPlugin
AnotherPlugin=ExampleArg,False,KWArg1=True,KWArg2=33.3
# Source: app.py
app = Sanic(__name__)
app.config['SPF_LOAD_INI'] = True
app.config['SPF_INI_FILE'] = 'spf.ini'
spf = SanicPluginsFramework(app)

# We can get the assoc object from SPF, it is already registered
assoc = spf.get_plugin_assoc('MyPlugin')

Or if the developer prefers to do it the old way, (like the Flask way), they can still do it like this:

# Source: app.py
from sanic import Sanic
from sanic.response import text
from my_plugin import MyPlugin

app = Sanic(__name__)
# this magically returns your previously initialized instance
# from your plugin module, if it is named `my_plugin` or `instance`.
assoc = MyPlugin(app)

# ... rest of user app here

Contributing

Questions, comments or improvements? Please create an issue on Github

Credits

Ashley Sommer ashleysommer@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Sanic-Plugins-Framework-0.9.3.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Sanic_Plugins_Framework-0.9.3-py2.py3-none-any.whl (23.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file Sanic-Plugins-Framework-0.9.3.tar.gz.

File metadata

  • Download URL: Sanic-Plugins-Framework-0.9.3.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.10

File hashes

Hashes for Sanic-Plugins-Framework-0.9.3.tar.gz
Algorithm Hash digest
SHA256 9fd3d6270a676134c8440f78ce6f0817574b456d6b0cb7c3d73c06bf1b7f8956
MD5 98c991b3b01156aa75e21f6296721e41
BLAKE2b-256 0752b4673cb4f76998ed81ac781788fe8e2231d0099ee8faf81291435928039d

See more details on using hashes here.

File details

Details for the file Sanic_Plugins_Framework-0.9.3-py2.py3-none-any.whl.

File metadata

  • Download URL: Sanic_Plugins_Framework-0.9.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 23.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.10

File hashes

Hashes for Sanic_Plugins_Framework-0.9.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 347a3657984b828fc6178f86f49f0519f32d45b588724a4f96b12220bce3b4a2
MD5 f79f082d5d6f4480ac6eb08a697d82f9
BLAKE2b-256 173339c4466ff47e7bc2f43f7491e1134ccdf658e2c5e9d60512b01f79d70f9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page