Porting an Existing WSGI Application to Pyramid

Pyramid 很酷,但已经运行的代码更酷。您可能没有时间、金钱或精力将现有的 Pylons 、Django、Zope或其他基于WSGi的应用程序移植到 Pyramid 批发。在这种情况下,有必要 incrementally 将现有应用程序移植到 Pyramid 。

The broad-brush way to do this is:

  • 建立一个 exception view 每当 Pyramid 发现未发现异常时,就会调用它。

  • 在这个异常视图中,委托给已经编写的wsgi应用程序。

举个例子:

 1from pyramid.wsgi import wsgiapp2
 2from pyramid.exceptions import NotFound
 3
 4if __name__ == '__main__':
 5    # during Pyramid configuration (usually in your Pyramid project's
 6    # __init__.py), get a hold of an instance of your existing WSGI
 7    # application.
 8    original_app = MyWSGIApplication()
 9
10    # using the pyramid.wsgi.wsgiapp2 wrapper function, wrap the
11    # application into something that can be used as a Pyramid view.
12    notfound_view = wsgiapp2(original_app)
13
14    # in your configuration, use the wsgiapp2-wrapped application as
15    # a NotFound exception view
16    config = Configurator()
17
18    # ... your other Pyramid configuration ...
19    config.add_view(notfound_view, context=NotFound)
20    # .. the remainder of your configuration ...

当 Pyramid 无法解析视图的URL时,它将引发NotFound异常。这个 add_view

逐步地,您可以开始将功能从现有的WSGi应用程序移动到 Pyramid ;如果 Pyramid 可以将请求解析为视图,则将使用应用程序逻辑的 Pyramid “版本”。如果不能,将使用逻辑的原始WSGi应用程序版本。随着时间的推移,你可以移动 all 不需要一次完成所有的事情就可以进入 Pyramid 。