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