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

Changeset 2056

Show
Ignore:
Timestamp:
10/30/08 15:49:19
Author:
fumanchu
Message:

First crack at promoting default handlers to anywhere in the tree (not just leaf nodes). Still some broken tests in test_objectmapping.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/turbodefault/cherrypy/_cpdispatch.py

    r2030 r2056  
    224224        if "/" in app.config: 
    225225            nodeconf.update(app.config["/"]) 
    226         object_trail = [['root', root, nodeconf, curpath]] 
    227226         
    228227        node = root 
    229228        names = [x for x in path.strip('/').split('/') if x] + ['index'] 
    230         for name in names: 
     229        object_trail = [['root', root, nodeconf, curpath, names, False]] 
     230         
     231        for i in range(len(names)): 
     232            name = names[i] 
    231233            # map to legal Python identifiers (replace '.' with '_') 
    232234            objname = name.replace('.', '_') 
    233235             
    234236            nodeconf = {} 
    235             node = getattr(node, objname, None) 
    236             if node is not None: 
     237            is_index = False 
     238             
     239            subnode = getattr(node, objname, None) 
     240            if subnode is None: 
     241                # Try a "default" method on the current leaf. 
     242                subnode = getattr(node, "default", None) 
     243                if subnode is not None: 
     244                    is_index = path.endswith('/') 
     245                # Include the current (unfound) name in the vpath args 
     246                vpath = names[i:-1] 
     247            else: 
     248                if i == len(names) - 1: 
     249                    # We found the extra ".index". Mark request so tools 
     250                    # can redirect if path_info has no trailing slash. 
     251                    is_index = True 
     252                else: 
     253                    # We're not at an 'index' handler. Mark request so tools 
     254                    # can redirect if path_info has NO trailing slash. 
     255                    # Note that this also includes handlers which take 
     256                    # positional parameters (virtual paths). 
     257                    is_index = False 
     258                vpath = names[i+1:-1] 
     259            object_trail.append([name, subnode, nodeconf, curpath, vpath, is_index]) 
     260             
     261            if subnode is not None: 
    237262                # Get _cp_config attached to this node. 
    238                 if hasattr(node, "_cp_config"): 
    239                     nodeconf.update(node._cp_config) 
     263                if hasattr(subnode, "_cp_config"): 
     264                    nodeconf.update(subnode._cp_config) 
     265            node = subnode 
    240266             
    241267            # Mix in values from app.config for this path. 
     
    243269            if curpath in app.config: 
    244270                nodeconf.update(app.config[curpath]) 
    245              
    246             object_trail.append([name, node, nodeconf, curpath]) 
    247271         
    248272        def set_conf(): 
     
    251275            # Note that we merge the config from each node 
    252276            # even if that node was None. 
    253             for name, obj, conf, curpath in object_trail: 
     277            for name, obj, conf, curpath, vpath, is_index in object_trail: 
    254278                base.update(conf) 
    255279                if 'tools.staticdir.dir' in conf: 
     
    261285        for i in xrange(num_candidates, -1, -1): 
    262286             
    263             name, candidate, nodeconf, curpath = object_trail[i] 
     287            name, candidate, nodeconf, curpath, vpath, is_index = object_trail[i] 
    264288            if candidate is None: 
    265289                continue 
    266              
    267             # Try a "default" method on the current leaf. 
    268             if hasattr(candidate, "default"): 
    269                 defhandler = candidate.default 
    270                 if getattr(defhandler, 'exposed', False): 
    271                     # Insert any extra _cp_config from the default handler. 
    272                     conf = getattr(defhandler, "_cp_config", {}) 
    273                     object_trail.insert(i+1, ["default", defhandler, conf, curpath]) 
    274                     request.config = set_conf() 
    275                     # See http://www.cherrypy.org/ticket/613 
    276                     request.is_index = path.endswith("/") 
    277                     return defhandler, names[i:-1] 
    278              
    279             # Uncomment the next line to restrict positional params to "default". 
    280             # if i < num_candidates - 2: continue 
    281290             
    282291            # Try the current leaf. 
    283292            if getattr(candidate, 'exposed', False): 
    284293                request.config = set_conf() 
    285                 if i == num_candidates: 
    286                     # We found the extra ".index". Mark request so tools 
    287                     # can redirect if path_info has no trailing slash. 
    288                     request.is_index = True 
    289                 else: 
    290                     # We're not at an 'index' handler. Mark request so tools 
    291                     # can redirect if path_info has NO trailing slash. 
    292                     # Note that this also includes handlers which take 
    293                     # positional parameters (virtual paths). 
    294                     request.is_index = False 
    295                 return candidate, names[i:-1] 
     294                request.is_index = is_index 
     295                return candidate, vpath 
    296296         
    297297        # We didn't find anything 

Hosted by WebFaction

Log in as guest/cpguest to create tickets