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

Source Code for Module PyFoam.LogAnalysis.GeneralLineAnalyzer

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/LogAnalysis/GeneralLineAnalyzer.py 7656 2012-01-06T14:43:20.069830Z bgschaid  $  
  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 -class GeneralLineAnalyzer(LogLineAnalyzer):
9 """Base class for analyzers that write data to files and store time-lines 10 11 Combines the capabilities of TimeLineLineAnalyzer and FileLineAnalyzer""" 12
13 - def __init__(self, 14 doTimelines=False, 15 doFiles=False, 16 titles=[], 17 accumulation=None, 18 singleFile=False, 19 progressTemplate=None, 20 startTime=None, 21 endTime=None):
22 """ 23 @param titles: The titles of the data elements 24 @param progressTemplate: Progress output to be reported 25 """ 26 LogLineAnalyzer.__init__(self) 27 28 self.doTimelines=doTimelines 29 self.doFiles=doFiles 30 self.singleFile=singleFile 31 32 self.files=None 33 self.titles=titles 34 35 self.setTitles(titles) 36 37 accu="first" 38 if accumulation!=None: 39 accu=accumulation 40 if self.doTimelines: 41 self.lines=TimeLineCollection(accumulation=accu) 42 else: 43 self.lines=None 44 45 self.startTime=startTime 46 self.endTime=endTime 47 48 self.master=None 49 50 self.didProgress=False 51 self.progressTemplate=progressTemplate
52
53 - def getCurrentData(self):
54 if self.lines: 55 return self.lines.getLatestData() 56 else: 57 return {}
58
59 - def setMaster(self,master):
60 """Assign another line-analyzer that will do the actual data gathering""" 61 self.master=master 62 if self.lines and self.master.lines: 63 self.master.lines.addSlave(self.lines)
64
65 - def setTitles(self,titles):
66 """ 67 Sets the titles anew 68 @param titles: the new titles 69 """ 70 if self.doFiles: 71 self.titles=titles 72 if self.files!=None: 73 self.files.setTitles(titles)
74
75 - def setDirectory(self,oDir):
76 """Creates the OutFileCollection-object""" 77 if self.doFiles: 78 self.files=OutFileCollection(oDir, 79 titles=self.titles, 80 singleFile=self.singleFile) 81 else: 82 self.files=None
83
84 - def timeChanged(self):
85 """Sets the current time in the timelines""" 86 if self.doTimelines: 87 try: 88 time=float(self.getTime()) 89 if (self.startTime==None or time>=self.startTime) and (self.endTime==None or time<=self.endTime): 90 self.lines.setTime(self.getTime()) 91 except ValueError: 92 pass 93 self.didProgress=False
94
95 - def getTimeline(self,name):
96 """@param name: Name of the timeline to return 97 @return: the timeline as two list: the times and the values""" 98 if self.doTimelines: 99 return self.lines.getTimes(),self.lines.getValues(name) 100 else: 101 return [],[]
102
103 - def doAnalysis(self,line):
104 """General analysis method. Derived classes should instead override callbacks""" 105 106 m=self.exp.match(line) 107 if m!=None: 108 self.startAnalysis(m) 109 110 if self.doTimelines: 111 try: 112 time=float(self.getTime()) 113 if (self.startTime==None or time>=self.startTime) and (self.endTime==None or time<=self.endTime): 114 self.addToTimelines(m) 115 except ValueError: 116 pass 117 if self.doFiles: 118 self.addToFiles(m) 119 120 self.endAnalysis(m) 121 122 if not self.didProgress and self.progressTemplate: 123 myProgress=self.progressTemplate 124 for i,g in enumerate(m.groups()): 125 myProgress=myProgress.replace("$%d" % i,g) 126 self.writeProgress(myProgress) 127 128 self.didProgress=False
129
130 - def startAnalysis(self,match):
131 """Method at the start of a successfull match""" 132 pass
133
134 - def endAnalysis(self,match):
135 """Method at the end of a successfull match""" 136 pass
137
138 - def addToTimelines(self,match):
139 """Method that adds matched data to timelines 140 141 @param match: data matched by a regular expression""" 142 143 pass
144
145 - def addToFiles(self,match):
146 """Method that adds matched data to files 147 148 @param match: data matched by a regular expression""" 149 150 pass
151
152 - def tearDown(self):
153 """Closes files""" 154 LogLineAnalyzer.tearDown(self) 155 156 if self.files!=None: 157 self.files.close()
158