跳转到主要内容

完成所有创建Sanic插件的开销,让您无需自己动手。

项目描述

Build Status Latest Version Supported Python versions License

欢迎使用Sanic插件框架。

Sanic插件框架(SPF)是一个轻量级的Python库,旨在使为Sanic异步HTTP服务器构建插件尽可能简单。

SPF提供了一个Python基类对象SanicPlugin,您的插件可以在此基础上构建。它已设置好大多数Sanic插件所需的所有基本功能。

SPF Sanic插件以类似于Sanic蓝本的方式实现。您可以使用便利装饰器以相同的方式设置插件使用的所有路由、中间件、异常处理程序和监听器,就像您会使用蓝图一样,任何应用程序开发人员都可以导入您的插件并将其注册到他们的应用程序中。

Sanic插件框架不仅仅是插件类似蓝本的系统。它提供增强的中间件系统,并管理上下文对象。

注意:如果您需要与Sanic v19.12+兼容,请更新到SPF v0.9.0。有关更多详细信息,请参阅此处

增强的中间件系统

Sanic插件框架中的中间件系统既基于又扩展了原生Sanic中间件系统。而不是简单地有两个中间件队列(“请求”和“响应”),SPF中的中间件系统使用五个额外的队列。

  • 请求-预:这些中间件在应用程序自己的请求中间件之前运行。

  • 请求-后置:这些中间件在应用自身的请求中间件之后运行。

  • 响应-前置:这些中间件在应用自身的响应中间件之前运行。

  • 响应-后置:这些中间件在应用自身的响应中间件之后运行。

  • 清理:这些中间件在所有上述中间件之后运行,在响应发送后运行,即使响应为空也会运行。

因此,作为插件开发者,您可以选择您的中间件是在应用自身的中间件之前还是之后执行。

您还可以为您的插件中的每个中间件分配优先级,以便更精确地控制中间件的执行顺序,尤其是在应用使用多个插件时。

上下文对象管理器

许多人都发现Sanic缺少一个上下文对象。SPF提供了多个上下文对象,可用于不同的目的。

  • 共享上下文:所有注册在SPF中的插件都可以访问一个共享的、持久的上下文对象,任何人都可以读取和写入。

  • 请求上下文:所有插件都可以访问一个共享的临时上下文对象,任何人都可以读取和写入,该对象在请求开始时创建,在请求完成后删除。

  • 插件上下文:所有插件都拥有自己的私有持久上下文对象,只有该插件可以读取和写入。

  • 插件请求上下文:所有插件都拥有一个临时私有上下文对象,该对象在请求开始时创建,在请求完成后删除。

安装

使用pip或easy_install安装此扩展。

$ pip install -U sanic-plugins-framework

使用

使用Sanic插件框架编写的简单插件将类似于以下示例

# 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))

应用开发者可以在其代码中使用您的插件,如下所示

# 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

支持使用配置文件定义在SPF添加到应用时加载的插件列表。

# 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')

或者,如果开发者更喜欢使用旧方法(如Flask方式),他们仍然可以这样操作

# 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

贡献

有问题、评论或改进建议?请在Github上创建问题

致谢

Ashley Sommer ashleysommer@gmail.com

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

Sanic-Plugins-Framework-0.9.5.tar.gz (21.4 kB 查看哈希值)

上传时间 源代码

构建分发

Sanic_Plugins_Framework-0.9.5-py2.py3-none-any.whl (23.6 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下提供支持