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

Changeset 1081

Show
Ignore:
Timestamp:
04/26/06 18:56:41
Author:
fumanchu
Message:

Mixins would be hard to, uh, mix. So we'll use _cp_tools for class-level tool declaration instead. My hope is that the two remaining special attributes (logging and on_error) can become tools, as well, and we can have a nice uniform API.

Files:

Legend:

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

    r1077 r1081  
    3939            self.counter = 0 
    4040            self.ended = {} 
    41             class ToolMixin(object): 
    42                 def _cp_setup(me): 
    43                     self.setup(None) 
    44             self.Mixin = ToolMixin 
    4541         
    4642        def nadsat(self): 
     
    5753            self.ended[cherrypy.request.counter] = True 
    5854         
    59         def setup(self, conf): 
     55        def setup(self, conf=None): 
    6056            cherrypy.request.counter = self.counter = self.counter + 1 
    6157            self.ended[cherrypy.request.counter] = False 
     
    8682     
    8783    # METHOD ONE: 
    88     # Use tool.Mixin 
    89     class CPFilterList(Test, tools.nadsat.Mixin): 
     84    # Use _cp_tools 
     85    class Demo(Test): 
     86         
     87        _cp_tools = [tools.nadsat] 
    9088         
    9189        def index(self): 
     
    119117        # METHOD THREE: 
    120118        # Do it all in config 
    121         '/cpfilterlist': { 
     119        '/demo': { 
    122120            'tools.numerify.on': True, 
    123121            'tools.numerify.map': {"pie": "3.14159"}, 
    124122        }, 
    125         '/cpfilterlist/restricted': { 
     123        '/demo/restricted': { 
    126124            'server.show_tracebacks': False, 
    127125        }, 
    128         '/cpfilterlist/errinstream': { 
     126        '/demo/errinstream': { 
    129127            'stream_response': True, 
    130128        }, 
    131         '/cpfilterlist/err_in_onstart': { 
     129        '/demo/err_in_onstart': { 
    132130            # Because this isn't a dict, on_start_resource will error. 
    133131            'tools.numerify.map': "pie->3.14159" 
     
    143141class FilterTests(helper.CPWebCase): 
    144142     
    145     def testCPFilterList(self): 
    146 ##        self.getPage("/cpfilterlist/") 
    147 ##        # If body is "razdrez", then on_end_request is being called too early. 
    148 ##        self.assertBody("A horrorshow lomtick of cherry 3.14159") 
    149 ##        # If this fails, then on_end_request isn't being called at all. 
    150 ##        self.getPage("/cpfilterlist/ended/1") 
    151 ##        self.assertBody("True") 
    152 ##         
    153 ##        valerr = '\n    raise ValueError()\nValueError' 
    154 ##        self.getPage("/cpfilterlist/err") 
    155 ##        # If body is "razdrez", then on_end_request is being called too early. 
    156 ##        self.assertErrorPage(500, pattern=valerr) 
    157 ##        # If this fails, then on_end_request isn't being called at all. 
    158 ##        self.getPage("/cpfilterlist/ended/3") 
    159 ##        self.assertBody("True") 
    160 ##         
    161 ##        # If body is "razdrez", then on_end_request is being called too early. 
    162 ##        self.getPage("/cpfilterlist/errinstream") 
    163 ##        # Because this error is raised after the response body has 
    164 ##        # started, the status should not change to an error status. 
    165 ##        self.assertStatus("200 OK") 
    166 ##        self.assertBody("Unrecoverable error in the server.") 
    167 ##        # If this fails, then on_end_request isn't being called at all. 
    168 ##        self.getPage("/cpfilterlist/ended/5") 
    169 ##        self.assertBody("True") 
    170 ##         
     143    def testDemo(self): 
     144        self.getPage("/demo/") 
     145        # If body is "razdrez", then on_end_request is being called too early. 
     146        self.assertBody("A horrorshow lomtick of cherry 3.14159") 
     147        # If this fails, then on_end_request isn't being called at all. 
     148        self.getPage("/demo/ended/1") 
     149        self.assertBody("True") 
     150         
     151        valerr = '\n    raise ValueError()\nValueError' 
     152        self.getPage("/demo/err") 
     153        # If body is "razdrez", then on_end_request is being called too early. 
     154        self.assertErrorPage(500, pattern=valerr) 
     155        # If this fails, then on_end_request isn't being called at all. 
     156        self.getPage("/demo/ended/3") 
     157        self.assertBody("True") 
     158         
     159        # If body is "razdrez", then on_end_request is being called too early. 
     160        self.getPage("/demo/errinstream") 
     161        # Because this error is raised after the response body has 
     162        # started, the status should not change to an error status. 
     163        self.assertStatus("200 OK") 
     164        self.assertBody("Unrecoverable error in the server.") 
     165        # If this fails, then on_end_request isn't being called at all. 
     166        self.getPage("/demo/ended/5") 
     167        self.assertBody("True") 
     168         
    171169        # Test the decorator technique. 
    172         self.getPage("/cpfilterlist/restricted") 
     170        self.getPage("/demo/restricted") 
    173171        self.assertErrorPage(401) 
    174172     
     
    180178        # but because that failure is logged and passed over, the error 
    181179        # page we obtain in the user agent should be from before_finalize. 
    182         self.getPage("/cpfilterlist/err_in_onstart") 
     180        self.getPage("/demo/err_in_onstart") 
    183181        self.assertErrorPage(500) 
    184182        self.assertInBody("AttributeError: 'Request' object has no " 
  • trunk/cherrypy/tools.py

    r1078 r1081  
    3737     
    3838    def attach(self, point, callback, conf=None): 
    39         if conf is None: 
     39        if not conf: 
     40            # No point adding a wrapper if there's no conf 
    4041            self.callbacks[point].append(callback) 
    4142        else: 
     
    5253                g[toolname].setup(conf) 
    5354         
    54         # Run _cp_setup functions 
     55        # Run _cp_tools setup functions 
    5556        mounted_app_roots = cherrypy.tree.mount_points.values() 
    5657        objectList = _cputil.get_object_trail() 
    5758        objectList.reverse() 
    5859        for objname, obj in objectList: 
    59             s = getattr(obj, "_cp_setup", None) 
    60             if s: 
    61                 s() 
     60            for tool in getattr(obj, "_cp_tools", []): 
     61                tool.setup() 
    6262            if obj in mounted_app_roots: 
    6363                break 
     
    114114        # TODO: add an attribute to self for each arg 
    115115        # in inspect.getargspec(callable) 
    116          
    117         class ToolMixin(object): 
    118             def _cp_setup(me): 
    119                 self.setup(None) 
    120         self.Mixin = ToolMixin 
    121116     
    122117    def __call__(self, *args, **kwargs): 
     
    140135        return deco 
    141136     
    142     def setup(self, conf): 
     137    def setup(self, conf=None): 
    143138        """Hook this tool into cherrypy.request using the given conf. 
    144139         
     
    157152     
    158153    def __init__(self, callable, name=None): 
    159         self.point = 'before_main' 
    160         self.callable = callable 
    161         if name is None: 
    162             name = callable.__name__ 
    163         self.name = name 
     154        Tool.__init__(self, 'before_main', callable, name) 
    164155     
    165156    def handler(self, *args, **kwargs): 
     
    198189        return deco 
    199190     
    200     def setup(self, conf): 
     191    def setup(self, conf=None): 
    201192        """Hook this tool into cherrypy.request using the given conf. 
    202193         
     
    204195        method when the tool is "turned on" in config. 
    205196        """ 
     197        conf = conf or {} 
    206198        def wrapper(): 
    207199            if self.callable(**conf): 
     
    228220from cherrypy.lib import static 
    229221class _StaticDirTool(MainTool): 
    230     def setup(self, conf): 
     222    def setup(self, conf=None): 
    231223        """Hook this tool into cherrypy.request using the given conf.""" 
    232224        # Stick the section where "dir" was defined into the params 
     225        conf = conf or {} 
    233226        conf['section'] = cherrypy.config.get('tools.staticdir.dir', 
    234227                                              return_section=True) 
    235         def wrapper(): 
    236             if self.callable(**conf): 
    237                 cherrypy.request.dispatch = None 
    238         # Don't pass conf (or our wrapper will get wrapped!) 
    239         cherrypy.request.hooks.attach(self.point, wrapper) 
     228        MainTool.setup(self, conf) 
    240229staticdir = _StaticDirTool(static.staticdir) 
    241230staticfile = MainTool(static.staticfile) 
     
    267256        return deco 
    268257     
    269     def setup(self, conf): 
     258    def setup(self, conf=None): 
    270259        """Hook this tool into cherrypy.request using the given conf. 
    271260         
     
    273262        method when the tool is "turned on" in config. 
    274263        """ 
     264        conf = conf or {} 
    275265        def init(): 
    276266            s = cherrypy.request._session = _sessions.Session() 
     
    281271            if not hasattr(cherrypy, "session"): 
    282272                cherrypy.session = _sessions.SessionWrapper() 
     273        # init must be bound after headers are read 
    283274        cherrypy.request.hooks.attach('before_request_body', init) 
    284          
    285275        cherrypy.request.hooks.attach('before_finalize', _sessions.save) 
    286276        cherrypy.request.hooks.attach('on_end_request', _sessions.cleanup) 
     
    308298        path = _xmlrpc.patched_path(path, rpcmethod) 
    309299         
    310         from cherrypy import _cprequest 
    311         handler, opath, vpath = _cprequest.find_handler(path) 
     300        handler, opath, vpath = _cputil.find_handler(path) 
    312301         
    313302        # Decode any leftover %2F in the virtual_path atoms. 
     
    320309                        conf.get('allow_none', 0)) 
    321310     
    322     def setup(self, conf): 
     311    def setup(self, conf=None): 
    323312        """Hook this tool into cherrypy.request using the given conf.""" 
    324313        cherrypy.request.dispatch = self.dispatch 

Hosted by WebFaction

Log in as guest/cpguest to create tickets