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
15
16 ruleList=[]
17
20
23
24
26 """ This class collects the lines that should be plotted
27 """
28
31
34
38
44
46 """Adds the lines from a file to the custom regular expressions
47 @param fName: The name of the file"""
48 f=open(fName)
49 txt=f.read()
50 f.close()
51 self.lines_+=readCustomPlotInfo(txt)
52
54 grp=OptionGroup(self.parser,
55 "Regular expression",
56 "Where regular expressions for custom plots are found")
57
58 grp.add_option("--custom-regexp",
59 action="append",
60 default=None,
61 dest="customRegex",
62 help="Add a custom regular expression to be plotted (can be used more than once)")
63
64 grp.add_option("--regexp-file",
65 action="append",
66 default=None,
67 dest="regexpFile",
68 help="A file with regulare expressions that are treated like the expressions given with --custom-regexp")
69
70 grp.add_option("--no-auto-customRegexp",
71 action="store_false",
72 default=True,
73 dest="autoCustom",
74 help="Do not automatically load the expressions from the file customRegexp")
75
76 grp.add_option("--dump-custom-regegexp",
77 action="store_true",
78 default=False,
79 dest="dumpCustomRegexp",
80 help="Dump the used regular expressions in a format suitable to put into a customRegexp-file and finish the program")
81 self.parser.add_option_group(grp)
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
120 """Process the options that have to do with plot-lines"""
121
122 self.addPlotLines(self.opts.customRegex)
123
124 if self.opts.regexpFile!=None:
125 for f in self.opts.regexpFile:
126 print " Reading regular expressions from",f
127 self.addFileRegexps(f)
128
129
130 if autoPath!=None and self.opts.autoCustom:
131 autoFile=path.join(autoPath,"customRegexp")
132 if path.exists(autoFile):
133 print " Reading regular expressions from",autoFile
134 self.addFileRegexps(autoFile)
135
136 for include,expr in ruleList:
137 rexp=re.compile(expr)
138 for l in self.lines_:
139 if rexp.search(l.id):
140 if include:
141 l.enabled=True
142 else:
143 l.enabled=False
144
145 if self.opts.dumpCustomRegexp:
146 print "\nDumping customRegexp:\n"
147 for l in self.lines_:
148 print l
149 return -1
150
151 if self.opts.listCustomRegexp:
152 print "\nListing the customRegexp:\n"
153 for l in self.lines_:
154 if l.enabled:
155 prefix="*"
156 else:
157 prefix=" "
158
159 print prefix,l.id
160
161 if len(ruleList)>0:
162 print "\nAccording to list of rules:"
163 for incl,expr in ruleList:
164 if incl:
165 prefix="Include"
166 else:
167 prefix="Exclude"
168 print prefix,"matching",'"%s"' % expr
169
170 return -1
171