flask源码笔记:二,Flask源码目录结构
2016-03-02 18:38
627 查看
在目录下执行ls命令可以了解到,Flask源码目录的大致结构,如果需要以树状显示目录结构,可以使用命令tree。
(venv)[root@10-9-21-98 flask]# ls
app.py config.pyc exthook.pyc json.py sessions.pyc testsuite
app.pyc ctx.py globals.py json.pyc signals.py views.py
blueprints.py ctx.pyc globals.pyc logging.py signals.pyc views.pyc
blueprints.pyc debughelpers.py helpers.py logging.pyc templating.py wrappers.py
_compat.py debughelpers.pyc helpers.pyc module.py templating.pyc wrappers.pyc
_compat.pyc ext __init__.py module.pyc testing.py
config.py exthook.py __init__.pyc sessions.py testing.pyc
值得了解的是,pyc后缀的是运行后的二进制编译文件,可以忽略。粗体字是目录,内容需要进一步了解
(venv)[root@10-9-21-98 flask]# ls ext
__init__.py __init__.pyc
ext目录是一个中规中矩的python包
(venv)[root@10-9-21-98 flask]# ls testsuite/
appctx.py config.py ext.py regression.py static test_apps
appctx.pyc config.pyc ext.pyc regression.pyc subclassing.py testing.py
basic.py deprecations.py helpers.py reqctx.py subclassing.pyc testing.pyc
basic.pyc deprecations.pyc helpers.pyc reqctx.pyc templates views.py
blueprints.py examples.py __init__.py signals.py templating.py views.pyc
blueprints.pyc examples.pyc __init__.pyc signals.pyc templating.pyc
testsuite目录也是一个python包,但是其中的文件结构居然和Flask源码的文件结构大同小异,特别是文件名,由此推断:testsuite可能是一个基于测试套件的Flask子框架。
了解一个Python包,首先看其__init__.py文件。
所以,我们可以把__init__.py文件,看做是包的整体部分,而非其下的文件和目录部分。在编辑器中查看文件如下:
(venv)[root@10-9-21-98 flask]# cat __init__.py
# -*- coding: utf-8 -*-
"""
flask
~~~~~
A microframework based on Werkzeug. It's extensively documented
and follows best practice patterns.
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
__version__ = '0.10.1'
# utilities we import from Werkzeug and Jinja2 that are unused
# in the module but are exported as public interface.
from werkzeug.exceptions import abort
from werkzeug.utils import redirect
from jinja2 import Markup, escape
from .app import Flask, Request, Response
from .config import Config
from .helpers import url_for, flash, send_file, send_from_directory, \
get_flashed_messages, get_template_attribute, make_response, safe_join, \
stream_with_context
from .globals import current_app, g, request, session, _request_ctx_stack, \
_app_ctx_stack
from .ctx import has_request_context, has_app_context, \
after_this_request, copy_current_request_context
from .module import Module
from .blueprints import Blueprint
from .templating import render_template, render_template_string
# the signals
from .signals import signals_available, template_rendered, request_started, \
request_finished, got_request_exception, request_tearing_down, \
appcontext_tearing_down, appcontext_pushed, \
appcontext_popped, message_flashed
# We're not exposing the actual json module but a convenient wrapper around
# it.
from . import json
# This was the only thing that flask used to export at one point and it had
# a more generic name.
jsonify = json.jsonify
# backwards compat, goes away in 1.0
from .sessions import SecureCookieSession as Session
json_available = True
1,模块描述介绍了flask模块的简单信息。
2,版本信息的变量设置
3,导入第三方库werkzeug、jinja2中的四个函数
4,导入自定义模块中的对象
5,增设两个变量
分析
所有的设置的变量和导入的函数等对象,都是作为Flask包对外的接口存在。从这些对象中,我们可以找到创建flask实例常用的一些对象:Flask,url_for,current_app等。
从不同模块导入的对象具有不同的种类的功能,由此,我们可以推断不同模块的实现的功能大致是什么。
from .app import Flask, Request, Response
app.py主要提供创建flask实例和对请求、响应进行处理的功能。
from .config import Config
config.py主要提供配置文件的功能
from .helpers import url_for, flash, send_file, send_from_directory, \
get_flashed_messages, get_template_attribute, make_response, safe_join, \
stream_with_context
helpers.py主要提供诸多辅助功能
from .globals import current_app, g, request, session, _request_ctx_stack, \
_app_ctx_stack
globals.py主要提供全局变量,局部变量和上下文管理器的实例
from .ctx import has_request_context, has_app_context, \
after_this_request, copy_current_request_context
ctx.py主要定义了上下文管理器的类
from .module import Module
module.py主要提供模块功能,已被bluprint替代
from .blueprints import Blueprint
blueprints.py提供蓝本功能
from .templating import render_template, render_template_string
templating.py主要提供模板渲染功能
from .signals import signals_available, template_rendered, request_started, \
request_finished, got_request_exception, request_tearing_down, \
appcontext_tearing_down, appcontext_pushed, \
appcontext_popped, message_flashed
signals.py主要提供不同机制的信号实例
from . import json
json.py主要提供json格式数据的解析功能
另外的诸如:
ext目录:提供flask.ext.扩展名的方式来导入“flask_扩展名”和“flaskext.扩展名”的功能
sessions.py :提供session的类定义,他包含了cookie机制
testing.py:定义用于测试而非生产的一些基类、函数等
views.py:提供另一种以类来定义视图函数的方式,不常用
wrappers.py:定义了对请求和响应的封装类
logging:定义了日志管理器的类和创建函数
exthook.py:提供了ext目录需要用到的类,即导入钩子的类
debughelpers.py:定义了各种debug模式的错误类型
_compat.py :定义了对py2和py3的兼容,涉及到不同版本的对象,先在该文件中进行校验处理。
到此,我们对Flask源码目录也有一个大致了解了。
(venv)[root@10-9-21-98 flask]# ls
app.py config.pyc exthook.pyc json.py sessions.pyc testsuite
app.pyc ctx.py globals.py json.pyc signals.py views.py
blueprints.py ctx.pyc globals.pyc logging.py signals.pyc views.pyc
blueprints.pyc debughelpers.py helpers.py logging.pyc templating.py wrappers.py
_compat.py debughelpers.pyc helpers.pyc module.py templating.pyc wrappers.pyc
_compat.pyc ext __init__.py module.pyc testing.py
config.py exthook.py __init__.pyc sessions.py testing.pyc
值得了解的是,pyc后缀的是运行后的二进制编译文件,可以忽略。粗体字是目录,内容需要进一步了解
(venv)[root@10-9-21-98 flask]# ls ext
__init__.py __init__.pyc
ext目录是一个中规中矩的python包
(venv)[root@10-9-21-98 flask]# ls testsuite/
appctx.py config.py ext.py regression.py static test_apps
appctx.pyc config.pyc ext.pyc regression.pyc subclassing.py testing.py
basic.py deprecations.py helpers.py reqctx.py subclassing.pyc testing.pyc
basic.pyc deprecations.pyc helpers.pyc reqctx.pyc templates views.py
blueprints.py examples.py __init__.py signals.py templating.py views.pyc
blueprints.pyc examples.pyc __init__.pyc signals.pyc templating.pyc
testsuite目录也是一个python包,但是其中的文件结构居然和Flask源码的文件结构大同小异,特别是文件名,由此推断:testsuite可能是一个基于测试套件的Flask子框架。
了解一个Python包,首先看其__init__.py文件。
所以,我们可以把__init__.py文件,看做是包的整体部分,而非其下的文件和目录部分。在编辑器中查看文件如下:
(venv)[root@10-9-21-98 flask]# cat __init__.py
# -*- coding: utf-8 -*-
"""
flask
~~~~~
A microframework based on Werkzeug. It's extensively documented
and follows best practice patterns.
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
__version__ = '0.10.1'
# utilities we import from Werkzeug and Jinja2 that are unused
# in the module but are exported as public interface.
from werkzeug.exceptions import abort
from werkzeug.utils import redirect
from jinja2 import Markup, escape
from .app import Flask, Request, Response
from .config import Config
from .helpers import url_for, flash, send_file, send_from_directory, \
get_flashed_messages, get_template_attribute, make_response, safe_join, \
stream_with_context
from .globals import current_app, g, request, session, _request_ctx_stack, \
_app_ctx_stack
from .ctx import has_request_context, has_app_context, \
after_this_request, copy_current_request_context
from .module import Module
from .blueprints import Blueprint
from .templating import render_template, render_template_string
# the signals
from .signals import signals_available, template_rendered, request_started, \
request_finished, got_request_exception, request_tearing_down, \
appcontext_tearing_down, appcontext_pushed, \
appcontext_popped, message_flashed
# We're not exposing the actual json module but a convenient wrapper around
# it.
from . import json
# This was the only thing that flask used to export at one point and it had
# a more generic name.
jsonify = json.jsonify
# backwards compat, goes away in 1.0
from .sessions import SecureCookieSession as Session
json_available = True
1,模块描述介绍了flask模块的简单信息。
2,版本信息的变量设置
3,导入第三方库werkzeug、jinja2中的四个函数
4,导入自定义模块中的对象
5,增设两个变量
分析
所有的设置的变量和导入的函数等对象,都是作为Flask包对外的接口存在。从这些对象中,我们可以找到创建flask实例常用的一些对象:Flask,url_for,current_app等。
从不同模块导入的对象具有不同的种类的功能,由此,我们可以推断不同模块的实现的功能大致是什么。
from .app import Flask, Request, Response
app.py主要提供创建flask实例和对请求、响应进行处理的功能。
from .config import Config
config.py主要提供配置文件的功能
from .helpers import url_for, flash, send_file, send_from_directory, \
get_flashed_messages, get_template_attribute, make_response, safe_join, \
stream_with_context
helpers.py主要提供诸多辅助功能
from .globals import current_app, g, request, session, _request_ctx_stack, \
_app_ctx_stack
globals.py主要提供全局变量,局部变量和上下文管理器的实例
from .ctx import has_request_context, has_app_context, \
after_this_request, copy_current_request_context
ctx.py主要定义了上下文管理器的类
from .module import Module
module.py主要提供模块功能,已被bluprint替代
from .blueprints import Blueprint
blueprints.py提供蓝本功能
from .templating import render_template, render_template_string
templating.py主要提供模板渲染功能
from .signals import signals_available, template_rendered, request_started, \
request_finished, got_request_exception, request_tearing_down, \
appcontext_tearing_down, appcontext_pushed, \
appcontext_popped, message_flashed
signals.py主要提供不同机制的信号实例
from . import json
json.py主要提供json格式数据的解析功能
另外的诸如:
ext目录:提供flask.ext.扩展名的方式来导入“flask_扩展名”和“flaskext.扩展名”的功能
sessions.py :提供session的类定义,他包含了cookie机制
testing.py:定义用于测试而非生产的一些基类、函数等
views.py:提供另一种以类来定义视图函数的方式,不常用
wrappers.py:定义了对请求和响应的封装类
logging:定义了日志管理器的类和创建函数
exthook.py:提供了ext目录需要用到的类,即导入钩子的类
debughelpers.py:定义了各种debug模式的错误类型
_compat.py :定义了对py2和py3的兼容,涉及到不同版本的对象,先在该文件中进行校验处理。
到此,我们对Flask源码目录也有一个大致了解了。
相关文章推荐
- Python动态类型的学习---引用的理解
- 从源码安装Mysql/Percona 5.5
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 如何使用 Flask 编写 Python Web API
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例