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

Source Code for Module PyFoam.LogAnalysis.GeneralLineAnalyzer

  1  #  ICE Revision: $Id$ 
  2  """Line analyzer with output and the capability to store lines""" 
  3   
  4  from .LogLineAnalyzer import LogLineAnalyzer 
  5  from PyFoam.Basics.OutFileCollection import OutFileCollection 
  6  from PyFoam.Basics.TimeLineCollection import TimeLineCollection 
  7   
  8  from PyFoam.Error import warning 
  9  import sys 
10 11 -class GeneralLineAnalyzer(LogLineAnalyzer):
12 """Base class for analyzers that write data to files and store time-lines 13 14 Combines the capabilities of TimeLineLineAnalyzer and FileLineAnalyzer""" 15 16 # phase of the solver to distinguish similar results 17 __phase = "" 18
19 - def __init__(self, 20 doTimelines=False, 21 doFiles=False, 22 titles=[], 23 accumulation=None, 24 singleFile=False, 25 progressTemplate=None, 26 startTime=None, 27 endTime=None):
28 """ 29 @param titles: The titles of the data elements 30 @param progressTemplate: Progress output to be reported 31 """ 32 LogLineAnalyzer.__init__(self) 33 34 self.doTimelines=doTimelines 35 self.doFiles=doFiles 36 self.singleFile=singleFile 37 38 self.files=None 39 self.titles=titles 40 41 self.setTitles(titles) 42 43 accu="first" 44 if accumulation!=None: 45 accu=accumulation 46 if self.doTimelines: 47 self.lines=TimeLineCollection(accumulation=accu) 48 else: 49 self.lines=None 50 51 self.startTime=startTime 52 self.endTime=endTime 53 54 self.master=None 55 56 self.didProgress=False 57 self.progressTemplate=progressTemplate
58 59 @staticmethod
60 - def setPhase(p=""):
62 63 @staticmethod
64 - def fName(n):
65 if GeneralLineAnalyzer.__phase=="": 66 return n 67 else: 68 return n+"_"+GeneralLineAnalyzer.__phase
69
70 - def getCurrentData(self):
71 if self.lines: 72 return self.lines.getLatestData() 73 else: 74 return {}
75
76 - def setMaster(self,master):
77 """Assign another line-analyzer that will do the actual data gathering""" 78 self.master=master 79 if self.lines and self.master.lines: 80 self.master.lines.addSlave(self.lines)
81
82 - def setTitles(self,titles):
83 """ 84 Sets the titles anew 85 @param titles: the new titles 86 """ 87 if self.doFiles: 88 self.titles=titles 89 if self.files!=None: 90 self.files.setTitles(titles)
91
92 - def setDirectory(self,oDir):
93 """Creates the OutFileCollection-object""" 94 if self.doFiles: 95 self.files=OutFileCollection(oDir, 96 titles=self.titles, 97 singleFile=self.singleFile) 98 else: 99 self.files=None
100
101 - def timeChanged(self):
102 """Sets the current time in the timelines""" 103 if self.doTimelines: 104 try: 105 time=float(self.getTime()) 106 if (self.startTime==None or time>=self.startTime) and (self.endTime==None or time<=self.endTime): 107 self.lines.setTime(self.getTime()) 108 except ValueError: 109 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 110 warning("Problem with lines",e) 111 raise e 112 113 self.didProgress=False 114 self.setPhase()
115
116 - def getTimeline(self,name):
117 """@param name: Name of the timeline to return 118 @return: the timeline as two list: the times and the values""" 119 if self.doTimelines: 120 return self.lines.getTimes(),self.lines.getValues(name) 121 else: 122 return [],[]
123
124 - def stringToMatch(self,line):
125 """Returns string to match. To be overriden for multi-line expressions""" 126 return line
127
128 - def doAnalysis(self,line):
129 """General analysis method. Derived classes should instead override callbacks""" 130 131 m=self.exp.match(self.stringToMatch(line)) 132 if m!=None: 133 self.startAnalysis(m) 134 135 if self.doTimelines: 136 try: 137 time=float(self.getTime()) 138 if (self.startTime==None or time>=self.startTime) and (self.endTime==None or time<=self.endTime): 139 self.addToTimelines(m) 140 except ValueError: 141 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 142 warning("Problem doing timelines",e) 143 144 if self.doFiles: 145 self.addToFiles(m) 146 147 self.endAnalysis(m) 148 149 if not self.didProgress and self.progressTemplate: 150 myProgress=self.progressTemplate 151 for i,g in enumerate(m.groups()): 152 myProgress=myProgress.replace("$%d" % i,g) 153 self.writeProgress(myProgress) 154 155 self.didProgress=False
156
157 - def startAnalysis(self,match):
158 """Method at the start of a successfull match""" 159 pass
160
161 - def endAnalysis(self,match):
162 """Method at the end of a successfull match""" 163 pass
164
165 - def addToTimelines(self,match):
166 """Method that adds matched data to timelines 167 168 @param match: data matched by a regular expression""" 169 170 pass
171
172 - def addToFiles(self,match):
173 """Method that adds matched data to files 174 175 @param match: data matched by a regular expression""" 176 177 pass
178
179 - def tearDown(self):
180 """Closes files""" 181 LogLineAnalyzer.tearDown(self) 182 183 if self.files!=None: 184 self.files.close()
185 186 # Should work with Python3 and Python2 187