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

Changeset 381

Show
Ignore:
Timestamp:
06/25/05 15:01:48
Author:
peterhunt
Message:

added fun decorator utility funcs and getSPecialAttributePath

Files:

Legend:

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

    r377 r381  
    6565 
    6666def getSpecialAttribute(name): 
    67     """ Return the special attribute """ 
     67    """ Return the special attribute. A special attribute is 
     68    one that applies to all of the children from where it is 
     69    defined, such as _cpFilterList.""" 
    6870     
    6971    # First, we look in the right-most object if this special attribute is implemented. 
     
    8688                                    % repr(name)) 
    8789 
     90def getSpecialAttributePath(name): 
     91    """ Return the path to the special attribute """ 
     92    objectList = getObjectTrail() 
     93    if objectList: 
     94        pathList = (cpg.request.objectPath or cpg.request.path).split("/")[1:] 
     95        for i in xrange(len(objectList) - 1, -1, -1): 
     96            if hasattr(objectList[i], name): 
     97                return "/" + "/".join(pathList[:i] + [name]) 
     98    raise cperror.InternalError("Special attribute %s could not be found" 
     99                                    % repr(name)) 
    88100 
    89101def _cpLogMessage(msg, context = '', severity = 0): 
  • trunk/cherrypy/lib/cptools.py

    r376 r381  
    2828 
    2929""" 
    30 Just a few convenient functions 
     30Just a few convenient functions and classes. 
    3131""" 
     32 
     33import inspect 
     34 
     35def decorate(func, decorator): 
     36    """ 
     37    Return the decorated func. This will automatically copy all 
     38    non-standard attributes (like exposed) to the newly decorated function. 
     39    """ 
     40    newfunc = decorator(func) 
     41    for (k,v) in inspect.getmembers(func): 
     42        if not hasattr(newfunc, k): 
     43            setattr(newfunc, k, v) 
     44    return newfunc 
     45 
     46def decorateAll(obj, decorator): 
     47    """ 
     48    Recursively decorate all exposed functions of obj and all of its children, 
     49    grandchildren, etc. If you used to use aspects, you might want to look 
     50    into these. This function modifies obj; there is no return value. 
     51    """ 
     52    obj_type = type(obj) 
     53    for (k,v) in inspect.getmembers(obj): 
     54        if hasattr(obj_type, k): # only deal with user-defined attributes 
     55            continue 
     56        if callable(v) and getattr(v, "exposed", False): 
     57            setattr(obj, k, decorate(v, decorator)) 
     58        decorateAll(v, decorator) 
    3259 
    3360class ExposeItems: 

Hosted by WebFaction

Log in as guest/cpguest to create tickets