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

Changeset 1196

Show
Ignore:
Timestamp:
07/10/06 23:13:50
Author:
fumanchu
Message:

Some tool optimizations.

Files:

Legend:

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

    r1190 r1196  
    5252     
    5353    def _merged_args(self, d=None): 
    54         conf = cherrypy.request.toolmap.get(self._name, {}).copy() 
    55         conf.update(d or {}) 
     54        tm = cherrypy.request.toolmap 
     55        if self._name in tm: 
     56            conf = tm[self._name].copy() 
     57        else: 
     58            conf = {} 
     59        if d: 
     60            conf.update(d) 
    5661        if "on" in conf: 
    5762            del conf["on"] 
     
    113118        return wrapper 
    114119     
     120    def _wrapper(self): 
     121        if self.callable(**self._merged_args()): 
     122            cherrypy.request.handler = None 
     123     
    115124    def _setup(self): 
    116125        """Hook this tool into cherrypy.request. 
     
    119128        method when the tool is "turned on" in config. 
    120129        """ 
    121         def wrapper(): 
    122             if self.callable(**self._merged_args()): 
    123                 cherrypy.request.handler = None 
    124130        # Don't pass conf (or our wrapper will get wrapped!) 
    125         cherrypy.request.hooks.attach(self._point, wrapper) 
     131        cherrypy.request.hooks.attach(self._point, self._wrapper) 
    126132 
    127133 
     
    132138        Tool.__init__(self, None, callable, name) 
    133139     
     140    def _wrapper(self): 
     141        self.callable(**self._merged_args()) 
     142     
    134143    def _setup(self): 
    135144        """Hook this tool into cherrypy.request. 
     
    138147        method when the tool is "turned on" in config. 
    139148        """ 
    140         def wrapper(): 
    141             self.callable(**self._merged_args()) 
    142         cherrypy.request.error_response = wrapper 
     149        cherrypy.request.error_response = self._wrapper 
    143150 
    144151 
     
    148155from cherrypy.lib import sessions as _sessions, xmlrpc as _xmlrpc 
    149156from cherrypy.lib import caching as _caching, wsgiapp as _wsgiapp 
    150  
    151  
    152 class StaticDirTool(MainTool): 
    153     def _setup(self): 
    154         """Hook this tool into cherrypy.request using the given conf.""" 
    155         conf = self._merged_args() 
    156         def wrapper(): 
    157             if self.callable(**conf): 
    158                 cherrypy.request.handler = None 
    159         # Don't pass conf (or our wrapper will get wrapped!) 
    160         cherrypy.request.hooks.attach(self._point, wrapper) 
    161157 
    162158 
     
    172168                setattr(self, k, None) 
    173169     
     170    def _init(self): 
     171        conf = cherrypy.request.toolmap.get(self._name, {}) 
     172         
     173        s = cherrypy.request._session = _sessions.Session() 
     174        # Copy all conf entries onto Session object attributes 
     175        for k, v in conf.iteritems(): 
     176            setattr(s, str(k), v) 
     177        s.init() 
     178         
     179        if not hasattr(cherrypy, "session"): 
     180            cherrypy.session = _sessions.SessionWrapper() 
     181     
    174182    def _setup(self): 
    175183        """Hook this tool into cherrypy.request using the given conf. 
     
    178186        method when the tool is "turned on" in config. 
    179187        """ 
    180         def init(): 
    181             conf = cherrypy.request.toolmap.get(self._name, {}) 
    182              
    183             s = cherrypy.request._session = _sessions.Session() 
    184             # Copy all conf entries onto Session object attributes 
    185             for k, v in conf.iteritems(): 
    186                 setattr(s, str(k), v) 
    187             s.init() 
    188              
    189             if not hasattr(cherrypy, "session"): 
    190                 cherrypy.session = _sessions.SessionWrapper() 
    191188        # init must be bound after headers are read 
    192         cherrypy.request.hooks.attach('before_request_body', init) 
     189        cherrypy.request.hooks.attach('before_request_body', self._init) 
    193190        cherrypy.request.hooks.attach('before_finalize', _sessions.save) 
    194191        cherrypy.request.hooks.attach('on_end_request', _sessions.cleanup) 
     
    236233        """Hook this tool into cherrypy.request using the given conf.""" 
    237234        request = cherrypy.request 
     235        # Guard against running this method twice. 
    238236        if hasattr(request, 'xmlrpc'): 
    239237            return 
     
    303301default_toolbox.encode = Tool('before_finalize', encoding.encode) 
    304302default_toolbox.gzip = Tool('before_finalize', encoding.gzip) 
    305 default_toolbox.staticdir = StaticDirTool(static.staticdir) 
     303default_toolbox.staticdir = MainTool(static.staticdir) 
    306304default_toolbox.staticfile = MainTool(static.staticfile) 
    307305default_toolbox.sessions = SessionTool() 

Hosted by WebFaction

Log in as guest/cpguest to create tickets