本文整理汇总了Python中werkzeug.routing.Map.update方法的典型用法代码示例。如果您正苦于以下问题:Python Map.update方法的具体用法?Python Map.update怎么用?Python Map.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.routing.Map
的用法示例。
在下文中一共展示了Map.update方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Dispatcher
# 需要导入模块: from werkzeug.routing import Map [as 别名]
# 或者: from werkzeug.routing.Map import update [as 别名]
class Dispatcher(object):
"""Dispatch requests based on a WSGI environment.
The routes are loaded from the <package>/config/routes file, and each line should be blank,
a comment, or one of the following:
<route> <page class>
<route> redirect:<url>
<route> template:<filename>
"""
def __init__(self, app):
"""Load the URL routing map and instantiate the responders."""
self.app = app
# Set up URL routing
self.map = Map()
routes_file = file(os.path.join(app.directory, 'config', 'routes'), 'r')
for line in routes_file:
# Split the line from one of the documented formats
parts = line.split()
if len(parts) == 0 or parts[0][0] == '#':
# Ignore comments and blank lines
continue
if len(parts) != 2:
raise ConfigurationError("Error in routes file: %s" % line)
path, destination = parts
if ':' in destination:
# Responder explicitly specified
responder_name, extra = destination.split(':', 1)
responder_type = responder_types.get(responder_name, None)
if responder_type is None:
raise ConfigurationError("Invalid destination '%s' in routes file" % destination)
responder = responder_type(extra)
else:
# Default to PageResponder if there's no ':' in the destination
responder = PageResponder(destination)
for p, r in responder.get_routes(path): # FIXME: Better names for p and r
rule = Rule(p, endpoint=r, methods=r.methods)
self.map.add(rule)
self.map.update()
def dispatch(self, environ):
try:
request = Request(environ)
urls = self.map.bind_to_environ(environ)
responder, args = urls.match()
with Context(self.app, environ, request, args) as context:
for hook in self.app.get_hook_functions('pre-request'):
hook(context)
context.response = responder(context)
for hook in self.app.get_hook_functions('post-request'):
context.response = hook(context) or context.response
return context.response
# HTTPExceptions are returned as the response, while any other
# exceptions are re-raised to be either caught by the in-browser debugger
# or generate a 500 response.
except HTTPException, e:
return e
示例2: __new__
# 需要导入模块: from werkzeug.routing import Map [as 别名]
# 或者: from werkzeug.routing.Map import update [as 别名]
def __new__(cls, name, bases, attrs):
# Add a url_map to the class
url_map = UrlMap(strict_slashes=False)
# Add a collection of (unbound) view functions
view_functions = {}
for base in bases:
# Extend from url_map of base class
if hasattr(base, 'url_map') and isinstance(base.url_map, UrlMap):
for rule in base.url_map.iter_rules():
url_map.add(rule.empty())
# Extend from view_functions of base class
if hasattr(base, 'view_functions') and isinstance(base.view_functions, dict):
view_functions.update(base.view_functions)
for routeattr, route in attrs.items():
if isinstance(route, _NodeRoute):
# For wrapped routes, add a rule for each layer of wrapping
endpoints = []
while isinstance(route, _NodeRoute):
# Save the endpoint name
endpoints.append(route.endpoint)
# Construct the url rule
url_rule = UrlRule(route.rule, endpoint=route.endpoint, methods=route.methods, defaults=route.defaults)
url_rule.provide_automatic_options = True
url_map.add(url_rule)
route = route.f
# Make a list of endpoints
for e in endpoints:
view_functions[e] = route
# Restore the original function
attrs[routeattr] = route
# Finally, update the URL map and insert it into the class
url_map.update()
attrs['url_map'] = url_map
attrs['view_functions'] = view_functions
return type.__new__(cls, name, bases, attrs)