一文看懂neutron-server中Pecan的用法

前文有对neutron-server源码中的WSGI进行分析,总结出neutron-server的最简初始化过程。本文在此基础上对neutron-server中的Pecan继续进行深入介绍。

Pecan是一个基本对象分发风格路由的轻量级Python Web框架,所以相比PPRW框架(Paste,PastDeploy,Routes,WebOb),使用Pecan实现路由分发的neutron-server逻辑更加简洁易懂。一个V2Controller类,就完全表达了neutron-server中用于路由分发的功能,可见使用Pecan的编程效率。

V2Controller类:

class V2Controller(object)
    @utils.expose(generic=True)
    def index(self):
        if not pecan.request.path_url.endswith('/'):
            pecan.abort(404)
        layout = []
        for name, collection in attributes.CORE_RESOURCES.items():
            href = urlparse.urljoin(pecan.request.path_url, collection)
            resource = {'name': name,'collection': collection,'links': [{'rel': 'self','href': href}]}
            layout.append(resource)
        return {'resources': layout}
​
    @utils.when(index, method='HEAD')
    @utils.when(index, method='POST')
    @utils.when(index, method='PATCH')
    @utils.when(index, method='PUT')
    @utils.when(index, method='DELETE')
    def not_supported(self):
        pecan.abort(405)
​
    @utils.expose()
    def _lookup(self, collection, *remainder):
        if (remainder and manager.NeutronManage
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值