做所有的样板来创建一个sanic插件,这样你就不必这么做了。
Sanic-Plugins-Framework的Python项目详细描述
欢迎使用sanic插件框架自述文件。
sanic插件框架(spf)是一个轻量级的python库,旨在使其尽可能简单地构建 sanic异步http服务器的插件。
spf提供了一个sanicplugin的python基对象,您的插件可以在这个对象上构建。它设置了所有基本的 大多数sanic插件将需要的功能。
spf sanic插件的实现方式与sanic蓝图类似。你可以使用方便的装饰来设置所有 你的插件使用的路由、中间件、异常处理程序和监听器与你的蓝图一样, 任何应用程序开发人员都可以导入插件并将其注册到他们的应用程序中。
sanic插件框架不仅仅是一个类似于插件系统的蓝图。它提供了一个增强的中间件 系统,并管理上下文对象。
增强的中间件系统
sanic插件框架中的中间件系统是在原有sanic中间件系统的基础上构建和扩展的。 spf中的中间件系统不只是有两个中间件队列(“请求”和“响应”),而是使用五个 其他队列。
- 请求前置:这些中间件在应用程序自己的请求中间件之前运行。
- 请求发布:这些中间件在应用程序自己的请求中间件之后运行。
- response pre:这些中间件在应用程序自己的响应中间件之前运行。
- response post:这些中间件在应用程序自己的响应中间件之后运行。
- 清理:这些中间件在以上所有中间件之后运行,并在发送响应之后运行,即使响应为“无”,也会运行。
因此作为插件开发人员,您可以选择是否需要在 应用程序自己的中间件。
您还可以为每个插件的中间件分配优先级,以便更精确地控制 您的中间件被执行,特别是当应用程序使用多个插件时。
上下文对象管理器
许多人发现sanic缺少的一个特性是上下文对象。SPF提供了多个上下文对象,这些对象可以是 用于不同的目的。
- 共享上下文:SPF中注册的所有插件都可以访问共享的持久上下文对象,任何人都可以对其进行读写。
- 每个请求上下文:所有插件都可以访问一个共享的临时上下文对象,任何人都可以读写该对象,该对象在请求开始时创建,在请求完成时删除。
- 每个插件上下文:所有插件都有自己的私有持久上下文对象,只有该插件才能读写。
- 每个插件每个请求上下文:所有插件都得到一个临时上下文对象,该对象在请求开始时创建,在请求完成时删除。
安装
使用pip或easy_install安装扩展。
$ pip install -U sanic-plugins-framework
用法
使用sanic插件框架编写的简单插件如下:
# Source: my_plugin.pyfromspfimportSanicPluginfromsanic.responseimporttextclassMyPlugin(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.defon_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=c.sharedshared.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.middlewaredefmy_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)defmy_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)defmy_middleware3(request,response,context):# Do response middleware herereturnresponse#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)defmy_middleware4(request,response,context):# Do response middleware herereturnresponse#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)defmy_middleware5(request,context):# Do per-request cleanup here.returnNone#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)deft1(request,context):words=context['test1']returntext('from plugin! {}'.format(words))
应用程序开发人员可以在其代码中使用您的插件,如下所示:
# Source: app.pyfromsanicimportSanicfromspfimportSanicPluginsFrameworkfromsanic.responseimporttextimportmy_pluginapp=Sanic(__name__)spf=SanicPluginsFramework(app)assoc=spf.register_plugin(my_plugin)# ... rest of user app here
支持使用配置文件定义在将SPF添加到应用程序时要加载的插件列表。
# Source: spf.ini[plugins]MyPluginAnotherPlugin=ExampleArg,False,KWArg1=True,KWArg2=33.3
# Source: app.pyapp=Sanic(__name__)app.config['SPF_LOAD_INI']=Trueapp.config['SPF_INI_FILE']='spf.ini'spf=SanicPluginsFramework(app)# We can get the assoc object from SPF, it is already registeredassoc=spf.get_plugin_assoc('MyPlugin')
或者,如果开发人员喜欢使用旧方法(如烧瓶方式),他们仍然可以这样做:
# Source: app.pyfromsanicimportSanicfromsanic.responseimporttextfrommy_pluginimportMyPluginapp=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
学分
艾希礼·索默ashleysommer@gmail.com