Package Biskit :: Module ExeConfigCache
[hide private]
[frames] | no frames]

Source Code for Module Biskit.ExeConfigCache

  1  ## 
  2  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  3  ## Copyright (C) 2004-2006 Raik Gruenberg & Johan Leckner 
  4  ## 
  5  ## This program is free software; you can redistribute it and/or 
  6  ## modify it under the terms of the GNU General Public License as 
  7  ## published by the Free Software Foundation; either version 2 of the 
  8  ## License, or any later version. 
  9  ## 
 10  ## This program is distributed in the hope that it will be useful, 
 11  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 13  ## General Public License for more details. 
 14  ## 
 15  ## You find a copy of the GNU General Public License in the file 
 16  ## license.txt along with this program; if not, write to the Free 
 17  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 18  ## 
 19   
 20  ## last $Author: graik $ 
 21  ## last $Date: 2007/04/06 10:16:07 $ 
 22  ## $Revision: 2.8 $ 
 23  """ 
 24  Singleton Cache of ExeConfig instances 
 25  """ 
 26   
 27  import threading 
 28  from Biskit import ExeConfig 
 29   
30 -class ExeConfigCache:
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
78 - def reset():
79 """ 80 Empty the cache. 81 """ 82 83 ExeConfigCache.LOCK.acquire() 84 85 ExeConfigCache.CACHE = {} 86 87 ExeConfigCache.LOCK.release()
88 89 90 @staticmethod
91 - def len():
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 ## TESTING 103 ############# 104 import Biskit.test as BT 105
106 -class Test(BT.BiskitTest):
107 """Test""" 108 109
110 - def test_Hmmer( self):
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