def weave_all_methods(aspect, class_or_object): p = PointCut() _dict = dict(inspect.getmembers(class_or_object, inspect.ismethod)) for met_name in _dict: if not met_name.startswith("__"): p.addMethod(class_or_object, met_name) aspect.updatePointCut(p) for met_name in _dict: if not met_name.startswith("__"): __weave_method(aspect, class_or_object, met_name)
def weave_all_methods(aspect, class_or_object): p = PointCut() _dict = dict(inspect.getmembers(class_or_object, inspect.ismethod)) for met_name in _dict: if not met_name.startswith('__'): p.addMethod(class_or_object, met_name) aspect.updatePointCut(p) for met_name in _dict: if not met_name.startswith('__'): __weave_method(aspect, class_or_object, met_name)
def weave_method(aspect, class_or_object, met_name): p = PointCut() p.addMethod(class_or_object, met_name) aspect.updatePointCut(p) __weave_method(aspect, class_or_object, met_name)
def __new__(cls, classname, bases, classdict): _pointcut = PointCut() def updatePointCut(cls, pc): for wobj, met_names in pc.items(): for met_name in met_names: cls._pointcut.addMethod(wobj, met_name) def before(cls, wobj, data, *args, **kwargs): if cls.hasJoinPoint(wobj, data): met = getattr(cls, 'before__original') return met.im_func(cls, wobj, data, *args, **kwargs) def after(cls, wobj, data, *args, **kwargs): if cls.hasJoinPoint(wobj, data): met = getattr(cls, 'after__original') return met.im_func(cls, wobj, data, *args, **kwargs) def around(cls, wobj, data, *args, **kwargs): if cls.hasJoinPoint(wobj, data): met = getattr(cls, 'around__original') return met.im_func(cls, wobj, data, *args, **kwargs) def hasJoinPoint(cls, wobj, data): met_name = data['original_method_name'] if cls._pointcut.has_key(wobj): if met_name in cls._pointcut[wobj]: return True ## # Try object's class if instance is not found klass = data['__class__'] if cls._pointcut.has_key(klass): if met_name in cls._pointcut[klass]: return True return False def proceed(cls, wobj, data, *args, **kwargs): # continue on running the original method if data.has_key('original_method'): if inspect.isclass(wobj): return data['original_method'](wobj, *args, **kwargs) else: return data['original_method'](*args, **kwargs) # bind/rebind methods/attributes if classdict.has_key('before'): classdict['before__original'] = classdict['before'] classdict['before'] = before if classdict.has_key('after'): classdict['after__original'] = classdict['after'] classdict['after'] = after if classdict.has_key('around'): classdict['around__original'] = classdict['around'] classdict['around'] = around classdict['proceed'] = proceed classdict['_pointcut'] = _pointcut classdict['updatePointCut'] = updatePointCut classdict['hasJoinPoint'] = hasJoinPoint selfclass = super(MetaAspect, cls).__new__\ (cls, classname, bases, classdict) return selfclass