Changeset 1115
- Timestamp:
- 06/01/06 13:02:52
- Files:
-
- trunk/cherrypy/__init__.py (modified) (1 diff)
- trunk/cherrypy/test/test_tools.py (modified) (4 diffs)
- trunk/cherrypy/tools.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1102 r1115 98 98 """Decorator to set _cp_config using the given kwargs.""" 99 99 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) 101 103 return f 102 104 return wrapper trunk/cherrypy/test/test_tools.py
r1114 r1115 74 74 yield europoundUnicode 75 75 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 76 82 77 83 root = Root() … … 111 117 yield "confidential" 112 118 113 # METHOD TWO: decorator using tool.wrap119 # METHOD TWO: decorator using Tool.wrap 114 120 def restricted(self): 115 121 return "Welcome!" … … 127 133 conf = { 128 134 # METHOD THREE: 129 # Do it all in config135 # Do it all in detached config 130 136 '/demo': { 131 137 'tools.numerify.on': True, … … 184 190 self.assertBody("True") 185 191 186 # Test the decorator technique.192 # Test the "wrap" technique (call-time decorator). 187 193 self.getPage("/demo/restricted") 188 194 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") 189 200 190 201 def testGuaranteedHooks(self): trunk/cherrypy/tools.py
r1104 r1115 97 97 98 98 def wrap(self, *args, **kwargs): 99 """ Make a decorator for this tool.99 """Call-time decorator (wrap the handler with pre and post logic). 100 100 101 101 For example: … … 112 112 return wrapper 113 113 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 114 133 115 134 def setup(self): … … 332 351 from cherrypy.lib import wsgiapp as _wsgiapp 333 352 class _WSGIAppTool(MainTool): 334 """A filterfor running any WSGI middleware/application within CP.335 353 """A tool for running any WSGI middleware/application within CP. 354 336 355 Here are the parameters: 337 356 338 357 wsgi_app - any wsgi application callable 339 358 env_update - a dictionary with arbitrary keys and values to be 340 359 merged with the WSGI environment dictionary. 341 360 342 361 Example: 343 362

