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
17
20
21
23 """ This class collects the lines that should be plotted
24 """
25
28
31
35
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
53
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
120 """Process the options that have to do with plot-lines"""
121
122
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
176