~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/idlelib/Delegator.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
class Delegator:
 
2
 
 
3
    # The cache is only used to be able to change delegates!
 
4
 
 
5
    def __init__(self, delegate=None):
 
6
        self.delegate = delegate
 
7
        self.__cache = {}
 
8
 
 
9
    def __getattr__(self, name):
 
10
        attr = getattr(self.delegate, name) # May raise AttributeError
 
11
        setattr(self, name, attr)
 
12
        self.__cache[name] = attr
 
13
        return attr
 
14
 
 
15
    def resetcache(self):
 
16
        for key in self.__cache:
 
17
            try:
 
18
                delattr(self, key)
 
19
            except AttributeError:
 
20
                pass
 
21
        self.__cache.clear()
 
22
 
 
23
    def cachereport(self):
 
24
        keys = list(self.__cache.keys())
 
25
        keys.sort()
 
26
        print(keys)
 
27
 
 
28
    def setdelegate(self, delegate):
 
29
        self.resetcache()
 
30
        self.delegate = delegate
 
31
 
 
32
    def getdelegate(self):
 
33
        return self.delegate