Package PyFoam :: Package Infrastructure :: Module Configuration
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Infrastructure.Configuration

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Infrastructure/Configuration.py 7345 2011-03-09T21:17:59.267549Z bgschaid  $  
  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 
 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  #    "run_OPENMPI":"mpirun", 
 45  #    "run_LAM":"mpirun", 
 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  #    "ParallelExecution":"True", 
 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      } 
 91   
92 -class Configuration(ConfigParser):
93 """Reads the settings from files (if existing). Otherwise uses hardcoded 94 defaults""" 95
96 - def __init__(self):
97 """Constructs the ConfigParser and fills it with the hardcoded defaults""" 98 ConfigParser.__init__(self) 99 100 for section,content in _defaults.iteritems(): 101 self.add_section(section) 102 for key,value in content.iteritems(): 103 self.set(section,key,value) 104 105 self.read(self.configFiles()) 106 107 self.validSections={} 108 for s in self.sections(): 109 minusPos=s.find('-') 110 if minusPos<0: 111 name=s 112 else: 113 name=s[:minusPos] 114 try: 115 self.validSections[name].append(s) 116 except KeyError: 117 self.validSections[name]=[s] 118 119 for name,sections in self.validSections.iteritems(): 120 if not name in sections: 121 print "Invalid configuration for",name,"there is no default section for it in",sections
122
123 - def bestSection(self,section,option):
124 """Get the best-fitting section that has that option""" 125 126 from PyFoam import foamVersionString 127 128 try: 129 if len(self.validSections[section])==1 or foamVersionString()=="": 130 return section 131 except KeyError: 132 return section 133 134 result=section 135 fullName=section+"-"+foamVersionString() 136 137 for s in self.validSections[section]: 138 if fullName.find(s)==0 and len(s)>len(result): 139 if self.has_option(s,option): 140 result=s 141 142 return result
143
144 - def configSearchPath(self):
145 """Defines a search path for the configuration files as a pare of type/name 146 pairs""" 147 files=[("file",globalConfigFile()), 148 ("directory",globalConfigDir()), 149 ("file",userConfigFile()), 150 ("directory",userConfigDir())] 151 return files
152
153 - def configFiles(self):
154 """Return a list with the configurationfiles that are going to be used""" 155 files=[] 156 157 for t,f in self.configSearchPath(): 158 if path.exists(f): 159 if t=="file": 160 files.append(f) 161 elif t=="directory": 162 for ff in glob.glob(path.join(f,"*.cfg")): 163 files.append(ff) 164 else: 165 error("Unknown type",t,"for the search entry",f) 166 167 return files
168
169 - def addFile(self,filename,silent=False):
170 """Add another file to the configuration (if it exists)""" 171 if not path.exists(filename): 172 if not silent: 173 print "The configuration file",filename,"is not there" 174 else: 175 self.read([filename])
176
177 - def dump(self):
178 """Dumps the contents in INI-Form 179 @return: a string with the contents""" 180 result="" 181 for section in self.sections(): 182 result+="[%s]\n" % (section) 183 for key,value in self.items(section): 184 result+="%s: %s\n" % (key,value) 185 result+="\n" 186 187 return result
188
189 - def getList(self,section,option,default="",splitchar=","):
190 """Get a list of strings (in the original they are separated by commas) 191 @param section: the section 192 @param option: the option 193 @param default: if set and the option is not found, then this value is used 194 @param splitchar: the character by which the values are separated""" 195 196 val=self.get(section,option,default=default) 197 if val=="": 198 return [] 199 else: 200 return val.split(splitchar)
201
202 - def getboolean(self,section,option,default=None):
203 """Overrides the original implementation from ConfigParser 204 @param section: the section 205 @param option: the option 206 @param default: if set and the option is not found, then this value is used""" 207 208 try: 209 return ConfigParser.getboolean(self, 210 self.bestSection(section,option), 211 option) 212 except NoOptionError: 213 if default!=None: 214 return default 215 else: 216 raise
217
218 - def getfloat(self,section,option,default=None):
219 """Overrides the original implementation from ConfigParser 220 @param section: the section 221 @param option: the option 222 @param default: if set and the option is not found, then this value is used""" 223 224 try: 225 return ConfigParser.getfloat(self, 226 self.bestSection(section,option), 227 option) 228 except (NoOptionError,ValueError): 229 if default!=None: 230 return default 231 else: 232 raise
233
234 - def get(self,section,option,default=None):
235 """Overrides the original implementation from ConfigParser 236 @param section: the section 237 @param option: the option 238 @param default: if set and the option is not found, then this value is used""" 239 240 try: 241 return ConfigParser.get(self, 242 self.bestSection(section,option), 243 option) 244 except NoOptionError: 245 if default!=None: 246 return default 247 else: 248 raise
249
250 - def getdebug(self,name):
251 """Gets a debug switch""" 252 253 return self.getboolean("Debug",name,default=False)
254