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

Changeset 1115

Show
Ignore:
Timestamp:
06/01/06 13:02:52
Author:
fumanchu
Message:

New Tool.enable method (compile-time decorator to set the wrapped function's _cp_config).

Files:

Legend:

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

    r1102 r1115  
    9898    """Decorator to set _cp_config using the given kwargs.""" 
    9999    def wrapper(f): 
    100         f._cp_config = kwargs 
     100        if not hasattr(f, "_cp_config"): 
     101            f._cp_config = {} 
     102        f._cp_config.update(kwargs) 
    101103        return f 
    102104    return wrapper 
  • trunk/cherrypy/test/test_tools.py

    r1114 r1115  
    7474            yield europoundUnicode 
    7575        euro.exposed = True 
     76         
     77        # METHOD FOUR: decorator using Tool.enable 
     78        @tools.base_url.enable() 
     79        def base(self): 
     80            return cherrypy.request.base 
     81        base.exposed = True 
    7682     
    7783    root = Root() 
     
    111117            yield "confidential" 
    112118         
    113         # METHOD TWO: decorator using tool.wrap 
     119        # METHOD TWO: decorator using Tool.wrap 
    114120        def restricted(self): 
    115121            return "Welcome!" 
     
    127133    conf = { 
    128134        # METHOD THREE: 
    129         # Do it all in config 
     135        # Do it all in detached config 
    130136        '/demo': { 
    131137            'tools.numerify.on': True, 
     
    184190        self.assertBody("True") 
    185191         
    186         # Test the decorator technique
     192        # Test the "wrap" technique (call-time decorator)
    187193        self.getPage("/demo/restricted") 
    188194        self.assertErrorPage(401) 
     195         
     196        # Test the "enable" technique (compile-time decorator). 
     197        self.getPage("/base", headers=[('X-Forwarded-Host', 
     198                                        'www.myforward.com')]) 
     199        self.assertBody("http://www.myforward.com") 
    189200     
    190201    def testGuaranteedHooks(self): 
  • trunk/cherrypy/tools.py

    r1104 r1115  
    9797     
    9898    def wrap(self, *args, **kwargs): 
    99         """Make a decorator for this tool
     99        """Call-time decorator (wrap the handler with pre and post logic)
    100100         
    101101        For example: 
     
    112112            return wrapper 
    113113        return deco 
     114     
     115    def enable(self, **kwargs): 
     116        """Compile-time decorator (turn on the tool in config). 
     117         
     118        For example: 
     119         
     120            @tools.base_url.enable() 
     121            def whats_my_base(self): 
     122                return cherrypy.request.base 
     123            whats_my_base.exposed = True 
     124        """ 
     125        def wrapper(f): 
     126            if not hasattr(f, "_cp_config"): 
     127                f._cp_config = {} 
     128            f._cp_config["tools." + self.name + ".on"] = True 
     129            for k, v in kwargs: 
     130                f._cp_config["tools." + self.name + "." + k] = v 
     131            return f 
     132        return wrapper 
    114133     
    115134    def setup(self): 
     
    332351from cherrypy.lib import wsgiapp as _wsgiapp 
    333352class _WSGIAppTool(MainTool): 
    334     """A filter for running any WSGI middleware/application within CP. 
    335  
     353    """A tool for running any WSGI middleware/application within CP. 
     354     
    336355    Here are the parameters: 
    337  
     356     
    338357    wsgi_app - any wsgi application callable 
    339358    env_update - a dictionary with arbitrary keys and values to be 
    340359                 merged with the WSGI environment dictionary. 
    341  
     360     
    342361    Example: 
    343362     

Hosted by WebFaction

Log in as guest/cpguest to create tickets