Werkzeug源码阅读笔记(四)
今天主要讲一下werkzeug中的routing模块。这个模块是werkzeug中的重点模块,Flask中的路由相关的操作使用的都是这个模块
routing模块的用法
在讲解模块的源码之前,先讲讲这个模块怎么用。
创建Map()对象:
>>> m = Map([
... # Static URLs
... Rule('/', endpoint='static/index'),
... Rule('/about', endpoint='static/about'),
... Rule('/help', endpoint='static/help'),
... # Knowledge Base
... Subdomain('kb', [
... Rule('/', endpoint='kb/index'),
... Rule('/browse/', endpoint='kb/browse'),
... Rule('/browse/<int:id>/', endpoint='kb/browse'),
... Rule('/browse/<int:id>/<int:page>', endpoint='kb/browse')
... ])
... ], default_subdomain='www')
我们可以看到,一个Map中以列表的形式包含多个Rule. 示例里面还有个Subdomain,除了Subdomain名外,它里面以列表的形式包含多个Rule,如果没有Subdomain,后面的default_subdomain可以省略(default_subdomain适配于除了Subdomain之外的Rule部分)
在创建了Map的实例后,我们可以为每个Subdomain创建URL适配器
>>> c = m.bind('example.com')
>>> c.build("kb/browse", dict(id=42)) #如果url有参数,使用dict()里面填参数名和值
'http://kb.example.com/browse/42/'
>>> c.build("kb/browse", dict()) #build接受的参数是endpoint,返回url地址
'http://kb.example.com/browse/'
>>> c.build("kb/browse", dict(id=42, page=3))
'http://kb.example.com/browse/42/3'
>>> c.build("static/about")
'/about'
>>> c.build("static/index", force_external=True)
'http://www.example.com/'
>>> c = m.bind('example.com', subdomain='kb')
>>> c.build("static/about")
'http://www.example.com/about'
部分源码分析
在routing模块中有个RuleFactory类,提供了get_rules()工厂方法,该方法的设计目的是使得URL重用. 所有继承该类的类必须实现该方法
比如Subdomain类:
class Subdomain(RuleFactory):
def __init__(self, subdomain, rules):
self.subdomain = subdomain
self.rules = rules
def get_rules(self, map):
for rulefactory in self.rules:
for rule in rulefactory.get_rules(map):
rule = rule.empty()
rule.subdomain = self.subdomain
yield rule
在该类中get_rules方法是一个生成器,调用一次返回一个subdomain中的rule,且该Rule未绑定.
同理还有Submount,EndpointPrefix类,源码差不多,就不细讲了。只说下怎么用:
Submount的用法:
url_map = Map([
Rule('/', endpoint='index'),
Submount('/blog', [
Rule('/', endpoint='blog/index'),
Rule('/entry/<entry_slug>', endpoint='blog/show')
])
])
把submount中第一个元素(路径)跟在原路径后,里面的Rule均以这个路径为挂载点
这里当访问'blog/entry/<entry_slug>'就会找到'blog/show'这个endpoint;当访问'blog'就会找到'blog/index'这个endpoint
EndpointPrefix的用法:
url_map = Map([
Rule('/', endpoint='index'),
EndpointPrefix('blog/', [Submount('/blog', [
Rule('/', endpoint='index'),
Rule('/entry/<entry_slug>', endpoint='show')
])])
])
和上一个示例等效,只不过是提取出了所有endpoint中相同的前缀放在前面
Rule类
在该类的实例中最重要的就是存储了URL地址。以下是它的构造方法的头部:
def __init__(self, string, defaults=None, subdomain=None, methods=None,
build_only=False, endpoint=None, strict_slashes=None,
redirect_to=None, alias=False, host=None)
在该类中,实现了bind方法,源码如下:
def bind(self, map, rebind=False):
if self.map is not None and not rebind:
raise RuntimeError('url rule %r already bound to map %r' %
(self, self.map))
self.map = map
if self.strict_slashes is None:
self.strict_slashes = map.strict_slashes
if self.subdomain is None:
self.subdomain = map.default_subdomain
self.compile()
该方法的作用就是把Rule的实例绑定到一个Map实例上去
Map类
在该类的实例中,存储了很多个Rule类的实例,同时还有部分配置参数。以下是它的构造方法的头部:
def __init__(self, rules=None, default_subdomain='', charset='utf-8',
strict_slashes=True, redirect_defaults=True,
converters=None, sort_parameters=False, sort_key=None,
encoding_errors='replace', host_matching=False)
对照本文中开头的部分,可以发现rules参数是一个列表,该列表中包含了多个Rule的实例
在该类中实现了几个重要的方法:
add()方法:它的作用是把一个Rule的实例添加到该Map实例中,并绑定。源码很简单:
def add(self, rulefactory):
for rule in rulefactory.get_rules(self): #获得rule实例
rule.bind(self) #绑定该实例(源码见上面Rule类中的bind方法)
#在Map的_rules列表中加入该Rule实例,_rules用来装Map初始化函数中rules参数传进的Rules
self._rules.append(rule)
self._rules_by_endpoint.setdefault(rule.endpoint, []).append(rule)
self._remap = True
bind()方法:返回一个MapAdapter类的实例,该类的作用是用来做URL的匹配。bind的头部为:
bind(self, server_name, script_name=None, subdomain=None,
url_scheme='http', default_method='GET', path_info=None,
query_args=None)
MapAdapter类
该类用来做URL匹配。
dispatcher()方法:该方法的作用是,传入path_info,该方法会使用match()方法找到对应的endpoint和相关参数,然后再把这个endpoint作为参数传入view_func视图函数中,返回一个view_func对象. 源码如下
def dispatch(self, view_func, path_info=None, method=None,
catch_http_exceptions=False):
try:
try:
endpoint, args = self.match(path_info, method) #获得endpoint和所需参数
except RequestRedirect as e:
return e
return view_func(endpoint, args)
except HTTPException as e:
if catch_http_exceptions:
return e
raise
match()方法:该方法的作用是,传入path_info和method,该方法会返回对应的endpoint和路径包含的参数,比如:
>>> m = Map([
... Rule('/', endpoint='index'),
... Rule('/downloads/', endpoint='downloads/index'),
... Rule('/downloads/<int:id>', endpoint='downloads/show')
... ])
>>> urls = m.bind("example.com", "/") #urls是MapAdapter对象
>>> urls.match("/", "GET")
('index', {})
>>> urls.match("/downloads/42")
('downloads/show', {'id': 42})
build()方法:该方法与match()对应,传入endpoint和对应参数,返回path_info. 用法如下
>>> m = Map([
... Rule('/', endpoint='index'),
... Rule('/downloads/', endpoint='downloads/index'),
... Rule('/downloads/<int:id>', endpoint='downloads/show')
... ])
>>> urls = m.bind("example.com", "/")
>>> urls.build("index", {})
'/'
>>> urls.build("downloads/show", {'id': 42})
'/downloads/42'
Werkzeug源码阅读笔记(四)的更多相关文章
- werkzeug源码阅读笔记(二) 下
wsgi.py----第二部分 pop_path_info()函数 先测试一下这个函数的作用: >>> from werkzeug.wsgi import pop_path_info ...
- Werkzeug源码阅读笔记(三)
这次主要讲下werkzeug中的Local. 源码在werkzeug/local.py Thread Local 在Python中,状态是保存在对象中.Thread Local是一种特殊的对象,它是对 ...
- werkzeug源码阅读笔记(二) 上
因为第一部分是关于初始化的部分的,我就没有发布出来~ wsgi.py----第一部分 在分析这个模块之前, 需要了解一下WSGI, 大致了解了之后再继续~ get_current_url()函数 很明 ...
- Mina源码阅读笔记(四)—Mina的连接IoConnector2
接着Mina源码阅读笔记(四)-Mina的连接IoConnector1,,我们继续: AbstractIoAcceptor: 001 package org.apache.mina.core.rewr ...
- 源码阅读笔记 - 1 MSVC2015中的std::sort
大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...
- jdk源码阅读笔记-LinkedHashMap
Map是Java collection framework 中重要的组成部分,特别是HashMap是在我们在日常的开发的过程中使用的最多的一个集合.但是遗憾的是,存放在HashMap中元素都是无序的, ...
- HashMap源码阅读笔记
HashMap源码阅读笔记 本文在此博客的内容上进行了部分修改,旨在加深笔者对HashMap的理解,暂不讨论红黑树相关逻辑 概述 HashMap作为经常使用到的类,大多时候都是只知道大概原理,比如 ...
- guavacache源码阅读笔记
guavacache源码阅读笔记 官方文档: https://github.com/google/guava/wiki/CachesExplained 中文版: https://www.jianshu ...
- 【原】AFNetworking源码阅读(四)
[原]AFNetworking源码阅读(四) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇还遗留了很多问题,包括AFURLSessionManagerTaskDe ...
随机推荐
- JQuery(三)——操作HTML和CSS内容
前边我们学习过JS通过DOM来操作HTML(详看DOM(一)——HTML DOM ),这篇博客我们来看一下JQuery是如何方便的对HTML以及CSS进行各种操作呢?顺便两者之间相互比较一下,看其差别 ...
- ./configure: error: the HTTP rewrite module requires the PCRE library
docker中CentOS安装nginx出错,提示没有PCRE,需要安装pcre-devel,同时还需要安装openssl.openssl-devel yum -y install pcre-deve ...
- How to recover a skipped tablespace after an incomplete recovery with resetlogs? [ID 1561645.1]
n this Document Goal Solution This document is being delivered to you via Oracle Support's Rapid ...
- juce AsyncUpdaterMessage 分析
这个类同样是基于 CallbackMessage, 主要目的是为了在主线程中进行回调,只不过在收到消息的时候进行检测,检测消息发送对象是否已经删除,如果消息发送对象已经没了.消息回调最终调用了调用者的 ...
- mysql中select distinct的用法
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但 往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的 ...
- jquerymobile listview 局部刷新
function onSuccess(data, status) { data = $.trim(data); // alert(data); // return; if (data) { $('#l ...
- mysql性能优化学习笔记(2)如何发现有问题的sql
一.使用mysql慢查询日志对有效率问题的sql进行监控 1)开启慢查询 show variables like ‘slow_query_log’;//查看是否开启慢查询日志 ...
- Mining 影响数据挖掘结果的 5 方面
第一个: 数据类型. 对象的不同属性会用不同的数据类型来描述,如 年龄-->int; 生日 -->date;数据挖掘时也要对不同的类型有不同的对待. 第二个: 数据质量. 数据质量直接影 ...
- Python作业day2购物车
流程图: 实现情况: 可自主注册, 登陆系统可购物,充值(暂未实现),查询余额. 撸了两天一夜的代码,不多说,直接上码,注释神马的后面再说 #!/usr/bin/env python # -*- co ...
- Struts2 对Action中所有方法进行输入校验、单个方法进行校验
index.jsp: <body> <s:fielderror /> <form action="${pageContext.request.contextPa ...