1
2 """Analyze OpenFOAM logs"""
3
4 from TimeLineAnalyzer import TimeLineAnalyzer
5 from PyFoam.Basics.LineReader import LineReader
6 from PyFoam.Error import error
7
9 """Base class for all analyzers
10
11 Administrates and calls a number of LogLineAnlayzers for each
12 line"""
13
28
30 """Remove reference to self in children (hoping to remove
31 circular dependencies)"""
32
33 for a in self.analyzers.values():
34 a.tearDown()
35 a.setParent(None)
36
38 """Sets the time and alert all the LineAnalyzers that the time has changed
39 @param time: the new value of the time
40 """
41 if time!=self.time:
42 self.time=time
43 for listener in self.timeListeners:
44 listener.timeChanged()
45 for nm in self.analyzers:
46 self.analyzers[nm].timeChanged()
47 self.checkTriggers()
48
50 """@param listener: An object that is notified when the time changes. Has to
51 implement a timeChanged method"""
52 if not 'timeChanged' in dir(listener):
53 error("Error. Object has no timeChanged-method:"+str(listener))
54 else:
55 self.timeListeners.append(listener)
56
58 """@returns: A list with the names of the Analyzers"""
59 return self.analyzers.keys()
60
62 """Is this LogLineAnalyzer name there"""
63 return self.analyzers.has_key(name)
64
66 """Get the LogLineAnalyzer name"""
67 if self.analyzers.has_key(name):
68 return self.analyzers[name]
69 else:
70 return None
71
73 """Adds an analyzer
74
75 obj - A LogLineAnalyzer
76 name - the name of the analyzer"""
77
78 obj.setParent(self)
79 self.analyzers[name]=obj
80
82 """Calls all the anlyzers for a line"""
83 for nm in self.analyzers:
84 self.analyzers[nm].doAnalysis(line)
85
87 """Analyzes a file (one line at a time)
88
89 fh - handle of the file"""
90 while(self.line.read(fh)):
91 self.analyzeLine(self.line.line)
92
94 """Checks with all the analyzers
95
96 If one analyzer returns False it returns False"""
97 result=True
98
99 for nm in self.analyzers:
100
101 result=result and self.analyzers[nm].goOn()
102
103 return result
104
106 """Gets the current time"""
107 return str(self.time)
108
110 """Sets the output directory for all the analyzers"""
111 self.oDir=d
112 for nm in self.analyzers:
113 self.analyzers[nm].setDirectory(self.oDir)
114
116 """Gets the output directory"""
117 return self.oDir
118
119 - def addTrigger(self,time,func,once=True,until=None):
120 """Adds a trigger function that is to be called as soon as
121 the simulation time exceeds a certain value
122 @param time: the time at which the function should be triggered
123 @param func: the trigger function
124 @param once: Should this function be called once or at every time-step
125 @param until: The time until which the trigger should be called"""
126
127 data={}
128 data["time"]=float(time)
129 data["func"]=func
130 if until!=None:
131 data["until"]=float(until)
132 once=False
133 data["once"]=once
134
135 self.timeTriggers.append(data)
136
138 """Check for and execute the triggered functions"""
139
140 remove=[]
141 for i in range(len(self.timeTriggers)):
142 t=self.timeTriggers[i]
143 if t["time"]<=self.time:
144 t["func"]()
145 if t["once"]:
146 remove.append(i)
147 elif "until" in t:
148 if t["until"]<=self.time:
149 remove.append(i)
150
151 remove.reverse()
152
153 for i in remove:
154 self.timeTriggers.pop(i)
155