Package PyFoam :: Package LogAnalysis :: Module FoamLogAnalyzer
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.LogAnalysis.FoamLogAnalyzer

  1  """Analyze OpenFOAM logs""" 
  2   
  3  from LogLineAnalyzer import LogLineAnalyzer 
  4  from TimeLineAnalyzer import TimeLineAnalyzer 
  5  from PyFoam.Basics.LineReader import LineReader 
  6  from PyFoam.Error import error 
  7   
  8  import sys 
  9   
10 -class FoamLogAnalyzer(object):
11 """Base class for all analyzers 12 13 Administrates and calls a number of LogLineAnlayzers for each 14 line""" 15
16 - def __init__(self,progress=False):
17 """ 18 @param progress: Print time progress on console? 19 """ 20 self.analyzers={} 21 self.time="" 22 self.oDir="" 23 self.line=LineReader() 24 self.timeListeners=[] 25 26 self.addAnalyzer("Time",TimeLineAnalyzer(progress=progress))
27
28 - def tearDown(self):
29 """Remove reference to self in children (hoping to remove 30 circular dependencies)""" 31 32 for a in self.analyzers.values(): 33 a.setParent(None)
34
35 - def setTime(self,time):
36 """Sets the time and alert all the LineAnalyzers that the time has changed 37 @param time: the new value of the time 38 """ 39 if time!=self.time: 40 self.time=time 41 for listener in self.timeListeners: 42 listener.timeChanged() 43 for nm in self.analyzers: 44 self.analyzers[nm].timeChanged()
45
46 - def addTimeListener(self,listener):
47 """@param listener: An object that is notified when the time changes. Has to 48 implement a timeChanged method""" 49 if not 'timeChanged' in dir(listener): 50 error("Error. Object has no timeChanged-method:"+str(listener)) 51 else: 52 self.timeListeners.append(listener)
53
54 - def listAnalyzers(self):
55 """@returns: A list with the names of the Analyzers""" 56 return self.analyzers.keys()
57
58 - def getAnalyzer(self,name):
59 """Get the LogLineAnalyzer name""" 60 if self.analyzers.has_key(name): 61 return self.analyzers[name] 62 else: 63 return None
64
65 - def addAnalyzer(self,name,obj):
66 """Adds an analyzer 67 68 obj - A LogLineAnalyzer 69 name - the name of the analyzer""" 70 71 obj.setParent(self) 72 self.analyzers[name]=obj
73
74 - def analyzeLine(self,line):
75 """Calls all the anlyzers for a line""" 76 for nm in self.analyzers: 77 self.analyzers[nm].doAnalysis(line)
78
79 - def analyze(self,fh):
80 """Analyzes a file (one line at a time) 81 82 fh - handle of the file""" 83 while(self.line.read(fh)): 84 self.analyzeLine(self.line.line)
85
86 - def goOn(self):
87 """Checks with all the analyzers 88 89 If one analyzer returns False it returns False""" 90 result=True 91 92 for nm in self.analyzers: 93 # print nm,self.analyzers[nm].goOn() 94 result=result and self.analyzers[nm].goOn() 95 96 return result
97
98 - def getTime(self):
99 """Gets the current time""" 100 return str(self.time)
101
102 - def setDirectory(self,d):
103 """Sets the output directory for all the analyzers""" 104 self.oDir=d 105 for nm in self.analyzers: 106 self.analyzers[nm].setDirectory(self.oDir)
107
108 - def getDirectory(self):
109 """Gets the output directory""" 110 return self.oDir
111