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

Changeset 1292

Show
Ignore:
Timestamp:
08/28/06 16:28:08
Author:
fumanchu
Message:

Allow dynamic custom config namespaces.

Files:

Legend:

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

    r1288 r1292  
    123123        } 
    124124     
     125    namespaces = {"server": lambda k, v: setattr(cherrypy.server, k, v), 
     126                  "engine": lambda k, v: setattr(cherrypy.engine, k, v), 
     127                  "log": lambda k, v: setattr(cherrypy.log, k, v), 
     128                  } 
     129     
    125130    def __init__(self): 
    126131        self.reset() 
     
    164169        atoms = k.split(".", 1) 
    165170        namespace = atoms[0] 
    166         if namespace == "server": 
    167             setattr(cherrypy.server, atoms[1], v) 
    168         elif namespace == "engine": 
    169             setattr(cherrypy.engine, atoms[1], v) 
    170         elif namespace == "log": 
    171             setattr(cherrypy.log, atoms[1], v) 
     171        if namespace in self.namespaces: 
     172            self.namespaces[namespace](atoms[1], v) 
    172173 
    173174 
  • trunk/cherrypy/_cprequest.py

    r1288 r1292  
    331331        # Put a *copy* of the class error_page into self. 
    332332        self.error_page = self.error_page.copy() 
     333         
     334        self.namespaces = {"tools": self._set_tool, 
     335                           "hooks": self._set_hook, 
     336                           "request": self.__setattr__, 
     337                           "response": lambda k, v: setattr(cherrypy.response, k, v), 
     338                           "error_page": lambda k, v: self.error_page.__setitem__(int(k), v), 
     339                           } 
    333340     
    334341    def close(self): 
     
    539546        dispatch(path) 
    540547     
     548    def _set_tool(self, k, v): 
     549        """Attach tools specified in config.""" 
     550        toolname, arg = k.split(".", 1) 
     551        bucket = self.toolmap.setdefault(toolname, {}) 
     552        bucket[arg] = v 
     553     
     554    def _set_hook(self, k, v): 
     555        """Attach bare hooks declared in config.""" 
     556        # Use split again to allow multiple hooks for a single 
     557        # hookpoint per path (e.g. "hooks.before_main.1"). 
     558        # Little-known fact you only get from reading source ;) 
     559        hookpoint = k.split(".", 1)[0] 
     560        if isinstance(v, basestring): 
     561            v = cherrypy.lib.attributes(v) 
     562        if not isinstance(v, Hook): 
     563            v = Hook(v) 
     564        self.hooks[hookpoint].append(v) 
     565     
    541566    def tool_up(self): 
    542567        """Process self.config, populate self.toolmap and set up each tool.""" 
     
    547572            atoms = k.split(".", 1) 
    548573            namespace = atoms[0] 
    549             if namespace == "tools": 
    550                 toolname, arg = atoms[1].split(".", 1) 
    551                 bucket = tm.setdefault(toolname, {}) 
    552                 bucket[arg] = reqconf[k] 
    553             elif namespace == "hooks": 
    554                 # Attach bare hooks declared in config. 
    555                 # Use split again to allow multiple hooks for a single 
    556                 # hookpoint per path (e.g. "hooks.before_main.1"). 
    557                 # Little-known fact you only get from reading source ;) 
    558                 hookpoint = atoms[1].split(".", 1)[0] 
    559                 v = reqconf[k] 
    560                 if isinstance(v, basestring): 
    561                     v = cherrypy.lib.attributes(v) 
    562                 if not isinstance(v, Hook): 
    563                     v = Hook(v) 
    564                 self.hooks[hookpoint].append(v) 
    565             elif namespace == "request": 
    566                 # Override properties of this request object. 
    567                 setattr(self, atoms[1], reqconf[k]) 
    568             elif namespace == "response": 
    569                 # Override properties of the current response object. 
    570                 setattr(cherrypy.response, atoms[1], reqconf[k]) 
    571             elif namespace == "error_page": 
    572                 self.error_page[int(atoms[1])] = reqconf[k] 
     574            if namespace in self.namespaces: 
     575                self.namespaces[namespace](atoms[1], reqconf[k]) 
    573576         
    574577        # Run tool._setup(conf) for each tool in the new toolmap. 

Hosted by WebFaction

Log in as guest/cpguest to create tickets