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

Source Code for Module PyFoam.Error

  1  #  ICE Revision: $Id$ 
  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 isatty(s):
26 """Workaround for outputstreams that don't implement isatty 27 (specifically vtkPythonStdStreamCaptureHelper) 28 """ 29 try: 30 return s.isatty 31 except AttributeError: 32 return False
33
34 -def __common(format,standard,*text):
35 """Common function for errors and Warnings""" 36 info=getLine(up=2) 37 38 isTerm=isatty(sys.stderr) 39 40 if format and isTerm: 41 print_(format, end=' ', file=sys.stderr) 42 print_("PyFoam",standard.upper(),"on line",info[1],"of file",info[0],":", end=' ', file=sys.stderr) 43 for t in text: 44 print_(t, end=' ', file=sys.stderr) 45 46 if isTerm: 47 print_(defaultFormat.reset, file=sys.stderr)
48
49 -def warning(*text):
50 """Prints a warning message with the occuring line number 51 @param text: The error message""" 52 __common(defaultFormat.warn,"Warning",*text)
53
54 -def oldSchoolError(*text):
55 """Prints an error message and aborts 56 @param text: The error message""" 57 __common(defaultFormat.error,"Fatal Error",*text) 58 sys.exit(-1)
59
60 -def error(*text):
61 """Raises an error that might or might not get caught 62 @param text: The error message""" 63 # __common(defaultFormat.error,"Fatal Error",*text) 64 raise FatalErrorPyFoamException(*text)
65
66 -def debug(*text):
67 """Prints a debug message with the occuring line number 68 @param text: The error message""" 69 __common(None,"Debug",*text)
70
71 -def notImplemented(obj,name):
72 """Prints a 'not implemented' message for abstract interfaces 73 @param obj: the object for which the method is not defined 74 @param name: name of the method""" 75 error("The method",name,"is not implemented in this object of type",obj.__class__)
76
77 -class PyFoamException(Exception):
78 """The simplest exception for PyFoam""" 79
80 - def __init__(self,*text):
81 self.descr=text[0] 82 for t in text[1:]: 83 self.descr+=" "+str(t)
84
85 - def __str__(self):
86 return "Problem in PyFoam: '"+self.descr+"'"
87
88 -class FatalErrorPyFoamException(PyFoamException):
89 """The PyFoam-exception that does not expect to be caught""" 90
91 - def __init__(self,*text):
92 info=getLine(up=2) 93 descr="PyFoam FATAL ERROR on line %d of file %s:" % (info[1],info[0]) 94 # super(FatalErrorPyFoamException,self).__init__(descr,*text) # does not work with Python 2.4 95 PyFoamException.__init__(self,descr,*text)
96
97 - def __str__(self):
98 return "FatalError in PyFoam: '"+self.descr+"'"
99 100 # Should work with Python3 and Python2 101