django源码阅读

最近再看django-bootstrap-toolkit,一直困惑于静态文件的路径问题。所以只能从源码入手了。   
从manage.py开始。
manage.py 比较简单就几句话。

#!/usr/bin/env python
    #from django.core.management import execute_manager
    import os
    import sys
    
    
    if __name__ == "__main__":
        #加载配置文件
        os.environ.setdefault("DJANGO_SETTINGS_MODULE","settings")
        
        from django.core.management import execute_from_command_line
        #执行配置
        execute_from_command_line(sys.argv)

首先看os.environ.setdefault(),environ为类_Environ的一个实例,它继承自IterableUserDict,而IterableUserDict继承自UserDict,包括setdefault()这个方法也是UserDict的一个方法,看一下setdefault实现的功能:

def setdefault(self, key, failobj=None):
        if key not in self:
            self[key] = failobj
        return self[key]
        
其中的key是 DJANGO_SETTINGS_MODULE,这里的self是一个字典,判断key是不是在里面,不在里面就做一个键值绑定。然后返回。

下面进入execute_from_command_line(sys.argv),sys.argv位一个参数执行的列表,像我们执行python  manage.py runserver时,manage.py位sys.argv[0],runserver为sys.argv[1],以此类推,如果有更多参数加的话。

进入django的core模块下,找到management模块。在__init__.py下可以找到该函数

def execute_from_command_line(argv=None):
        """
        A simple method that runs a ManagementUtility.
        """
        utility = ManagementUtility(argv)
        utility.execute()
我们输入的参数便传到了这个函数里,其中ManagementUtility位一个类穿进去的参数列表会进入一个简单的初始化

def __init__(self, argv=None):
        self.argv = argv or sys.argv[:]
        self.prog_name = os.path.basename(self.argv[0])

将参数列表赋给argv,然后返回sys.argv[0]的文件名字给prog_name。  
实例化完了之后,再看utility.execute()即类ManagementUtility的execute()方法。

parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
                                        version=get_version(),
                                        option_list=BaseCommand.option_list)
            self.autocomplete()
            try:
                options, args = parser.parse_args(self.argv)
                handle_default_options(options)
            except:
                pass # Ignore any option errors at this point.

try:
                subcommand = self.argv[1]
            except IndexError:
                subcommand = ‘help‘ # Display help if no arguments were given.

if subcommand == ‘help‘:
            
                    if len(args) <= 2:
                        parser.print_lax_help()
                        sys.stdout.write(self.main_help_text() + ‘\n‘)
                    elif args[2] == ‘--commands‘:
                        sys.stdout.write(self.main_help_text(commands_only=True) + ‘\n‘)
                    else:
                        self.fetch_command(args[2]).print_help(self.prog_name, args[2])
            elif subcommand == ‘version‘:
                sys.stdout.write(parser.get_version() + ‘\n‘)
            # Special-cases: We want ‘django-admin.py --version‘ and
            # ‘django-admin.py --help‘ to work, for backwards compatibility.
            elif self.argv[1:] == [‘--version‘]:
            # LaxOptionParser already takes care of printing the version.
                pass
            elif self.argv[1:] in ([‘--help‘], [‘-h‘]):
                parser.print_lax_help()
                sys.stdout.write(self.main_help_text() + ‘\n‘)
            else:
                self.fetch_command(subcommand).run_from_argv(self.argv)

