Package PyFoam :: Module Error
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Error

 1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Error.py 7239 2011-02-23T17:26:11.661549Z bgschaid  $  
 2  """Standardized Error Messages""" 
 3   
 4  import traceback 
 5  import sys 
 6   
 7  from PyFoam.Basics.TerminalFormatter import TerminalFormatter 
 8   
 9  defaultFormat=TerminalFormatter() 
10  defaultFormat.getConfigFormat("error") 
11  defaultFormat.getConfigFormat("warning",shortName="warn") 
12   
13 -def getLine(up=0):
14 try: # just get a few frames 15 f = traceback.extract_stack(limit=up+2) 16 if f: 17 return f[0] 18 except: 19 if __debug__: 20 traceback.print_exc() 21 pass 22 return ('', 0, '', None)
23
24 -def __common(format,standard,*text):
25 """Common function for errors and Warnings""" 26 info=getLine(up=2) 27 if format: 28 print >>sys.stderr,format, 29 print >>sys.stderr, "PyFoam",standard.upper(),"on line",info[1],"of file",info[0],":", 30 for t in text: 31 print >>sys.stderr,t, 32 print >>sys.stderr,defaultFormat.reset
33
34 -def warning(*text):
35 """Prints a warning message with the occuring line number 36 @param text: The error message""" 37 __common(defaultFormat.warn,"Warning",*text)
38
39 -def oldSchoolError(*text):
40 """Prints an error message and aborts 41 @param text: The error message""" 42 __common(defaultFormat.error,"Fatal Error",*text) 43 sys.exit(-1)
44
45 -def error(*text):
46 """Raises an error that might or might not get caught 47 @param text: The error message""" 48 # __common(defaultFormat.error,"Fatal Error",*text) 49 raise FatalErrorPyFoamException(*text)
50
51 -def debug(*text):
52 """Prints a debug message with the occuring line number 53 @param text: The error message""" 54 __common(None,"Debug",*text)
55
56 -def notImplemented(obj,name):
57 """Prints a 'not implemented' message for abstract interfaces 58 @param obj: the object for which the method is not defined 59 @param name: name of the method""" 60 error("The method",name,"is not implemented in this object of type",obj.__class__)
61
62 -class PyFoamException(Exception):
63 """The simplest exception for PyFoam""" 64
65 - def __init__(self,*text):
66 self.descr=text[0] 67 for t in text[1:]: 68 self.descr+=" "+str(t)
69
70 - def __str__(self):
71 return "Problem in PyFoam: '"+self.descr+"'"
72
73 -class FatalErrorPyFoamException(PyFoamException):
74 """The PyFoam-exception that does not expect to be caught""" 75
76 - def __init__(self,*text):
77 info=getLine(up=2) 78 descr="PyFoam FATAL ERROR on line %d of file %s:" % (info[1],info[0]) 79 # super(FatalErrorPyFoamException,self).__init__(descr,*text) # does not work with Python 2.4 80 PyFoamException.__init__(self,descr,*text)
81
82 - def __str__(self):
83 return "FatalError in PyFoam: '"+self.descr+"'"
84