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

Changeset 1060

Show
Ignore:
Timestamp:
04/22/06 17:46:48
Author:
fumanchu
Message:

More tool improvements:

  1. New MainTool?.handler method.
  2. Removed superfluous "exposed" logic on decorators.
  3. Tool.call now returns the results of callable()
  4. MainTool?.wrap wasn't returning response.body.
Files:

Legend:

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

    r1058 r1060  
    88        docstring. 
    99     
    10     Function decorators: if the tool exposes a "wrap" callable, that 
    11         is assumed to be a decorator for use in wrapping individual 
    12         CherryPy page handlers (methods on the CherryPy tree). 
     10    Function decorators: if the tool exposes a "wrap" callable, that is 
     11        assumed to be a decorator for use in wrapping individual CherryPy 
     12        page handlers (methods on the CherryPy tree). The tool may choose 
     13        not to call the page handler at all, if the response has already 
     14        been populated. 
    1315     
    1416    CherryPy hooks: "hooks" are points in the CherryPy request-handling 
     
    1719        this. If a tool exposes a "setup" callable, this will be called 
    1820        once per Request (if the feature is enabled via config). 
    19      
    20     WSGI middleware: 
    21          
    2221 
    2322Tools may be implemented as any object with a namespace. The builtins 
     
    3736     
    3837    def __call__(self, *args, **kwargs): 
    39         self.callable(*args, **kwargs) 
     38        return self.callable(*args, **kwargs) 
    4039     
    4140    def wrap(self, *args, **kwargs): 
    42         """Make a decorator for this tool.""" 
     41        """Make a decorator for this tool. 
     42         
     43        For example: 
     44         
     45            @tools.decode.wrap(encoding='chinese') 
     46            def mandarin(self, name): 
     47                return "%s, ni hao shi jie" % name 
     48            mandarin.exposed = True 
     49        """ 
    4350        def deco(f): 
    4451            def wrapper(*a, **kw): 
    45                 print args, kwargs 
    4652                handled = self.callable(*args, **kwargs) 
    4753                return f(*a, **kw) 
    48             missing = object() 
    49             exposed = getattr(f, "exposed", missing) 
    50             if exposed is not missing: 
    51                 wrapper.exposed = exposed 
    5254            return wrapper 
    5355        return deco 
    5456     
    5557    def setup(self, conf): 
    56         """Hook this tool into cherrypy.request using the given conf.""" 
     58        """Hook this tool into cherrypy.request using the given conf. 
     59         
     60        The standard CherryPy request object will automatically call this 
     61        method when the tool is "turned on" in config. 
     62        """ 
    5763        cherrypy.request.hooks.attach(self.point, self.callable, conf) 
    5864 
     
    6975        self.callable = callable 
    7076     
    71     def __call__(self, *args, **kwargs): 
    72         self.callable(*args, **kwargs) 
     77    def handler(self, *args, **kwargs): 
     78        """Use this tool as a CherryPy page handler. 
     79         
     80        For example: 
     81            cherrypy.root.nav = tools.staticdir.handler( 
     82                                    section="/nav", dir="nav", root=absDir) 
     83        """ 
     84        def wrapper(*a, **kw): 
     85            handled = self.callable(*args, **kwargs) 
     86            if not handled: 
     87                raise cherrypy.NotFound() 
     88            return cherrypy.response.body 
     89        wrapper.exposed = True 
     90        return wrapper 
    7391     
    7492    def wrap(self, *args, **kwargs): 
    75         """Make a decorator for this tool.""" 
     93        """Make a decorator for this tool. 
     94         
     95        For example: 
     96         
     97            @tools.staticdir.wrap(section="/slides", dir="styles", root=absDir) 
     98            def slides(self, slide=None, style=None): 
     99                return "No such file" 
     100            slides.exposed = True 
     101        """ 
    76102        def deco(f): 
    77103            def wrapper(*a, **kw): 
    78                 print args, kwargs 
    79104                handled = self.callable(*args, **kwargs) 
    80                 if not handled: 
     105                if handled: 
     106                    return cherrypy.response.body 
     107                else: 
    81108                    return f(*a, **kw) 
    82             missing = object() 
    83             exposed = getattr(f, "exposed", missing) 
    84             if exposed is not missing: 
    85                 wrapper.exposed = exposed 
    86109            return wrapper 
    87110        return deco 
    88111     
    89112    def setup(self, conf): 
    90         """Hook this tool into cherrypy.request using the given conf.""" 
     113        """Hook this tool into cherrypy.request using the given conf. 
     114         
     115        The standard CherryPy request object will automatically call this 
     116        method when the tool is "turned on" in config. 
     117        """ 
    91118        def wrapper(): 
    92119            if self.callable(**conf): 
     
    98125#                              Builtin tools                              # 
    99126 
    100 from cherrypy.lib import cptools, encodings, static 
    101  
     127from cherrypy.lib import cptools 
    102128base_url = Tool('before_request_body', cptools.base_url) 
    103129response_headers = Tool('before_finalize', cptools.response_headers) 
    104130virtual_host = Tool('before_request_body', cptools.virtual_host) 
     131del cptools 
    105132 
     133from cherrypy.lib import encodings 
    106134decode = Tool('before_main', encodings.decode) 
    107135encode = Tool('before_finalize', encodings.encode) 
    108136gzip = Tool('before_finalize', encodings.gzip) 
     137del encodings 
    109138 
     139from cherrypy.lib import static 
    110140class _StaticDirTool(MainTool): 
    111141    def setup(self, conf): 
    112142        """Hook this tool into cherrypy.request using the given conf.""" 
    113         section = cherrypy.config.get('tools.staticdir.dir', return_section=True) 
    114         conf['section'] = section 
     143        conf['section'] = cherrypy.config.get('tools.staticdir.dir', 
     144                                              return_section=True) 
    115145        def wrapper(): 
    116146            if self.callable(**conf): 
     
    118148        # Don't pass conf (or our wrapper will get wrapped!) 
    119149        cherrypy.request.hooks.attach(self.point, wrapper) 
    120  
    121150staticdir = _StaticDirTool(static.get_dir) 
    122151staticfile = MainTool(static.get_file) 
     152del static 
    123153 
    124154# These modules are themselves Tools 

Hosted by WebFaction

Log in as guest/cpguest to create tickets