63 lines
2.3 KiB
Python
63 lines
2.3 KiB
Python
|
|
def getter(attrib):
|
|
return lambda self, *args, **kwargs: getattr(object.__getattribute__( self, "_o"), attrib)(*args, **kwargs)
|
|
|
|
|
|
class InterceptedProxy:
|
|
_special_names=[
|
|
'__abs__', '__add__', '__and__', '__call__', '__cmp__', '__coerce__',
|
|
'__contains__', '__delitem__', '__delslice__', '__div__', '__divmod__',
|
|
'__eq__', '__float__', '__floordiv__', '__ge__', '__getitem__',
|
|
'__getslice__', '__gt__', '__hash__', '__hex__', '__iadd__', '__iand__',
|
|
'__idiv__', '__idivmod__', '__ifloordiv__', '__ilshift__', '__imod__',
|
|
'__imul__', '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__',
|
|
'__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__',
|
|
'__long__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__',
|
|
'__neg__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',
|
|
'__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__',
|
|
'__repr__', '__reversed__', '__rfloorfiv__', '__rlshift__', '__rmod__',
|
|
'__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
|
|
'__rtruediv__', '__rxor__', '__setitem__', '__setslice__', '__sub__',
|
|
'__truediv__', '__xor__', 'next',
|
|
]
|
|
|
|
def _get_proxy_cls(cls,obj):
|
|
ocls = obj.__class__
|
|
ns = {}
|
|
|
|
for n in InterceptedProxy._special_names:
|
|
if (hasattr(ocls, n)):
|
|
ns[n] = getter(n)
|
|
return type( "InterceptedProxy(%s)" % (ocls.__name__,), (cls,), ns )
|
|
|
|
def __new__(cls,obj,listener):
|
|
pcls = InterceptedProxy._get_proxy_cls(cls,obj)
|
|
|
|
inst = object.__new__( pcls )
|
|
object.__setattr__( inst, "_o", obj)
|
|
object.__setattr__( inst, "_l", listener)
|
|
|
|
return inst
|
|
|
|
def __getattribute__(self,name):
|
|
v = getattr( object.__getattribute__( self, "_o"), name )
|
|
return v
|
|
|
|
def __setattr__(self,name,value):
|
|
o = object.__getattribute__( self, "_o")
|
|
setattr( o, name, value )
|
|
object.__getattribute__( self, "_l").notify("__setattr__",o,name,value)
|
|
|
|
def __delattr__(self,name):
|
|
delattr( object.__getattribute__( self, "_o"), name )
|
|
|
|
def __nonzero__(self):
|
|
return bool(object.__getattribute__(self, "_o"))
|
|
def __str__(self):
|
|
return str(object.__getattribute__(self, "_o"))
|
|
def __repr__(self):
|
|
return repr(object.__getattribute__(self, "_o"))
|
|
def __dir__(self):
|
|
return dir( object.__getattribute__(self, "_o") )
|
|
|