1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """
24 Singleton Cache of ExeConfig instances
25 """
26
27 import threading
28 from Biskit import ExeConfig
29
31 """
32 Keep track of previously loaded ExeConfig instances in order to avoid the
33 multiple parsing of one and the same config file. The class contains
34 only static methods -- it is not necessary to create an instance of it.
35
36 Usage:
37 >>> my_config = ExeConfigCache.get( 'my_program' )
38
39 Should be thread-save, i.e. multiple threads can simultaniously access
40 the cache (not tested).
41 """
42
43 LOCK = threading.Lock()
44 CACHE= {}
45
46
47 @staticmethod
48 - def get( name, reload=0, **kw ):
49 """
50 Get the ExeConfig instance for the given program.
51
52 @param name: program name
53 @type name: str
54 @param reload: force new instance (re-read configuration file)
55 (default: 0)
56 @type reload: 0|1
57 @param kw: options for L{ Biskit.ExeConfig() }; no effect for
58 cached entries unless reload=1
59 @type kw: key=value
60
61 @return: ExeConfig object
62 @rtype: ExeConfig
63 """
64
65 ExeConfigCache.LOCK.acquire()
66
67 if not name in ExeConfigCache.CACHE or reload:
68 ExeConfigCache.CACHE[ name ] = ExeConfig( name, **kw )
69
70 r = ExeConfigCache.CACHE[ name ]
71
72 ExeConfigCache.LOCK.release()
73
74 return r
75
76
77 @staticmethod
88
89
90 @staticmethod
92 """
93 Number of entries in the cache.
94
95 @return: length of cache
96 @rtype: int
97 """
98 return len( ExeConfigCache.CACHE )
99
100
101
102
103
104 import Biskit.test as BT
105
106 -class Test(BT.BiskitTest):
107 """Test"""
108
109
111 """ExeConfigCache test """
112
113 self.x = ExeConfigCache.get( 'xplor' )
114
115 if self.local:
116 print self.x
117
118 self.assert_( self.x.dat_found )
119
120
121 if __name__ == '__main__':
122
123 BT.localTest()
124