2000-08-14 22:13:23 -03:00
|
|
|
class Delegator:
|
|
|
|
|
|
|
|
# The cache is only used to be able to change delegates!
|
|
|
|
|
|
|
|
def __init__(self, delegate=None):
|
|
|
|
self.delegate = delegate
|
2013-06-30 19:37:05 -03:00
|
|
|
self.__cache = set()
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
attr = getattr(self.delegate, name) # May raise AttributeError
|
|
|
|
setattr(self, name, attr)
|
2013-06-30 19:37:05 -03:00
|
|
|
self.__cache.add(name)
|
2000-08-14 22:13:23 -03:00
|
|
|
return attr
|
|
|
|
|
|
|
|
def resetcache(self):
|
2007-08-23 02:25:55 -03:00
|
|
|
for key in self.__cache:
|
2000-08-14 22:13:23 -03:00
|
|
|
try:
|
|
|
|
delattr(self, key)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
self.__cache.clear()
|
|
|
|
|
|
|
|
def setdelegate(self, delegate):
|
|
|
|
self.resetcache()
|
|
|
|
self.delegate = delegate
|