第一个语句为词法分析,可是找遍了LaxOptionParser这个类也没有找到入口,里面并没有__init__,只有几个函数,可是python也没有正式的构造函数重载的什么的,后来发现此类继承自OptionParser这个类,它来自于optparse模块,在OptionParser模块里有一个__init__()函数,会接受若干个参数,进行初始化,这里只是传递了3个参数,usage传递了一个字符串,version为版本,BaseCommand.option_list为一个关于命令行的元组,其中调用了make_option这个类__init__进行一些初始化,主要初始化_short_opts和_long_opts这两个参数,并且进行了一些检查。_short_opts为短命令,_long_opts为长命令,如-h,--help分别为短命令和长命令。  
parser为LaxOptionParser的一个实例。 parser.parse_args(self.argv)这个函数会根据上面初始化的内容进行解析命令行参数,之后返回两个值:options,它是一个对象(,保存有命令行参数值。只要知道命令行参数名,如 file,就可以访问其对应的值: options.file 。args,它是一个由 positional arguments 组成的列表。

def handle_default_options(options):
        """
        Include any default options that all commands should accept here
        so that ManagementUtility can handle them before searching for
        user commands.

"""
            if options.settings:
                os.environ[‘DJANGO_SETTINGS_MODULE‘] = options.settings
            if options.pythonpath:
                sys.path.insert(0, options.pythonpath)
                
handle_default_options将解析出来的options对象当做参数,判断settings和pythonpath是否存在,然后                
设置环境变量和python模块的搜索路径。   
       
接下来是获得命令行的命令参数self.argv[1],并判断这个命令,包括错误处理,是否时help,是否是version,根据不同的情况展示不同的信息。

最重要的是最后一句,即前面的情况都不是,就进入self.fetch_command(subcommand).run_from_argv(self.argv)
       
       
    def fetch_command(self, subcommand):
        """
        Tries to fetch the given subcommand, printing a message with the
        appropriate command called from the command line (usually
        "django-admin.py" or "manage.py") if it can‘t be found.
        """
        # Get commands outside of try block to prevent swallowing exceptions
        commands = get_commands()
        try:
            app_name = commands[subcommand]
        except KeyError:
            sys.stderr.write("Unknown command: %r\nType ‘%s help‘ for usage.\n" %
                (subcommand, self.prog_name))
            sys.exit(1)
        if isinstance(app_name, BaseCommand):
            # If the command is already loaded, use it directly.
            klass = app_name
        else:
            klass = load_command_class(app_name, subcommand)
        return klass
        
获得django/core/management/commands目录下与INSTALLED_APPS/management/commands目录下的子命令对应的模块前缀

def load_command_class(app_name, name):
        """
        Given a command name and an application name, returns the Command
        class instance. All errors raised by the import process
        (ImportError, AttributeError) are allowed to propagate.
        """
        module = import_module(‘%s.management.commands.%s‘ % (app_name, name))
        return module.Command()

返回Command类的实例。进入management/commands下进入每个文件会发现,每个都有Command类对应相应的命令。

self.fetch_command(subcommand).run_from_argv(self.argv)找到我们输入的命令参数,并且使用run_from_argv执行。

进入runserver这个命令下看一下,Command这个类继承了BaseCommand这个类,BaseCommand在Base.py中,里面有run_from_arv这个方法,在其他的命令里
有的重写了这个方法,不过runserver没有。

def run_from_argv(self, argv):
        """
        Set up any environment changes requested (e.g., Python path
        and Django settings), then run this command. If the
        command raises a ``CommandError``, intercept it and print it sensibly
        to stderr. If the ``--traceback`` option is present or the raised
        ``Exception`` is not ``CommandError``, raise it.
        """
        #设置环境变量,然后运行这个命令
        #如python  manage.py  runserver,  manage.py就是argv[0],runserver是argv[1]
        #create_parser直接调用OptionParser(prog=prog_name,usage=self.usage(subcommand),version=self.get_version(),option_list=self.option_list)
        #将manage.py 和runserver加入参数列表
        #接下来用parser.parse_args进行解析,argv[2]之后为默认参数,ip地址还有端口号,我们也可以显示的传进去,默认是127.0.0.1,端口号8000.
        #下面又到了handle_default_options函数。
        #接下来是执行execute函数,还有异常捕获。
        parser = self.create_parser(argv[0], argv[1])
        options, args = parser.parse_args(argv[2:])
        handle_default_options(options)
        try:
            self.execute(*args, **options.__dict__)
        except Exception as e:
            if options.traceback or not isinstance(e, CommandError):
                raise

# self.stderr is not guaranteed to be set here
            stderr = getattr(self, ‘stderr‘, OutputWrapper(sys.stderr, self.style.ERROR))
            stderr.write(‘%s: %s‘ % (e.__class__.__name__, e))
            sys.exit(1)

下面进入execute函数,前边是一些设置和错误检查,最主要的是 output = self.handle(*args, **options),可以发现handle在BaseCommand里是空的,每个命令对其进行了重写。  
runserver也是。看看runserver的handle:

def handle(self, addrport=‘‘, *args, **options):
        #导入设置模块
        from django.conf import settings
        #DEBUG和ALLOWED_HOSTS的设置
        if not settings.DEBUG and not settings.ALLOWED_HOSTS:
            raise CommandError(‘You must set settings.ALLOWED_HOSTS if DEBUG is False.‘)
        #ipv6的设置
        self.use_ipv6 = options.get(‘use_ipv6‘)
        if self.use_ipv6 and not socket.has_ipv6:
            raise CommandError(‘Your Python does not support IPv6.‘)
        if args:
            raise CommandError(‘Usage is runserver %s‘ % self.args)
        self._raw_ipv6 = False
        if not addrport:
            self.addr = ‘‘
            self.port = DEFAULT_PORT
        else:
            #如果设置了ip地址和端口号,用正则匹配出来
            m = re.match(naiveip_re, addrport)
            if m is None:
                raise CommandError(‘"%s" is not a valid port number ‘
                                   ‘or address:port pair.‘ % addrport)
            self.addr, _ipv4, _ipv6, _fqdn, self.port = m.groups()
            if not self.port.isdigit():
                raise CommandError("%r is not a valid port number." % self.port)
            if self.addr:
                if _ipv6:
                    self.addr = self.addr[1:-1]
                    self.use_ipv6 = True
                    self._raw_ipv6 = True
                elif self.use_ipv6 and not _fqdn:
                    raise CommandError(‘"%s" is not a valid IPv6 address.‘ % self.addr)
        if not self.addr:
            #如果没有设置ip地址使用127.0.0.1代替
            self.addr = ‘::1‘ if self.use_ipv6 else ‘127.0.0.1‘
            self._raw_ipv6 = bool(self.use_ipv6)
         #运行命令
        self.run(*args, **options)
        
runserver里的run方法主要时调用了inner_run(*args, **options)这个方法。

def inner_run(self, *args, **options):
         #省略了部分代码
            #很熟悉吧,这就是我们开始运行时,终端上输出的信息啊。   
            self.stdout.write((
                "%(started_at)s\n"
                "Django version %(version)s, using settings %(settings)r\n"
                "Starting development server at http://%(addr)s:%(port)s/\n"
                "Quit the server with %(quit_command)s.\n"
            ) % {
                "started_at": now,
                "version": self.get_version(),
                "settings": settings.SETTINGS_MODULE,
                "addr": ‘[%s]‘ % self.addr if self._raw_ipv6 else self.addr,
                "port": self.port,
                "quit_command": quit_command,
            })
            #加载编码设置
            translation.activate(settings.LANGUAGE_CODE)
    
            try:
                handler = self.get_handler(*args, **options)
                run(self.addr, int(self.port), handler,
                    ipv6=self.use_ipv6, threading=threading)
    
              #省略部分代码
          
          
          
        def get_handler(self, *args, **options):
            """
            Returns the default WSGI handler for the runner.
            """
            return get_internal_wsgi_application()

get_internal__wsgi_application()和run(self.addr, int(self.port), handler,ipv6=self.use_ipv6, threading=threading)都在django.core.servers.basehttp中:

def get_internal_wsgi_application():
        
        from django.conf import settings
        #在settings模块中并没有找到WSGI_APPLICATION这个环境变量,因此app_path位空
        app_path = getattr(settings, ‘WSGI_APPLICATION‘)
        if app_path is None:
            return get_wsgi_application()
            
在django/core下wsig.py中的文件如下:就几句话   
       
    import django
    from django.core.handlers.wsgi import WSGIHandler

def get_wsgi_application():
    
        #setup是加载log和settings.INSTALLED_APPS
        django.setup()
        #返回WSGIHhadler类的一个实例
        return WSGIHandler()
        
        
    class WSGIHandler(base.BaseHandler):
        #初始化一个线程锁
        initLock = Lock()
        #WSGIRequest为http.HttpRequest的一个子类,
        #WSGIRequest实现了wsgi规范
        request_class = WSGIRequest
        
下面进入run方法:run(self.addr, int(self.port), handler,ipv6=self.use_ipv6, threading=threading),标准的wsgi实现:

def run(addr, port, wsgi_handler, ipv6=False, threading=False):
        server_address = (addr, port)
        if threading:
            httpd_cls = type(str(‘WSGIServer‘), (socketserver.ThreadingMixIn, WSGIServer), {})
        else:
            httpd_cls = WSGIServer
        httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
        httpd.set_app(wsgi_handler)
        httpd.serve_forever()

httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)为实例化一个WSGIServer类,最终的实例化方法在父类SocketServer中的TCPServer和BaseServer中。包括初始化线程,初始化网络句柄,像下面的__is_shut_down和__shutdown_request都是在其中初始化的

