def invoke_controller(self, controller, args, kwargs, state):
        '''
        The main request handler for Pecan applications.
        '''
        cfg = _cfg(controller)
        content_types = cfg.get('content_types', {})
        req = state.request
        resp = state.response
        pecan_state = req.pecan

        # If a keyword is supplied via HTTP GET or POST arguments, but the
        # function signature does not allow it, just drop it (rather than
        # generating a TypeError).
        argspec = getargspec(controller)
        keys = kwargs.keys()
        for key in keys:
            if key not in argspec.args and not argspec.keywords:
                kwargs.pop(key)

        # get the result from the controller
        result = controller(*args, **kwargs)

        # a controller can return the response object which means they've taken
        # care of filling it out
        if result is response:
            return
        elif isinstance(result, WebObResponse):
            state.response = result
            return

        raw_namespace = result

        # pull the template out based upon content type and handle overrides
        template = content_types.get(pecan_state['content_type'])

        # check if for controller override of template
        template = pecan_state.get('override_template', template)
        if template is None and cfg['explicit_content_type'] is False:
            if self.default_renderer == 'json':
                template = 'json'

        pecan_state['content_type'] = pecan_state.get(
            'override_content_type',
            pecan_state['content_type']
        )

        # if there is a template, render it
        if template:
            if template == 'json':
                pecan_state['content_type'] = 'application/json'
            result = self.render(template, result)

        # If we are in a test request put the namespace where it can be
        # accessed directly
        if req.environ.get('paste.testing'):
            testing_variables = req.environ['paste.testing_variables']
            testing_variables['namespace'] = raw_namespace
            testing_variables['template_name'] = template
            testing_variables['controller_output'] = result

        # set the body content
        if result and isinstance(result, six.text_type):
            resp.text = result
        elif result:
            resp.body = result

        if pecan_state['content_type']:
            # set the content type
            resp.content_type = pecan_state['content_type']
def invoke_controller ( self , controller , args , kwargs , state ) : cfg = _cfg ( controller ) content_types = cfg . get ( 'content_types' , { } ) req = state . request resp = state . response pecan_state = req . pecan argspec = getargspec ( controller ) keys = kwargs . keys ( ) for key in keys : if key not in argspec . args and not argspec . keywords : kwargs . pop ( key ) result = controller ( * args , ** kwargs ) if result is response : return elif isinstance ( result , WebObResponse ) : state . response = result return raw_namespace = result template = content_types . get ( pecan_state [ 'content_type' ] ) template = pecan_state . get ( 'override_template' , template ) if template is None and cfg [ 'explicit_content_type' ] is False : if self . default_renderer == 'json' : template = 'json' pecan_state [ 'content_type' ] = pecan_state . get ( 'override_content_type' , pecan_state [ 'content_type' ] ) if template : if template == 'json' : pecan_state [ 'content_type' ] = 'application/json' result = self . render ( template , result ) if req . environ . get ( 'paste.testing' ) : testing_variables = req . environ [ 'paste.testing_variables' ] testing_variables [ 'namespace' ] = raw_namespace testing_variables [ 'template_name' ] = template testing_variables [ 'controller_output' ] = result if result and isinstance ( result , six . text_type ) : resp . text = result elif result : resp . body = result if pecan_state [ 'content_type' ] : resp . content_type = pecan_state [ 'content_type' ]
The main request handler for Pecan applications.