Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1008

Show
Ignore:
Timestamp:
03/15/06 22:06:27
Author:
dowski
Message:

As per the discussion on cherrypy-devel,
the WSGIAppFilter is no longer configurable through the CP config
system. You should either add an instance of
cherrypy.filters.wsgiappfilter.WSGIAppFilter to your _cp_filters
list or use the cherrypy.lib.cptools.WSGIApp convenience class.

Also tweaked the test for wsgiappfilter to make sure that static
content higher than a WSGI application on the tree gets served
correctly.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/filters/__init__.py

    r1005 r1008  
    1919    "cherrypy.filters.tidyfilter.TidyFilter", 
    2020    "cherrypy.filters.xmlrpcfilter.XmlRpcFilter", 
    21     "cherrypy.filters.wsgiappfilter.WSGIAppFilter", 
    2221] 
    2322 
  • trunk/cherrypy/filters/wsgiappfilter.py

    r1007 r1008  
    1 """a WSGI application filter for CherryPy""" 
     1"""a WSGI application filter for CherryPy 
     2 
     3also see cherrypy.lib.cptools.WSGIApp""" 
    24 
    35# by Christian Wyglendowski 
     
    79import cherrypy 
    810from cherrypy.filters.basefilter import BaseFilter 
    9 from cherrypy.lib import cptools 
    1011from cherrypy._cputil import get_object_trail 
    11  
    12  
    13 class NoWSGIEnvironmentError(Exception): 
    14     pass 
    1512 
    1613 
     
    5451    """grabbed some of below from _cpwsgiserver.py 
    5552     
    56     for compatibility with the 2.2beta release of CP 
     53    for hosting WSGI apps in non-WSGI environments (yikes!) 
    5754    """ 
    5855 
     
    9289    """A filter for running any WSGI middleware/application within CP. 
    9390 
    94     It can be used through the config system or by adding it to 
    95     a classes _cp_filters special attribute.  Here are the parameters: 
     91    Here are the parameters: 
    9692 
    97     wsgi_app (wsgiapp_filter.app) - any wsgi application callable 
    98     env_update (wsgiapp_filter.env_update) - a dictionary with 
    99     arbitrary keys and values to be merged with the WSGI 
    100     environment dictionary. 
     93    wsgi_app - any wsgi application callable 
     94    env_update - a dictionary with arbitrary keys and values to be  
     95                 merged with the WSGI environment dictionary. 
    10196 
    102     Examples: 
    103  
    104     # using _cp_filters 
     97    Example: 
     98     
    10599    class Whatever: 
    106100        _cp_filters = [WSGIAppFilter(some_app)] 
    107  
    108     # using a config file 
    109     [/hosted/app] 
    110     wsgiapp_filter.on = True 
    111     wsgiapp_filter.app = 'package.module.wsgi_app_callable' 
    112     wsgiapp_filter.env_update = {'running_in_cp':True} 
    113101    """ 
    114102 
    115     def __init__(self, wsgi_app=None, env_update=None): 
     103    def __init__(self, wsgi_app, env_update=None): 
    116104        self.app = wsgi_app 
    117105        self.env_update = env_update or {} 
    118106    
    119107    def before_request_body(self): 
    120         request = cherrypy.request 
    121         enabled = cherrypy.config.get('wsgiapp_filter.on', False) 
    122         request.wsgi_app = self.app 
    123         request.wsgi_env_update = self.env_update 
    124         request.wsgiapp_filter_on = enabled or bool(self.app) 
    125  
    126         if not request.wsgiapp_filter_on: 
    127             return 
    128  
    129         if not request.wsgi_app: 
    130             app = cherrypy.config.get('wsgiapp_filter.app', None) 
    131             if app and isinstance(app, basestring): 
    132                 app = cptools.attributes(app) 
    133             request.wsgi_app = app 
    134108         
    135         if not request.wsgi_env_update: 
    136             env_update = cherrypy.config.get('wsgiapp_filter.env_update', {}) 
    137             request.wsgi_env_update = env_update 
    138  
    139109        # keep the request body intact so the wsgi app 
    140110        # can have its way with it 
     
    146116         
    147117        request = cherrypy.request 
    148  
    149         if not request.wsgiapp_filter_on: 
    150             return 
    151          
    152118        # if the static filter is on for this path and 
    153119        # request.execute_main is False, assume that the 
     
    167133        # update the environ with the dict passed to the filter's 
    168134        # constructor 
    169         environ.update(request.wsgi_env_update) 
     135        environ.update(self.env_update) 
    170136 
    171137        # run the wsgi app and have it set response.body 
    172         cherrypy.response.body = request.wsgi_app(environ, start_response) 
     138        cherrypy.response.body = self.app(environ, start_response) 
    173139         
    174140        # tell CP not to handle the request further 
    175141        request.execute_main = False 
    176  
    177  
    178 class WSGIApp(object): 
    179     """a convenience class used to easily mount a wsgi app. 
    180  
    181     example: 
    182     cherrypy.root = SomeRoot() 
    183     cherrypy.root.otherapp = WSGIApp(other_wsgi_app) 
    184     """ 
    185     def __init__(self, app, env_update=None): 
    186         self._cpFilterList = [WSGIAppFilter(app, env_update)] 
    187142 
    188143 
     
    209164        index.exposed = True 
    210165 
     166    class HostedWSGI(object): 
     167        _cp_filters = [WSGIAppFilter(my_app, {'cherrypy.wsgi':True,}),] 
     168 
    211169    # mount standard CherryPy app 
    212     cherrypy.root = Root(
     170    cherrypy.tree.mount(Root(), '/'
    213171    # mount the WSGI app 
    214     cherrypy.root.app = WSGIApp(my_app, {'cherrypy.wsgi_filter':True}) 
    215  
     172    cherrypy.tree.mount(HostedWSGI(), '/app') 
    216173 
    217174    cherrypy.server.start() 
  • trunk/cherrypy/lib/cptools.py

    r974 r1008  
    1414import cherrypy 
    1515import httptools 
     16 
     17from cherrypy.filters.wsgiappfilter import WSGIAppFilter 
    1618 
    1719 
     
    234236 
    235237 
     238class WSGIApp(object): 
     239    """a convenience class that uses the WSGIAppFilter 
     240     
     241    to easily add a WSGI application to the CP object tree. 
     242 
     243    example: 
     244    cherrypy.tree.mount(SomeRoot(), '/') 
     245    cherrypy.tree.mount(WSGIApp(other_wsgi_app), '/ext_app') 
     246    """ 
     247    def __init__(self, app, env_update=None): 
     248        self._cpFilterList = [WSGIAppFilter(app, env_update)] 
     249 
     250 
    236251# public domain "unrepr" implementation, found on the web and then improved. 
    237252import compiler 
  • trunk/cherrypy/test/test_wsgiapp_filter.py

    r1005 r1008  
    22test.prefer_parent_path() 
    33 
     4import os 
     5curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
     6 
    47import cherrypy 
    5 from cherrypy.filters.wsgiappfilter import WSGIAppFilter, WSGIApp 
     8from cherrypy.filters.wsgiappfilter import WSGIAppFilter 
     9from cherrypy.lib.cptools import WSGIApp 
    610 
    711def test_app(environ, start_response): 
     
    3943               'server.socket_port': helper.CPWebCase.PORT, 
    4044               }, 
    41     '/xmlrpc': {'xmlrpc_filter.on':True}, 
    42     '/hosted/app2': {'wsgiapp_filter.on':True, 
    43                      'wsgiapp_filter.app': test_app, 
    44                      'wsgiapp_filter.env_update': {'cp.hosted':True}, 
    45                     }, 
     45    '/hosted/app0/static' : { 
     46                'static_filter.on':True, 
     47                'static_filter.root': curdir, 
     48                'static_filter.dir': 'static', 
     49               }, 
     50                         
    4651    }) 
    4752 
     
    6671        self.assertInBody(self.wsgi_output) 
    6772 
    68     def test_04_from_config_prog(self): 
    69         self.getPage("/hosted/app2") 
    70         self.assertHeader("Content-Type", "text/plain"
    71         self.assertInBody(self.wsgi_output
    72         self.assertInBody("cp.hosted"
    73          
     73    def test_04_static_subdir(self): 
     74        self.getPage("/hosted/app0/static/index.html") 
     75        self.assertStatus('200 OK'
     76        self.assertHeader('Content-Type', 'text/html'
     77        self.assertBody('Hello, world\r\n'
     78 
    7479if __name__ == '__main__': 
    7580    from cherrypy import _cpwsgi 

Hosted by WebFaction

Log in as guest/cpguest to create tickets