#处理一个http请求直到关闭
       def serve_forever(self, poll_interval=0.5):
            #__is_shut_down为一个初始化的threading.Event()的句柄,用于线程间通信
            #.clear()将标识设置为false
            self.__is_shut_down.clear()
            try:
                while not self.__shutdown_request:
                    #下面的函数就是一个封装好了的select函数,后面的四个为传递给select函数的参数,分别表示输入对象列表,输出对象列表,错误对象列表以及超时时间。
                    #返回值为三个列表,代表可写,可读,错误的事件的对象列表。
                    r, w, e = _eintr_retry(select.select, [self], [], [],
                                           poll_interval)
                    #判断self事件是否在可读对象列表中,在就进入进行处理
                    if self in r:
                        #处理连接请求
                        self._handle_request_noblock()
            finally:
                self.__shutdown_request = False
                #将标识设置为true
                self.__is_shut_down.set()
                
       #对select函数的封装        
       def _eintr_retry(func, *args):
        """restart a system call interrupted by EINTR"""
          while True:
            try:
                return func(*args)
            except (OSError, select.error) as e:
                if e.args[0] != errno.EINTR:
                    raise

def _handle_request_noblock(self):
       
            try:
                #返回请求句柄,客户端地址,get_request()中调用了self.socket.accept()来实现客户端的连接
                request, client_address = self.get_request()
            except socket.error:
                return
            if self.verify_request(request, client_address):
                try:
                    #真正的处理连接请求的地方,调用了self.finish_request(request, client_address)
                    self.process_request(request, client_address)
                except:
                    self.handle_error(request, client_address)
                    self.shutdown_request(request)

