本文整理匯總了Python中router.Router.new方法的典型用法代碼示例。如果您正苦於以下問題:Python Router.new方法的具體用法?Python Router.new怎麽用?Python Router.new使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類router.Router
的用法示例。
在下文中一共展示了Router.new方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: Application
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import new [as 別名]
class Application(object):
"""WSGI-application.
:param root_factory: callable which returns a root object
The application passes ``environ`` and ``start_response`` to the
controller and expects an iterable as return value (or generator).
If traversal is used, the result is passed on to the controller as
the first argument.
"""
def __init__(self, root_factory=None):
self._router = Router()
self._root_factories = {}
self._root_factory = root_factory
def __call__(self, environ, start_response):
path = environ['PATH_INFO']
controller = self.match(path)
return controller(environ, start_response)
def match(self, path):
match = self._router(path)
if match is None:
return self.not_found
route = match.route
if match.path is not None:
root_factory = self._root_factories[route]
root = root_factory()
context = self.traverse(root, match.path)
controller = route.get(type(context))
return partial(controller, context, **match.dict)
else:
controller = route.get()
return partial(controller, **match.dict)
def not_found(self, environ, start_response):
start_response("404 Not Found", [('Content-type', 'text/plain')])
return 'Page not found',
def route(self, path, root_factory=None):
if root_factory is None:
root_factory = self._root_factory
route = self._router.new(path)
self._root_factories[route] = root_factory
return route
def traverse(self, root, path):
segments = path.split('/')
context = root
for segment in segments:
context = context[segment]
return context