Exemple #1
0
 def input(self, **kw):
     """
     返回一个由传入的数据和从environ里取出的数据 组成的Dict对象,Dict对象的定义 见db模块
     Get input as dict from request, fill dict using provided default value if key not exist.
     i = ctx.request.input(role='guest')
     i.role ==> 'guest'
     >>> from StringIO import StringIO
     >>> r = Request({'REQUEST_METHOD':'POST', 'wsgi.input':StringIO('a=1&b=M%20M&c=ABC&c=XYZ&e=')})
     >>> i = r.input(x=2008)
     >>> i.a
     u'1'
     >>> i.b
     u'M M'
     >>> i.c
     u'ABC'
     >>> i.x
     2008
     >>> i.get('d', u'100')
     u'100'
     >>> i.x
     2008
     """
     copy = Dict(**kw)
     raw = self._get_raw_input()
     for k, v in raw.iteritems():
         copy[k] = v[0] if isinstance(v, list) else v
     return copy
Exemple #2
0
    def get_wsgi_application(self, debug=False):
        self._check_not_running()
        if debug:
            self._get_dynamic.append(StaticFileRoute())
        self._running = True

        _application = Dict(document_root=self._document_root)

        # 根据path_info,从保存的路由信息里,找到相应的处理函数
        def fn_route():
            request_method = ctx.request.request_method
            path_info = ctx.request.path_info
            if request_method == 'GET':
                fn = self._get_static.get(path_info, None)
                if fn:
                    return fn()
                for fn in self._get_dynamic:
                    args = fn.match(path_info)
                    if args:
                        return fn(*args)
                raise HttpError.notfound()
            if request_method == 'POST':
                fn = self._post_static.get(path_info, None)
                if fn:
                    return fn()
                for fn in self._post_dynamic:
                    args = fn.match(path_info)
                    if args:
                        return fn(*args)
                raise HttpError.notfound()
            raise HttpError.badrequest()

        fn_exec = _build_interceptor_chain(fn_route, *self._interceptors)
        fn_exec = _build_interceptor_chain(fn_route, *self._interceptors)

        def wsgi(env, start_response):
            """
            WSGI 处理函数
            """
            ctx.application = _application
            ctx.request = Request(env)
            response = ctx.response = Response()
            try:
                r = fn_exec()
                if isinstance(r, Template):
                    r['ctx'] = ctx
                    r = self._template_engine(r.template_name, r.model)
                if isinstance(r, unicode):
                    r = r.encode('utf-8')
                if r is None:
                    r = []
                start_response(response.status, response.headers)
                return r
            except _RedirectError, e:
                response.set_header('Location', e.location)
                start_response(e.status, response.headers)
                return []
            except _HttpError, e:
                start_response(e.status, response.headers)
                return ['<html><body><h1>', e.status, '</h1></body></html>']
Exemple #3
0
    def get_wsgi_application(self, debug=False):
        self._check_not_running()
        if debug:
            self._get_dynamic.append(
                StaticFileRoute())  #TODO:wht dynamic add staticroute???
            # 因为需要根据match的结果,再去判断是否存在文件,而不是把所有可能文件路径都存储在static get 字典
        self._running = True

        _application = Dict(document_root=self._document_root
                            )  # _application={'document_root':_document_root}

        # 根据请求的path,method,从字典里找出相应的处理函数,执行fn()
        def fn_route():
            request_method = ctx.request.request_method
            path_info = ctx.request.path_info  # 这个事请求里面method和info,从相应字典里取出相应的处理函数
            if request_method == 'GET':
                fn = self._get_static.get(path_info, None)
                if fn:
                    return fn()
                for fn in self._get_dynamic:
                    args = fn.match(path_info)  # TODO 大写加粗,这就是那个提取url里参数的那个!
                    if args:
                        return fn(*args)
                raise HttpError.not_found()
            if request_method == 'POST':
                fn = self._post_static.get(path_info, None)
                if fn:
                    return fn
                for fn in self._post_dynamic:
                    args = fn.match(path_info)  # 从列表里找到符合url的fn,并取出参数,传递给它
                    if args:
                        return fn(*args)
                raise HttpError.not_found()
            raise HttpError.badrequest()

        # 执行顺序:fi1_before,fi2_before,fn,fi2_after,fi1_after
        fn_exec = _build_interceptor_chain(fn_route, *self._interceptors)

        def wsgi(env, start_response):
            ctx.application = _application  # application 里面有document_root属性
            ctx.request = Request(env)
            response = ctx.response = Response()
            try:
                r = fn_exec()
                if isinstance(r, Template):
                    r = self._template_engine(r.template_name, r.model)
                if isinstance(r, unicode):
                    r = r.encode('utf-8')
                if r is None:
                    r = []
                start_response(response.status, response.headers)
                return r
            except _RedirectError, e:
                response.set_header('Location', e.location)
                start_response(e.status, response.headers)
                return []
            except _HttpError, e:
                start_response(e.status, response.headers)
                return ['<html><body><h1>', e.status, '</h1></body></html>']
Exemple #4
0
 def input(self, **kw):
     """
     返回一个由传入的数据和从environ里取出的数据 组成的Dict对象,Dict对象的定义 见db模块
     """
     copy = Dict(**kw)
     raw = self._get_raw_input()
     for k, v in raw.iteritems():
         copy[k] = v[0] if isinstance(v, list) else v
     return copy
Exemple #5
0
 def cookies(self):
     '''
     Return all cookies as dict. The cookie name is str and values is unicode.
     >>> r = Request({'HTTP_COOKIE':'A=123; url=http%3A%2F%2Fwww.example.com%2F'})
     >>> r.cookies['A']
     u'123'
     >>> r.cookies['url']
     u'http://www.example.com/'
     '''
     return Dict(**self._get_cookies())
Exemple #6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'Peter Guan'

from db import Dict
import logging
logging.basicConfig(level=logging.INFO)

d1 = Dict()
d1["x"] = 3
print d1.x

d3 = Dict(('a', 'b', 'c'), (1, 2, 3))
print d3.b
logging.info("ok")
Exemple #7
0
 def cookies(self):
     return Dict(**self._get_cookies())
Exemple #8
0
 def input(self, **kwargs):
     copy = Dict(**kwargs)  # 这个Dict是已经实现d.a调用的dict
     raw = self._get_raw_input()
     for k, v in raw.iteritems():
         copy[k] = v[0] if isinstance(v, list) else v
     return copy
Exemple #9
0
 def cookies(self):
     """
     Return all cookies as dict. The cookie name is str and values is unicode.
     """
     return Dict(**self._get_cookies())
Exemple #10
0
 def input(self, **kw):
     copy = Dict(**kw)
     raw = self._get_raw_input()
     for k, v in raw.iteritems():
         copy[k] = v[0] if isinstance(v, list) else v
     return copy
Exemple #11
0
 def cookies(self):
     # setter 以Dict对象返回cookies
     return Dict(**self._get_cookies())