annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/idlelib/delegator.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 class Delegator:
jpayne@69 2
jpayne@69 3 def __init__(self, delegate=None):
jpayne@69 4 self.delegate = delegate
jpayne@69 5 self.__cache = set()
jpayne@69 6 # Cache is used to only remove added attributes
jpayne@69 7 # when changing the delegate.
jpayne@69 8
jpayne@69 9 def __getattr__(self, name):
jpayne@69 10 attr = getattr(self.delegate, name) # May raise AttributeError
jpayne@69 11 setattr(self, name, attr)
jpayne@69 12 self.__cache.add(name)
jpayne@69 13 return attr
jpayne@69 14
jpayne@69 15 def resetcache(self):
jpayne@69 16 "Removes added attributes while leaving original attributes."
jpayne@69 17 # Function is really about resetting delegator dict
jpayne@69 18 # to original state. Cache is just a means
jpayne@69 19 for key in self.__cache:
jpayne@69 20 try:
jpayne@69 21 delattr(self, key)
jpayne@69 22 except AttributeError:
jpayne@69 23 pass
jpayne@69 24 self.__cache.clear()
jpayne@69 25
jpayne@69 26 def setdelegate(self, delegate):
jpayne@69 27 "Reset attributes and change delegate."
jpayne@69 28 self.resetcache()
jpayne@69 29 self.delegate = delegate
jpayne@69 30
jpayne@69 31 if __name__ == '__main__':
jpayne@69 32 from unittest import main
jpayne@69 33 main('idlelib.idle_test.test_delegator', verbosity=2)