Change callHooks structure.

This commit is contained in:
rowanc1
2013-12-04 15:31:11 -08:00
parent abfe1f033e
commit ccb4d4052d
4 changed files with 62 additions and 60 deletions
+33 -5
View File
@@ -23,7 +23,7 @@ def hook(obj, method, name=None, overwrite=False, silent=False):
If name is None, the name of the method is used.
"""
if name is None:
if name is None:
name = method.__name__
if name == '<lambda>':
raise Exception('Must provide name to hook lambda functions.')
@@ -87,11 +87,39 @@ def printStoppers(obj, stoppers, pad='', stop='STOP!', done='DONE!'):
print pad + stopper['str'] % (l<=r,l,r)
print pad + "%s%s%s" % ('-'*25,done,'-'*25)
def callHooks(obj, match, *args, **kwargs):
for method in [posible for posible in dir(obj) if ('_'+match) in posible]:
if getattr(obj,'debug',False): print (match+' is calling self.'+method)
getattr(obj,method)(*args, **kwargs)
def callHooks(match):
"""
Use this to wrap a funciton::
@callHooks('doEndIteration')
def doEndIteration(self):
pass
This will call everything named _doEndIteration* at the beginning of the function call.
"""
def callHooksWrap(f):
@wraps(f)
def wrapper(self,*args,**kwargs):
for method in [posible for posible in dir(self) if ('_'+match) in posible]:
if getattr(self,'debug',False): print (match+' is calling self.'+method)
getattr(self,method)(*args, **kwargs)
return f(self,*args,**kwargs)
extra = """
If you have things that also need to run in the method %s, you can create a method::
def _%s*(self, ... ):
pass
Where the * can be any string. If present, _%s* will be called at the start of the default %s call.
You may also completely overwrite this function.
""" % (match, match, match, match)
wrapper.__doc__ += extra
return wrapper
return callHooksWrap
class Counter(object):