1
2 """Reads configuration-files that define defaults for various PyFoam-Settings
3
4 Also hardcodes defaults for the settings"""
5
6 from ConfigParser import ConfigParser,NoOptionError
7
8 from Hardcoded import globalConfigFile,userConfigFile,globalDirectory,userDirectory,globalConfigDir,userConfigDir
9
10 from os import path
11 import glob,re
12
13 _defaults={
14 "Network": {
15 "startServerPort" : "18000",
16 "nrServerPorts" : "100",
17 "portWait" : "1.",
18 "socketTimeout" : "1.",
19 "socketRetries" : "10",
20 },
21 "Metaserver": {
22 "port" : "17999",
23 "ip" : "192.168.1.11",
24 "checkerSleeping" : "30.",
25 "searchServers" : "192.168.1.0/24,192.168.0.0/24",
26 "webhost" : "127.0.0.1:9000",
27 "doWebsync" : "True",
28 "websyncInterval" : "300.",
29 },
30 "IsAlive": {
31 "maxTimeStart" : "30.",
32 "isLivingMargin" : "1.1"
33 },
34 "Logging": {
35 "default" : "INFO",
36 "server" : "INFO",
37 },
38 "OpenFOAM": {
39 "Installation" : "~/OpenFOAM",
40 "AdditionalInstallation" : "~/OpenFOAM",
41 "Version" : "1.5",
42 },
43 "MPI": {
44
45
46 "OpenMPI_add_prefix":"False",
47 "options_OPENMPI_pre": '["--mca","pls","rsh","--mca","pls_rsh_agent","rsh"]',
48 "options_OPENMPI_post":'["-x","PATH","-x","LD_LIBRARY_PATH","-x","WM_PROJECT_DIR","-x","PYTHONPATH","-x","FOAM_MPI_LIBBIN","-x","MPI_BUFFER_SIZE","-x","MPI_ARCH_PATH"]'
49 },
50 "Paths": {
51 "python" : "/usr/bin/python",
52 "bash" : "/bin/bash",
53 },
54 "ClusterJob": {
55 "useFoamMPI":'["1.5"]',
56 "path":"/opt/openmpi/bin",
57 "ldpath":"/opt/openmpi/lib",
58 "doAutoReconstruct":"True",
59 },
60 "Debug": {
61
62 },
63 "Execution":{
64 "controlDictRestoreWait":"60.",
65 },
66 "CaseBuilder":{
67 "descriptionPath": eval('["'+path.curdir+'","'+path.join(userDirectory(),"caseBuilderDescriptions")+'","'+path.join(globalDirectory(),"caseBuilderDescriptions")+'"]'),
68 },
69 "Formats":{
70 "error" : "bold,red,standout",
71 "warning" : "under",
72 "source" : "red,bold",
73 "destination" : "blue,bold",
74 "difference" : "green,back_black,bold",
75 "question" : "green,standout",
76 "input" : "cyan,under",
77 },
78 "CommandOptionDefaults":{
79 "sortListCases":"mtime",
80 },
81 "Plotting":{
82 "preferredImplementation":"gnuplot",
83 },
84 "OutfileCollection": {
85 "maximumOpenFiles":"100",
86 },
87 "SolverOutput": {
88 "timeRegExp": "^(Time =|Iteration:) (.+)$",
89 },
90 "Clearing": {
91 "additionalPatterns":"[]",
92 }
93 }
94
96 """Reads the settings from files (if existing). Otherwise uses hardcoded
97 defaults"""
98
100 """Constructs the ConfigParser and fills it with the hardcoded defaults"""
101 ConfigParser.__init__(self)
102
103 for section,content in _defaults.iteritems():
104 self.add_section(section)
105 for key,value in content.iteritems():
106 self.set(section,key,value)
107
108 self.read(self.configFiles())
109
110 self.validSections={}
111 for s in self.sections():
112 minusPos=s.find('-')
113 if minusPos<0:
114 name=s
115 else:
116 name=s[:minusPos]
117 try:
118 self.validSections[name].append(s)
119 except KeyError:
120 self.validSections[name]=[s]
121
122 for name,sections in self.validSections.iteritems():
123 if not name in sections:
124 print "Invalid configuration for",name,"there is no default section for it in",sections
125
127 """Get the best-fitting section that has that option"""
128
129 from PyFoam import foamVersionString
130
131 try:
132 if len(self.validSections[section])==1 or foamVersionString()=="":
133 return section
134 except KeyError:
135 return section
136
137 result=section
138 fullName=section+"-"+foamVersionString()
139
140 for s in self.validSections[section]:
141 if fullName.find(s)==0 and len(s)>len(result):
142 if self.has_option(s,option):
143 result=s
144
145 return result
146
155
157 """Return a list with the configurationfiles that are going to be used"""
158 files=[]
159
160 for t,f in self.configSearchPath():
161 if path.exists(f):
162 if t=="file":
163 files.append(f)
164 elif t=="directory":
165 for ff in glob.glob(path.join(f,"*.cfg")):
166 files.append(ff)
167 else:
168 error("Unknown type",t,"for the search entry",f)
169
170 return files
171
172 - def addFile(self,filename,silent=False):
179
181 """Dumps the contents in INI-Form
182 @return: a string with the contents"""
183 result=""
184 for section in self.sections():
185 result+="[%s]\n" % (section)
186 for key,value in self.items(section):
187 result+="%s: %s\n" % (key,value)
188 result+="\n"
189
190 return result
191
192 - def getList(self,section,option,default="",splitchar=","):
193 """Get a list of strings (in the original they are separated by commas)
194 @param section: the section
195 @param option: the option
196 @param default: if set and the option is not found, then this value is used
197 @param splitchar: the character by which the values are separated"""
198
199 val=self.get(section,option,default=default)
200 if val=="":
201 return []
202 else:
203 return val.split(splitchar)
204
205 - def getboolean(self,section,option,default=None):
206 """Overrides the original implementation from ConfigParser
207 @param section: the section
208 @param option: the option
209 @param default: if set and the option is not found, then this value is used"""
210
211 try:
212 return ConfigParser.getboolean(self,
213 self.bestSection(section,option),
214 option)
215 except NoOptionError:
216 if default!=None:
217 return default
218 else:
219 raise
220
221 - def getfloat(self,section,option,default=None):
222 """Overrides the original implementation from ConfigParser
223 @param section: the section
224 @param option: the option
225 @param default: if set and the option is not found, then this value is used"""
226
227 try:
228 return ConfigParser.getfloat(self,
229 self.bestSection(section,option),
230 option)
231 except (NoOptionError,ValueError):
232 if default!=None:
233 return default
234 else:
235 raise
236
238 """Get an entry and interpret it as a regular expression. Subsitute
239 the usual regular expression value for floating point numbers
240 @param section: the section
241 @param option: the option
242 @param default: if set and the option is not found, then this value is used"""
243 floatRegExp="[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?"
244
245 return re.compile(self.get(section,option).replace("%f%",floatRegExp))
246
247 - def get(self,section,option,default=None):
248 """Overrides the original implementation from ConfigParser
249 @param section: the section
250 @param option: the option
251 @param default: if set and the option is not found, then this value is used"""
252
253 try:
254 return ConfigParser.get(self,
255 self.bestSection(section,option),
256 option)
257 except NoOptionError:
258 if default!=None:
259 return default
260 else:
261 raise
262
264 """Gets a debug switch"""
265
266 return self.getboolean("Debug",name,default=False)
267