def finish_request(self, request, client_address):
            """Finish one request by instantiating RequestHandlerClass."""
            此处的RequestHandlerClass为初始化的时候传进来的WSGIRequestHandler,实现了回调。
            self.RequestHandlerClass(request, client_address, self)    
            
在WSGIRequestHandler中实现下面的初始化函数:
    
     def __init__(self, *args, **kwargs):
        from django.conf import settings
        self.admin_static_prefix = urljoin(settings.STATIC_URL, ‘admin/‘)
        # We set self.path to avoid crashes in log_message() on unsupported
        # requests (like "OPTIONS").
        self.path = ‘‘
        self.style = color_style()
        #调用父类的WSGIRequestHandler进行初始化,它的父类是simple_server.py里的WSGIRequestHandler,它里面没有__init__,继续找它的父类
        #如此重复,在最终的基类中可以找到__init__方法,位于SocketServer.py中的 BaseRequestHandler类。
        super(WSGIRequestHandler, self).__init__(*args, **kwargs)

def __init__(self, request, client_address, server):
            self.request = request
            self.client_address = client_address
            self.server = server
            #setup函数
            self.setup()
            try:
                self.handle()
            finally:
                self.finish()

def setup(self):
                pass

def handle(self):
                pass

def finish(self):
                pass
可以看到里面的方法并没有实现,只是给出了定义,实现都在子类中。

再回到WSGIRequestHandler类中,在simple_server.py中实现了handle函数,实现对请求的处理

def handle(self):
            """Handle a single HTTP request"""

self.raw_requestline = self.rfile.readline()
            if not self.parse_request(): # An error code has been sent, just exit
                return
            #传入的参数,读,写,错误,环境变量。在其父类SimpleHandler中进行了初始化,并且打开了多线程和多进程选项
            handler = ServerHandler(
                self.rfile, self.wfile, self.get_stderr(), self.get_environ()
            )
            handler.request_handler = self      # backpointer for logging
            在SimpleHandler的父类BaseHandler中含实现了run方法。此处get_app()是上面的run方法中我们传进去的wsgi_handler,httpd.set_app(wsgi_handler)
            handler.run(self.server.get_app())

def run(self, application):
      
            try:
                #设置环境变量
                self.setup_environ()
                #执行WSGIHandler(self.environ,self.start_response)
                #因为类中实现了__call__方法,调用类的实例就相当于调用了__call__方法
                self.result = application(self.environ, self.start_response)
                self.finish_response()
            except:
                try:
                    self.handle_error()
                except:
                    # If we get an error handling an error, just give up already!
                    self.close()
                    raise   # ...and let the actual server figure it out.

def __call__(self, environ, start_response):
        # Set up middleware if needed. We couldn‘t do this earlier, because
        # settings weren‘t available.
        if self._request_middleware is None:
            with self.initLock:
                try:
                    # Check that middleware is still uninitialised.
                    if self._request_middleware is None:
                    #尝试加载中间件
                        self.load_middleware()
                except:
                    # Unload whatever middleware we got
                    self._request_middleware = None
                    raise
        #设置脚本路径
        set_script_prefix(base.get_script_name(environ))
        signals.request_started.send(sender=self.__class__)
        try:
            request = self.request_class(environ)
        except UnicodeDecodeError:
            logger.warning(‘Bad Request (UnicodeDecodeError)‘,
                exc_info=sys.exc_info(),
                extra={
                    ‘status_code‘: 400,
                }
            )
            response = http.HttpResponseBadRequest()
        else:
            response = self.get_response(request)

