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

Changeset 1147

Show
Ignore:
Timestamp:
06/19/06 00:07:40
Author:
fumanchu
Message:

New hooks namespace in config, for attaching arbitrary hooks to requests. See test_tools for an example.

Files:

Legend:

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

    r1146 r1147  
    337337                bucket = self.toolmap.setdefault(toolname, {}) 
    338338                bucket[".".join(atoms)] = v 
     339            elif namespace == "hooks": 
     340                # Attach bare hooks declared in config. 
     341                hookpoint = atoms[0] 
     342                if isinstance(v, basestring): 
     343                    v = cherrypy.lib.attributes(v) 
     344                self.hooks.attach(hookpoint, v) 
    339345         
    340346        # Run tool._setup(conf) for each tool in the new toolmap. 
  • trunk/cherrypy/test/test_tools.py

    r1145 r1147  
    6464    tools.nadsat = NadsatTool() 
    6565     
     66    def pipe_body(): 
     67        cherrypy.request.process_request_body = False 
     68        clen = int(cherrypy.request.headers['Content-Length']) 
     69        cherrypy.request.body = cherrypy.request.rfile.read(clen) 
     70     
    6671    class Root: 
    6772        def index(self): 
     
    7479            yield europoundUnicode 
    7580        euro.exposed = True 
     81         
     82        # Bare hooks 
     83        def pipe(self): 
     84            return cherrypy.request.body 
     85        pipe.exposed = True 
     86        pipe._cp_config = {'hooks.before_request_body': pipe_body} 
    7687     
    7788    root = Root() 
     
    93104     
    94105    # METHOD ONE: 
    95     # Use _cp_config 
     106    # Declare Tools in _cp_config 
    96107    class Demo(Test): 
    97108         
     
    129140    conf = { 
    130141        # METHOD THREE: 
    131         # Do it all in detached config 
     142        # Declare Tools in detached config 
    132143        '/demo': { 
    133144            'tools.numerify.on': True, 
     
    211222        self.getPage("/euro", headers=[("Accept-Encoding", "gzip")]) 
    212223        self.assertInBody(zbuf.getvalue()[:3]) 
     224     
     225    def testBareHooks(self): 
     226        content = "bit of a pain in me gulliver" 
     227        self.getPage("/pipe", 
     228                     headers=[("Content-Length", len(content)), 
     229                              ("Content-Type", "text/plain")], 
     230                     method="POST", body=content) 
     231        self.assertBody(content) 
    213232 
    214233 

Hosted by WebFaction

Log in as guest/cpguest to create tickets