diff options
author | Peter Hunt <none@none> | 2005-06-25 20:01:48 +0000 |
---|---|---|
committer | Peter Hunt <none@none> | 2005-06-25 20:01:48 +0000 |
commit | 95b1f7e9c51fde72758e60bd69c0875719ab2117 (patch) | |
tree | e4bc72a5cf082807b818876ef2107de31c94d9c7 | |
parent | f2c51a30eeb6174487c65d35dcaae750a62dffa4 (diff) | |
download | cherrypy-git-95b1f7e9c51fde72758e60bd69c0875719ab2117.tar.gz |
added fun decorator utility funcs and getSPecialAttributePath
-rw-r--r-- | cherrypy/_cputil.py | 16 | ||||
-rw-r--r-- | cherrypy/lib/cptools.py | 29 |
2 files changed, 42 insertions, 3 deletions
diff --git a/cherrypy/_cputil.py b/cherrypy/_cputil.py index 295f16c7..b265aec0 100644 --- a/cherrypy/_cputil.py +++ b/cherrypy/_cputil.py @@ -64,7 +64,9 @@ def getObjectTrail(): return None def getSpecialAttribute(name): - """ Return the special attribute """ + """ Return the special attribute. A special attribute is
+ one that applies to all of the children from where it is
+ defined, such as _cpFilterList.""" # First, we look in the right-most object if this special attribute is implemented. # If not, then we try the previous object and so on until we reach cpg.root @@ -83,9 +85,19 @@ def getSpecialAttribute(name): return globals()[name] except KeyError: raise cperror.InternalError("Special attribute %s could not be found" + % repr(name))
+
+def getSpecialAttributePath(name):
+ """ Return the path to the special attribute """
+ objectList = getObjectTrail()
+ if objectList:
+ pathList = (cpg.request.objectPath or cpg.request.path).split("/")[1:]
+ for i in xrange(len(objectList) - 1, -1, -1):
+ if hasattr(objectList[i], name):
+ return "/" + "/".join(pathList[:i] + [name])
+ raise cperror.InternalError("Special attribute %s could not be found" % repr(name)) - def _cpLogMessage(msg, context = '', severity = 0): """ Default method for logging messages """ diff --git a/cherrypy/lib/cptools.py b/cherrypy/lib/cptools.py index 21b4d569..295a4f7e 100644 --- a/cherrypy/lib/cptools.py +++ b/cherrypy/lib/cptools.py @@ -27,8 +27,35 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ """ -Just a few convenient functions +Just a few convenient functions and classes. """ +
+import inspect
+
+def decorate(func, decorator):
+ """
+ Return the decorated func. This will automatically copy all
+ non-standard attributes (like exposed) to the newly decorated function.
+ """
+ newfunc = decorator(func)
+ for (k,v) in inspect.getmembers(func):
+ if not hasattr(newfunc, k):
+ setattr(newfunc, k, v)
+ return newfunc
+
+def decorateAll(obj, decorator):
+ """
+ Recursively decorate all exposed functions of obj and all of its children,
+ grandchildren, etc. If you used to use aspects, you might want to look
+ into these. This function modifies obj; there is no return value.
+ """
+ obj_type = type(obj)
+ for (k,v) in inspect.getmembers(obj):
+ if hasattr(obj_type, k): # only deal with user-defined attributes
+ continue
+ if callable(v) and getattr(v, "exposed", False):
+ setattr(obj, k, decorate(v, decorator))
+ decorateAll(v, decorator)
class ExposeItems: """ |