Package PyFoam :: Package Applications :: Module CommonPlotLines
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.CommonPlotLines

  1   
  2  """ 
  3  Class that implements common functionality for collecting plot-lines 
  4  """ 
  5   
  6  import sys 
  7  import re 
  8  from os import path 
  9  from optparse import OptionGroup 
 10   
 11  from PyFoam.Error import error,warning 
 12  from PyFoam.LogAnalysis.RegExpLineAnalyzer import RegExpLineAnalyzer 
 13   
 14  from PyFoam.Basics.CustomPlotInfo import readCustomPlotInfo,resetCustomCounter 
 15   
 16  ruleList=[] 
 17   
18 -def addRegexpInclude(option,opt,value,parser,*args,**kwargs):
19 ruleList.append((True,value))
20
21 -def addRegexpExclude(option,opt,value,parser,*args,**kwargs):
22 ruleList.append((False,value))
23 24
25 -class CommonPlotLines(object):
26 """ This class collects the lines that should be plotted 27 """ 28
29 - def __init__(self):
30 self.lines_=[]
31
32 - def plotLines(self):
33 return self.lines_
34
35 - def addPlotLine(self,line):
36 """Add a single line""" 37 self.lines_+=readCustomPlotInfo(line)
38
39 - def addPlotLines(self,lines,name=None):
40 """Adds a list of lines""" 41 if lines: 42 for i,l in enumerate(lines): 43 if name: 44 useName="%s_%i" % (name,i) 45 else: 46 useName=None 47 self.lines_+=readCustomPlotInfo(l,useName=useName)
48
49 - def addFileRegexps(self,fName):
50 """Adds the lines from a file to the custom regular expressions 51 @param fName: The name of the file""" 52 f=open(fName) 53 txt=f.read() 54 f.close() 55 self.lines_+=readCustomPlotInfo(txt)
56
57 - def addOptions(self):
58 grp=OptionGroup(self.parser, 59 "Regular expression", 60 "Where regular expressions for custom plots are found") 61 62 grp.add_option("--custom-regexp", 63 action="append", 64 default=None, 65 dest="customRegex", 66 help="Add a custom regular expression to be plotted (can be used more than once)") 67 68 grp.add_option("--regexp-file", 69 action="append", 70 default=None, 71 dest="regexpFile", 72 help="A file with regulare expressions that are treated like the expressions given with --custom-regexp") 73 74 grp.add_option("--no-auto-customRegexp", 75 action="store_false", 76 default=True, 77 dest="autoCustom", 78 help="Do not automatically load the expressions from the file customRegexp") 79 80 grp.add_option("--dump-custom-regegexp", 81 action="store_true", 82 default=False, 83 dest="dumpCustomRegexp", 84 help="Dump the used regular expressions in a format suitable to put into a customRegexp-file and finish the program") 85 self.parser.add_option_group(grp) 86 87 grp.add_option("--list-custom-Regexp", 88 action="store_true", 89 default=False, 90 dest="listCustomRegexp", 91 help="List the customRegexp by name. A * before the name means that it is enabled") 92 93 grp.add_option("--include-regexp-fitting", 94 action="callback", 95 callback=addRegexpInclude, 96 type="string", 97 help="Add all the customRegex whose name fits this regular expression. This option can be used as often as liked ") 98 99 grp.add_option("--exclude-regexp-fitting", 100 action="callback", 101 callback=addRegexpExclude, 102 type="string", 103 help="Remove all the customRegex whose name fits this regular expression. This option can be used as often as liked ") 104 105 self.parser.add_option_group(grp) 106 107 grp2=OptionGroup(self.parser, 108 "Data files", 109 "How data files are written") 110 grp2.add_option("--single-data-files-only", 111 action="store_true", 112 default=False, 113 dest="singleDataFilesOnly", 114 help="Don't create consecutive data files 'value', 'value_2', 'value_3' etc but put all the data into a single file") 115 grp2.add_option("--write-files", 116 action="store_true", 117 default=False, 118 dest="writeFiles", 119 help="Writes the parsed data to files") 120 121 self.parser.add_option_group(grp2)
122
123 - def processPlotLineOptions(self,autoPath=None):
124 """Process the options that have to do with plot-lines""" 125 126 # make sure that every object starts with a new batch 127 resetCustomCounter() 128 129 self.addPlotLines(self.opts.customRegex) 130 131 if self.opts.regexpFile!=None: 132 for f in self.opts.regexpFile: 133 print " Reading regular expressions from",f 134 self.addFileRegexps(f) 135 136 137 if autoPath!=None and self.opts.autoCustom: 138 autoFile=path.join(autoPath,"customRegexp") 139 if path.exists(autoFile): 140 print " Reading regular expressions from",autoFile 141 self.addFileRegexps(autoFile) 142 143 for include,expr in ruleList: 144 rexp=re.compile(expr) 145 for l in self.lines_: 146 if rexp.search(l.id): 147 if include: 148 l.enabled=True 149 else: 150 l.enabled=False 151 152 if self.opts.dumpCustomRegexp: 153 print "\nDumping customRegexp:\n" 154 for l in self.lines_: 155 print l 156 return -1 157 158 if self.opts.listCustomRegexp: 159 print "\nListing the customRegexp:\n" 160 for l in self.lines_: 161 if l.enabled: 162 prefix="*" 163 else: 164 prefix=" " 165 166 print prefix,l.id 167 168 if len(ruleList)>0: 169 print "\nAccording to list of rules:" 170 for incl,expr in ruleList: 171 if incl: 172 prefix="Include" 173 else: 174 prefix="Exclude" 175 print prefix,"matching",'"%s"' % expr 176 177 return -1
178