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

Source Code for Module Biskit.LogFile

  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  ## $Revision: 2.8 $ 
 21  ## last $Date: 2007/03/06 18:45:47 $ 
 22  ## last $Author: graik $ 
 23  """ 
 24  Simple log file. 
 25  """ 
 26   
 27  import Biskit.tools as T 
 28  import sys 
 29   
30 -class LogFile:
31 """ 32 Simple log file that can be passed between objects. 33 It is flushed after each writing and should hence always be 34 up2date. 35 """ 36
37 - def __init__(self, fname, mode='w'):
38 """ 39 @param fname: name of log file 40 @type fname: str 41 @param mode: mode (default: w) 42 @type mode: str 43 """ 44 self.fname = T.absfile( fname ) 45 self.mode = mode 46 self._f = None
47 48
49 - def f( self ):
50 """ 51 Open file only when needed for first time. 52 53 @return: open file handle 54 @rtype: object 55 """ 56 if self._f is None: 57 self._f = open( self.fname, self.mode ) 58 59 return self._f
60 61
62 - def write(self, s):
63 """Synonym for add_nobreak. 64 @param s: line 65 @type s: str 66 """ 67 self.add_nobreak( s )
68
69 - def writeln(self, s):
70 """Synonym for add. 71 @param s: line 72 @type s: str 73 """ 74 self.add(s)
75
76 - def add(self, s):
77 """ 78 Add new line to logfile and flush 79 80 @param s: line 81 @type s: str 82 """ 83 if not type(s) is list: 84 s = [ s ] 85 86 for i in s: 87 self.f().write(i) 88 self.f().write('\n') 89 90 self.f().flush()
91 92
93 - def add_nobreak(self, s):
94 """ 95 Add new line without creating a linebreak to logfile and flush 96 97 @param s: line 98 @type s: str 99 """ 100 self.f().write(s) 101 self.f().flush()
102 103
104 - def __del__(self):
105 """ 106 Close file handle. 107 """ 108 if self._f is not None: 109 self.f().close() 110 self._f = None
111 112
113 -class ErrLog( LogFile ):
114 """ 115 Print to stderr instead. 116 """ 117
118 - def __init__(self):
119 self.fname = None 120 self.mode = None 121 self._f = sys.stderr
122
123 - def __del__(self):
124 pass
125 126
127 -class StdLog( LogFile ):
128 """ 129 Print to std out. 130 """
131 - def __init__(self):
132 self.fname = None 133 self.mode = None 134 self._f = sys.stdout
135
136 - def __del__(self):
137 pass
138 139 140 141 ############# 142 ## TESTING 143 ############# 144 import Biskit.test as BT 145
146 -class Test(BT.BiskitTest):
147
148 - def prepare(self):
149 import tempfile 150 self.f_out = tempfile.mktemp( '_test_LogFile' )
151
152 - def cleanUp(self):
153 if T.tryRemove( self.f_out ) and self.local: 154 print 'log file removed.'
155
156 - def test_LogFile( self ):
157 """LogFile test """ 158 159 self.l = LogFile( self.f_out, mode='w') 160 161 self.l.writeln('1') 162 self.l.write('2') 163 self.l.writeln('3') 164 165 if self.local: 166 print 'log file written to %s'%self.f_out 167 168 lines = open(self.f_out).readlines() 169 self.assertEqual( lines, ['1\n','23\n'] )
170
171 - def test_StdLog(self):
172 """StdLog test (only in interactive mode)""" 173 if self.local: 174 self.l = StdLog() 175 self.l.write(' test message ... ')
176
177 - def test_ErrLog(self):
178 """ErrLog test""" 179 self.l = ErrLog() 180 self.l.write(' test message ... ')
181 182 183 if __name__ == '__main__': 184 185 BT.localTest() 186