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

Source Code for Module PyFoam.LogAnalysis.RegExpLineAnalyzer

  1  #  ICE Revision: $Id$ 
  2  """Analyzes lines with regular expressions""" 
  3   
  4  import re 
  5   
  6  from .GeneralLineAnalyzer import GeneralLineAnalyzer 
  7   
8 -class RegExpLineAnalyzer(GeneralLineAnalyzer):
9 """Parses lines for an arbitrary regular expression 10 11 Only one data-set is stored per time-step 12 13 One pattern group of the RegExp can be used as a unique 14 identifier, so that more than one data-sets can be stored per 15 time-step 16 17 The string %f% in the regular expression is replaced with the 18 regular expression for a floating point number 19 """ 20 21 floatRegExp="[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?" 22
23 - def __init__(self, 24 name, 25 exp, 26 idNr=None, 27 titles=[], 28 doTimelines=False, 29 doFiles=True, 30 accumulation=None, 31 progressTemplate=None, 32 singleFile=False, 33 startTime=None, 34 endTime=None):
35 """ 36 @param name: name of the expression (needed for output 37 @param exp: the regular expression, %f% will be replaced with the 38 regular expression for a float 39 @param idNr: number of the pattern group that is used as an identifier 40 @param titles: titles of the columns 41 @param accumulation: How multiple values should be accumulated 42 """ 43 GeneralLineAnalyzer.__init__(self, 44 titles=titles, 45 doTimelines=doTimelines, 46 doFiles=doFiles, 47 accumulation=accumulation, 48 progressTemplate=progressTemplate, 49 singleFile=singleFile, 50 startTime=startTime, 51 endTime=endTime) 52 53 self.name=name 54 self.idNr=idNr 55 56 self.multiLine=False 57 self.linesToMatch=None 58 59 exp=exp.replace("%f%",self.floatRegExp) 60 61 self.strExp=exp 62 reFlags=0 63 64 if self.strExp.find(r"\n")>-1: 65 self.multiLine=True 66 from collections import deque 67 68 self.linesToMatch=deque([],maxlen=1+self.strExp.count(r'\n')) 69 reFlags=re.MULTILINE 70 71 self.exp=re.compile(self.strExp,reFlags) 72 73 self.data={}
74
75 - def stringToMatch(self,line):
76 """Returns string to match. To be overriden for multi-line expressions""" 77 if self.multiLine: 78 self.linesToMatch.append(line) 79 80 return "\n".join(self.linesToMatch) 81 else: 82 return line
83
84 - def startAnalysis(self,match):
85 self.tm=self.parent.getTime() 86 if self.tm=="": 87 self.tm="-1e10"
88
89 - def addToFiles(self,match):
90 name=self.fName(self.name) 91 fdata=match.groups() 92 if self.idNr!=None: 93 ID=match.group(self.idNr) 94 name+="_"+ID 95 fdata=fdata[:self.idNr-1]+fdata[self.idNr:] 96 else: 97 ID="" 98 99 self.sub(ID)[float(self.tm)]=fdata 100 if ID!="": 101 self.sub("")[float(self.tm)]=match.groups() 102 103 self.files.write(name,self.tm,fdata)
104
105 - def addToTimelines(self,match):
106 name=self.fName(self.name) 107 fdata=match.groups() 108 109 prefix="" 110 if self.idNr!=None: 111 ID=match.group(self.idNr) 112 prefix=ID+"_" 113 fdata=fdata[:self.idNr-1]+fdata[self.idNr:] 114 115 for i in range(len(fdata)): 116 val=float(fdata[i]) 117 name=prefix+"value %d" % i 118 if i<len(self.titles): 119 if self.idNr!=None and self.titles[i].find("%s")>=0: 120 name=self.titles[i] % ID 121 else: 122 name=prefix+str(self.titles[i]) 123 124 self.lines.setValue(name,val)
125
126 - def sub(self,ID):
127 """ get the data set for the identifier ID""" 128 if ID not in self.data: 129 self.data[ID]={} 130 return self.data[ID]
131
132 - def getTimes(self,ID=None):
133 """get the available time for the identifier ID""" 134 if ID==None: 135 ID="" 136 return list(self.sub(ID).keys())
137
138 - def getIDs(self):
139 """get a list of the available IDs""" 140 ids=list(self.data.keys()) 141 if "" in ids: 142 ids.remove("") 143 return ids
144
145 - def getLast(self,ID=None):
146 """get the last time for the identifier ID""" 147 times=self.getTimes(ID) 148 if len(times)>0: 149 return max(times) 150 else: 151 return None
152
153 - def getData(self,time=None,ID=None):
154 """get a data value at a specific time for a specific ID""" 155 if ID==None: 156 ID="" 157 158 if time==None: 159 time=self.getLast(ID) 160 else: 161 time=float(time) 162 163 data=self.sub(ID) 164 165 if time in data: 166 return data[time] 167 else: 168 return None
169
170 -class RegExpTimeLineLineAnalyzer(RegExpLineAnalyzer):
171 """Class that stores results as timelines, too""" 172
173 - def __init__(self, 174 name, 175 exp, 176 titles=[], 177 startTime=None, 178 endTime=None):
179 """ 180 @param name: name of the expression (needed for output 181 @param exp: the regular expression, %f% will be replaced with the 182 regular expression for a float 183 @param titles: titles of the columns 184 """ 185 RegExpLineAnalyzer.__init__(self, 186 name, 187 exp, 188 idNr=None, 189 titles=titles, 190 doTimelines=True, 191 doFiles=False, 192 startTime=startTime, 193 endTime=endTime)
194 195 # Should work with Python3 and Python2 196