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

Source Code for Module Biskit.ErrorHandler

  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.7 $ 
 21  ## last $Date: 2007/02/28 22:37:32 $ 
 22  ## last $Author: graik $ 
 23   
 24  """ 
 25  Default Error Handler for Biskit classes. 
 26  """ 
 27       
 28  import Biskit.tools as T 
 29  from Biskit.LogFile import ErrLog 
 30  from Biskit.Errors import HandledError, NormalError, FatalError 
 31   
32 -class ErrorHandler( object ):
33 """ 34 Default Error Handler for Biskit classes. 35 """ 36
37 - def __init__( self, log=None ):
38 """ 39 @param log: target of error messages, None->StdErr (default: None) 40 @type log: LogFile 41 """ 42 self.log = log or ErrLog()
43 44
45 - def fatal( self, message ):
46 """ 47 Handle a fatal error (likely a bug), stop program execution. 48 49 @param message: message to be given to user 50 @type message: str 51 52 @raise FatalError: 53 """ 54 s = '\nFatal Error: '+str(message) 55 s += '\n\t' + T.lastError() + '\n' 56 s += 'TraceBack: \n' + T.lastErrorTrace() + '\n' 57 58 self.log.add(s) 59 raise FatalError
60 61
62 - def error( self, message ):
63 """ 64 Handle a normal error (like non-existing file) that is not 65 necessarily a bug. 66 67 @param message: message to be given to user 68 @type message: str 69 70 @raise NormalError: 71 """ 72 s = '\nError: '+str(message) 73 s += '\n\t' + T.lastError() 74 s += '\nTraceBack: \n' + T.lastErrorTrace() + '\n' 75 76 self.log.add(s) 77 raise NormalError
78 79
80 - def warning( self, message, error=1, trace=0 ):
81 """ 82 Issue a warning. No exception is raised. 83 84 @param message: message to be given to user 85 @type message: str 86 @param error: report Exception with line (default: 1) 87 @type error: 1||0 88 @param trace: report full back trace to exception (default: 0) 89 @type trace: 1||0 90 """ 91 s = '\nWarning (ignored): '+str(message) 92 try: 93 if trace: error = 1 94 if error: 95 s += '\n\t' + T.lastError() + '\n' 96 if trace: 97 s += '\nTraceBack: \n' + T.lastErrorTrace() + '\n' 98 except: 99 pass 100 101 self.log.add(s)
102 103 104 105 ############# 106 ## TESTING 107 ############# 108 import Biskit.test as BT 109
110 -class Test(BT.BiskitTest):
111 """ErrorHandler test""" 112
113 - def prepare(self):
114 import tempfile 115 self.f_out = tempfile.mktemp( '_test_ErrorHandler' )
116
117 - def cleanUp(self):
118 T.tryRemove( self.f_out )
119
120 - def test_ErrorHandler( self ):
121 """ErrorHandler test""" 122 from Biskit.LogFile import LogFile 123 124 self.err_log = LogFile( self.f_out ) 125 126 self.e = ErrorHandler( log=self.err_log ) 127 self.e.warning( 'A warning' ) 128 129 if self.local: 130 print 'An error log file was written to %s'%self.f_out 131 132 lines = open(self.err_log.fname).readlines() 133 self.assertEquals(lines[-1],'Warning (ignored): A warning\n')
134 135 if __name__ == '__main__': 136 137 BT.localTest(debug=0) 138