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

Changeset 1612

Show
Ignore:
Timestamp:
02/05/07 15:30:03
Author:
fumanchu
Message:

Promoted namespace dicts to their own class (so they can share docs).

Files:

Legend:

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

    r1605 r1612  
    155155 
    156156 
    157 def _call_namespaces(config, namespaces): 
    158     """Iterate through config and pass it to each namespace. 
    159      
    160     'config' should be a flat dict, where keys use dots to separate 
    161     namespaces, and values are arbitrary. 
    162     'namespaces' should be a dict whose keys are strings and whose 
    163     values are namespace handlers. 
    164      
    165     The first name in each config key is used to look up the corresponding 
    166     namespace handler. For example, a config entry of {'tools.gzip.on': v} 
    167     will call the 'tools' namespace handler with the args: ('gzip.on', v) 
    168      
    169     Each handler may be a bare callable, or it may be a context manager 
    170     with __enter__ and __exit__ methods, in which case the __enter__ 
    171     method should return the callable. 
     157class NamespaceSet(dict): 
     158    """A dict of config namespace names and handlers. 
     159     
     160    Each config entry should begin with a namespace name; the corresponding 
     161    namespace handler will be called once for each config entry in that 
     162    namespace, and will be passed two arguments: the config key (with the 
     163    namespace removed) and the config value. 
     164     
     165    Namespace handlers may be any Python callable; they may also be 
     166    Python 2.5-style 'context managers', in which case their __enter__ 
     167    method should return a callable to be used as the handler. 
     168    See cherrypy.tools (the Toolbox class) for an example. 
    172169    """ 
    173     # Separate the given config into namespaces 
    174     ns_confs = {} 
    175     for k in config: 
    176         if "." in k: 
    177             ns, name = k.split(".", 1) 
    178             bucket = ns_confs.setdefault(ns, {}) 
    179             bucket[name] = config[k] 
    180      
    181     # I chose __enter__ and __exit__ so someday this could be 
    182     # rewritten using Python 2.5's 'with' statement: 
    183     # for ns, handler in namespaces.iteritems(): 
    184     #     with handler as callable: 
    185     #         for k, v in ns_confs.get(ns, {}).iteritems(): 
    186     #             callable(k, v) 
    187     for ns, handler in namespaces.iteritems(): 
    188         exit = getattr(handler, "__exit__", None) 
    189         if exit: 
    190             callable = handler.__enter__() 
    191             no_exc = True 
    192             try: 
     170     
     171    def __call__(self, config): 
     172        """Iterate through config and pass it to each namespace handler. 
     173         
     174        'config' should be a flat dict, where keys use dots to separate 
     175        namespaces, and values are arbitrary. 
     176         
     177        The first name in each config key is used to look up the corresponding 
     178        namespace handler. For example, a config entry of {'tools.gzip.on': v} 
     179        will call the 'tools' namespace handler with the args: ('gzip.on', v) 
     180        """ 
     181        # Separate the given config into namespaces 
     182        ns_confs = {} 
     183        for k in config: 
     184            if "." in k: 
     185                ns, name = k.split(".", 1) 
     186                bucket = ns_confs.setdefault(ns, {}) 
     187                bucket[name] = config[k] 
     188         
     189        # I chose __enter__ and __exit__ so someday this could be 
     190        # rewritten using Python 2.5's 'with' statement: 
     191        # for ns, handler in self.iteritems(): 
     192        #     with handler as callable: 
     193        #         for k, v in ns_confs.get(ns, {}).iteritems(): 
     194        #             callable(k, v) 
     195        for ns, handler in self.iteritems(): 
     196            exit = getattr(handler, "__exit__", None) 
     197            if exit: 
     198                callable = handler.__enter__() 
     199                no_exc = True 
    193200                try: 
    194                     for k, v in ns_confs.get(ns, {}).iteritems(): 
    195                         callable(k, v) 
    196                 except: 
    197                     # The exceptional case is handled here 
    198                     no_exc = False 
    199                     if exit is None: 
    200                         raise 
    201                     if not exit(*sys.exc_info()): 
    202                         raise 
    203                     # The exception is swallowed if exit() returns true 
    204             finally: 
    205                 # The normal and non-local-goto cases are handled here 
    206                 if no_exc and exit: 
    207                     exit(None, None, None) 
    208         else: 
    209             for k, v in ns_confs.get(ns, {}).iteritems(): 
    210                 handler(k, v) 
     201                    try: 
     202                        for k, v in ns_confs.get(ns, {}).iteritems(): 
     203                            callable(k, v) 
     204                    except: 
     205                        # The exceptional case is handled here 
     206                        no_exc = False 
     207                        if exit is None: 
     208                            raise 
     209                        if not exit(*sys.exc_info()): 
     210                            raise 
     211                        # The exception is swallowed if exit() returns true 
     212                finally: 
     213                    # The normal and non-local-goto cases are handled here 
     214                    if no_exc and exit: 
     215                        exit(None, None, None) 
     216            else: 
     217                for k, v in ns_confs.get(ns, {}).iteritems(): 
     218                    handler(k, v) 
     219     
     220    def __repr__(self): 
     221        return "%s.%s(%s)" % (self.__module__, self.__class__.__name__, 
     222                              dict.__repr__(self)) 
     223     
     224    def __copy__(self): 
     225        newobj = self.__class__() 
     226        newobj.update(self) 
     227        return newobj 
     228    copy = __copy__ 
    211229 
    212230 
     
    220238        } 
    221239     
    222     namespaces = {"server": lambda k, v: setattr(cherrypy.server, k, v), 
    223                   "engine": lambda k, v: setattr(cherrypy.engine, k, v), 
    224                   "log": lambda k, v: setattr(cherrypy.log, k, v), 
    225                   "checker": lambda k, v: setattr(cherrypy.checker, k, v), 
    226                   } 
     240    namespaces = NamespaceSet( 
     241        **{"server": lambda k, v: setattr(cherrypy.server, k, v), 
     242           "engine": lambda k, v: setattr(cherrypy.engine, k, v), 
     243           "log": lambda k, v: setattr(cherrypy.log, k, v), 
     244           "checker": lambda k, v: setattr(cherrypy.checker, k, v), 
     245           }) 
    227246     
    228247    def __init__(self): 
     
    262281         
    263282        dict.update(self, config) 
    264         _call_namespaces(config, self.namespaces
     283        self.namespaces(config
    265284     
    266285    def __setitem__(self, k, v): 
    267286        dict.__setitem__(self, k, v) 
    268         _call_namespaces({k: v}, self.namespaces
     287        self.namespaces({k: v}
    269288 
    270289 
  • trunk/cherrypy/_cprequest.py

    r1609 r1612  
    77 
    88import cherrypy 
    9 from cherrypy import _cpcgifs 
     9from cherrypy import _cpcgifs, _cpconfig 
    1010from cherrypy._cperror import format_exc, bare_error 
    1111from cherrypy.lib import http 
     
    383383    HTTPError, which are more properly called 'exceptions', not errors).""" 
    384384     
    385     namespaces = {"hooks": hooks_namespace, 
    386                   "request": request_namespace, 
    387                   "response": response_namespace, 
    388                   "error_page": error_page_namespace, 
    389                   # "tools": See _cptools.Toolbox 
    390                   } 
    391     namespaces__doc = """ 
    392     A dict of config namespace names and handlers. Each config entry should 
    393     begin with a namespace name; the corresponding namespace handler will 
    394     be called once for each config entry in that namespace, and will be 
    395     passed two arguments: the config key (with the namespace removed) 
    396     and the config value. 
    397      
    398     Namespace handlers may be any Python callable; they may also be 
    399     Python 2.5-style 'context managers', in which case their __enter__ 
    400     method should return a callable to be used as the handler. 
    401     See cherrypy.tools (the Toolbox class) for an example. 
    402      
    403     Namespaces may be added at the class level and will be inherited 
    404     by all Request instances. 
    405     """ 
     385    namespaces = _cpconfig.NamespaceSet( 
     386        **{"hooks": hooks_namespace, 
     387           "request": request_namespace, 
     388           "response": response_namespace, 
     389           "error_page": error_page_namespace, 
     390           # "tools": See _cptools.Toolbox 
     391           }) 
    406392     
    407393    def __init__(self, local_host, remote_host, scheme="http", 
     
    536522                    self.toolmaps = {} 
    537523                    self.get_resource(path_info) 
    538                     cherrypy._cpconfig._call_namespaces(self.config, 
    539                                                         self.namespaces) 
     524                    self.namespaces(self.config) 
    540525                     
    541526                    self.hooks.run('on_start_resource') 
     
    719704    """ 
    720705     
     706    __metaclass__ = cherrypy._AttributeDocstrings 
     707     
    721708    # Class attributes for dev-time introspection. 
    722709    status = "" 
  • trunk/cherrypy/_cptree.py

    r1605 r1612  
    2828    of {key: value} pairs.""" 
    2929     
    30     namespaces = {} 
    31     namespaces__doc = """ 
    32     A dict of config namespace names and handlers. Each config entry should 
    33     begin with a namespace name; the corresponding namespace handler will 
    34     be called once for each config entry in that namespace, and will be 
    35     passed two arguments: the config key (with the namespace removed) 
    36     and the config value. 
    37      
    38     Namespace handlers may be any Python callable; they may also be 
    39     Python 2.5-style 'context managers', in which case their __enter__ 
    40     method should return a callable to be used as the handler. 
    41     See cherrypy.tools (the Toolbox class) for an example. 
    42     """ 
     30    namespaces = _cpconfig.NamespaceSet() 
    4331     
    4432    log = None 
     
    8169         
    8270        # Handle namespaces specified in config. 
    83         _cpconfig._call_namespaces(self.config.get("/", {}), self.namespaces
     71        self.namespaces(self.config.get("/", {})
    8472     
    8573    def __call__(self, environ, start_response): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets