1
2
3 __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
4 __version__ = "0.2"
5 __license__ = "GNU Lesser General Public License"
6
7 try:
8 from threading import Lock
9 except ImportError:
10 from dummy_threading import Lock
13 registry = {}
14 _lock = Lock()
15
17 subject = None
18 if 'subject' in kwds and not cls.__name__.startswith('Weekly'):
19 subject = kwds['subject']
20
21
22 if 'bypass_registry' in kwds:
23 del kwds['bypass_registry']
24 inst = object.__new__(cls)
25 inst.init(*args, **kwds)
26 return inst
27
28 key = cls._hash_func(*args, **kwds)
29 if subject is not None:
30 key = (hash(subject), key)
31
32 Cacheable._lock.acquire()
33 try:
34 inst, already_registered = Cacheable.register(object.__new__(cls), key)
35 if not already_registered:
36 inst.init(*args, **kwds)
37 finally:
38 Cacheable._lock.release()
39 return inst
40
41 @staticmethod
53