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