前文有对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