Ejemplo n.º 1
0
def controllerHandler(connection, sessionDict):
    if not Cfg.MvcOn:
        return
    cname=sessionDict.get('CONTROLLER')
    data=sessionDict.get('CONTROLLER_ARGS')
    aname=sessionDict.get('ACTION')
    debug("do we have a controller? %s",  ('No', 'Yes')[bool(cname)])
    if cname:
        c=Cfg.controllers.get(cname)
        if c:
            if isinstance(c, basestring):
                # resolve it
                c=_resolve_string(c)
                if not c:
                    return
            if not aname:
                aname='index'
            meth=getattr(c, aname, None)
            if meth and not is_exposed(meth):
                info("request for method that isn't exposed, not honoring")
                meth=None
            if not meth:
                # look for a default method and pass
                # the action name in as a keyword argument
                data['action']=aname
                if callable(c):
                    meth=c
                else:
                    # "default" doesn't need to be exposed
                    meth=getattr(c, "default", None)
		    
            if meth:
                # some defaults
                connection.setStatus(200)
                connection.setContentType('text/html')
                try:
                    res=meth(**data)
                except PreemptiveResponse:
                    # not a problem, raise it
                    raise
                except Response:
                    res(connection)
                    return connection.response()
                except:
                    # OK, something bad.  This should be a server error.
                    return _handleException(connection)
                else:
                    if res:
                        if isinstance(res, basestring):
                            connection.write(res)
                        elif callable(res):
                            res(connection)
                        else:
                            # an iterable, hopefully
                            for thing in res:
                                connection.write(thing)
                        return connection.response()
Ejemplo n.º 2
0
def controllerHandler(connection, sessionDict):
    if not Cfg.MvcOn:
        return
    cname = sessionDict.get('CONTROLLER')
    data = sessionDict.get('CONTROLLER_ARGS')
    aname = sessionDict.get('ACTION')
    debug("do we have a controller? %s", ('No', 'Yes')[bool(cname)])
    if cname:
        c = Cfg.controllers.get(cname)
        if c:
            if isinstance(c, basestring):
                # resolve it
                c = _resolve_string(c)
                if not c:
                    return
            if not aname:
                aname = 'index'
            meth = getattr(c, aname, None)
            if meth and not is_exposed(meth):
                info("request for method that isn't exposed, not honoring")
                meth = None
            if not meth:
                # look for a default method and pass
                # the action name in as a keyword argument
                data['action'] = aname
                if callable(c):
                    meth = c
                else:
                    # "default" doesn't need to be exposed
                    meth = getattr(c, "default", None)

            if meth:
                # some defaults
                connection.setStatus(200)
                connection.setContentType('text/html')
                try:
                    res = meth(**data)
                except PreemptiveResponse:
                    # not a problem, raise it
                    raise
                except Response:
                    res(connection)
                    return connection.response()
                except:
                    # OK, something bad.  This should be a server error.
                    return _handleException(connection)
                else:
                    if res:
                        if isinstance(res, basestring):
                            connection.write(res)
                        elif callable(res):
                            res(connection)
                        else:
                            # an iterable, hopefully
                            for thing in res:
                                connection.write(thing)
                        return connection.response()
Ejemplo n.º 3
0
def routing_hook(connection, sessionDict):
    if not Cfg.MvcOn:
        return
    debug("in routing hook")
    # initialize routes request config
    rcfg=request_config()
    rcfg.redirect=_do_redirect
    rcfg.mapper=map=Mapper()
    rcfg.host=connection.host
    if connection.env.get('HTTPS', False):
        rcfg.protocol='HTTPS'
    else:
        rcfg.protocol='HTTP'
    env=connection.env.copy()
    env['wsgi.version']=(1,0)
    env['wsgi.multithread']=False
    env['wsgi.multiprocess']=True
    env['wsgi.run_once']=False
    env['wsgi.url_scheme']='https' if env.get('HTTPS', 'off') in ('on', '1') else 'http'
    env['SCRIPT_NAME']=''
    rcfg.environ=env
        
    # initialize the map
    for r in Cfg.routes:
        if isinstance(r, dict):
            map.connect(**r)
        elif isinstance(r, (list, tuple)):
            if (len(r)==2 and
                isinstance(r[0], (list, tuple)) and
                isinstance(r[1], dict)):
                map.connect(*r[0], **r[1])
            else:
                map.connect(*r)
        else:
            raise ValueError, "wrong arguments for connect()"

    map.create_regs(Cfg.controllers.keys())

    # test the url with the map
    res=rcfg.mapper_dict=map.match(connection.uri) or {}
    debug("performed routing match")
    # put the result if the session dict
    if res:
        debug("have routing match!")
        res=res.copy()
        debug("result dict: %s", res)
        controller=res.pop('controller', None)
        action=res.pop('action', None)
        sessionDict['CONTROLLER']=controller
        sessionDict['ACTION']=action
        sessionDict['CONTROLLER_ARGS']=res
Ejemplo n.º 4
0
def routing_hook(connection, sessionDict):
    if not Cfg.MvcOn:
        return
    debug("in routing hook")
    # initialize routes request config
    rcfg = request_config()
    rcfg.redirect = _do_redirect
    rcfg.mapper = map = Mapper()
    rcfg.host = connection.host
    if connection.env.get('HTTPS', False):
        rcfg.protocol = 'HTTPS'
    else:
        rcfg.protocol = 'HTTP'
    env = connection.env.copy()
    env['wsgi.version'] = (1, 0)
    env['wsgi.multithread'] = False
    env['wsgi.multiprocess'] = True
    env['wsgi.run_once'] = False
    env['wsgi.url_scheme'] = 'https' if env.get(
        'HTTPS', 'off') in ('on', '1') else 'http'
    env['SCRIPT_NAME'] = ''
    rcfg.environ = env

    # initialize the map
    for r in Cfg.routes:
        if isinstance(r, dict):
            map.connect(**r)
        elif isinstance(r, (list, tuple)):
            if (len(r) == 2 and isinstance(r[0], (list, tuple))
                    and isinstance(r[1], dict)):
                map.connect(*r[0], **r[1])
            else:
                map.connect(*r)
        else:
            raise ValueError, "wrong arguments for connect()"

    map.create_regs(Cfg.controllers.keys())

    # test the url with the map
    res = rcfg.mapper_dict = map.match(connection.uri) or {}
    debug("performed routing match")
    # put the result if the session dict
    if res:
        debug("have routing match!")
        res = res.copy()
        debug("result dict: %s", res)
        controller = res.pop('controller', None)
        action = res.pop('action', None)
        sessionDict['CONTROLLER'] = controller
        sessionDict['ACTION'] = action
        sessionDict['CONTROLLER_ARGS'] = res