response._handler_class = self.__class__

status = ‘%s %s‘ % (response.status_code, response.reason_phrase)
        response_headers = [(str(k), str(v)) for k, v in response.items()]
        for c in response.cookies.values():
            response_headers.append((str(‘Set-Cookie‘), str(c.output(header=‘‘))))
        start_response(force_str(status), response_headers)
        return response
                
        
以上为我阅读django源码的一些心得,大体了解了django的一点原理

时间: 2024-11-08 23:24:42

django源码阅读的相关文章

CI框架源码阅读笔记3 全局函数Common.php

从本篇开始,将深入CI框架的内部,一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说,全局函数具有最高的加载优先权,因此大多数的框架中BootStrap引导文件都会最先引入全局函数,以便于之后的处理工作). 打开Common.php中,第一行代码就非常诡异: if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 上一篇(CI框架源码阅读笔记2 一切的入口 index

淘宝数据库OceanBase SQL编译器部分 源码阅读--生成逻辑计划

body, td { font-family: tahoma; font-size: 10pt; } 淘宝数据库OceanBase SQL编译器部分 源码阅读--生成逻辑计划 SQL编译解析三部曲分为:构建语法树,生成逻辑计划,指定物理执行计划.第一步骤,在我的上一篇博客淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树里做了介绍,这篇博客主要研究第二步,生成逻辑计划. 一. 什么是逻辑计划?我们已经知道,语法树就是一个树状的结构组织,每个节点代表一种类型的语法含义.如

JDK部分源码阅读与理解

本文为博主原创,允许转载,但请声明原文地址:http://www.coselding.cn/article/2016/05/31/JDK部分源码阅读与理解/ 不喜欢重复造轮子,不喜欢贴各种东西.JDK代码什么的,让整篇文章很乱...JDK源码谁都有,没什么好贴的...如果你没看过JDK源码,建议打开Eclipse边看源码边看这篇文章,看过的可以把这篇文章当成是知识点备忘录... JDK容器类中有大量的空指针.数组越界.状态异常等异常处理,这些不是重点,我们关注的应该是它的一些底层的具体实现,这篇

如何阅读Java源码 阅读java的真实体会

刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧,如果你从来没有学过Java,或是任何一门编程语言如C++,一开始去啃<Core Java>,你是很难从中吸收到营养的,特别是<深入Java虚拟机>这类书,别人觉得好,未必适合现在的你. 虽然Tomcat的源码很漂亮,但我绝不建议你一开始就读它.我文中会专门谈到这个,暂时不展开. 强烈

Memcache-Java-Client-Release源码阅读(之七)

一.主要内容 本章节的主要内容是介绍Memcache Client的Native,Old_Compat,New_Compat三个Hash算法的应用及实现. 二.准备工作 1.服务器启动192.168.0.106:11211,192.168.0.106:11212两个服务端实例. 2.示例代码: String[] servers = { "192.168.0.106:11211", "192.168.0.106:11212" }; SockIOPool pool =

源码阅读笔记 - 1 MSVC2015中的std::sort

大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格式化,去掉或者展开用于条件编译或者debug检查的宏,依重要程度重新排序函数,但是不会改变命名方式(虽然MSVC的STL命名实在是我不能接受的那种),对于代码块的解释会在代码块前(上面)用注释标明. template<class _RanIt, class _Diff, class _Pr> in

JDK 源码 阅读 - 2 - 设计模式 - 创建型模式

A.创建型模式 抽象工厂(Abstract Factory) javax.xml.parsers.DocumentBuilderFactory DocumentBuilderFactory通过FactoryFinder实例化具体的Factory. 使用例子: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilder

CI源码阅读

CodeIgniter源码分析 http://calixwu.com/2014/11/codeigniter-yuanmafenxi.html CI框架源码阅读笔记 http://www.cnblogs.com/ohmygirl/p/4052686.html

《java.util.concurrent 包源码阅读》13 线程池系列之ThreadPoolExecutor 第三部分

这一部分来说说线程池如何进行状态控制,即线程池的开启和关闭. 先来说说线程池的开启,这部分来看ThreadPoolExecutor构造方法: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecut