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 6513 2010-04-21T20:32:43.495518Z 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      "Version" : "1.5", 
 41      }, 
 42      "MPI": { 
 43  #    "run_OPENMPI":"mpirun", 
 44  #    "run_LAM":"mpirun", 
 45      "options_OPENMPI_pre": '["--mca","pls","rsh","--mca","pls_rsh_agent","rsh"]', 
 46      "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"]' 
 47      }, 
 48      "Paths": { 
 49      "python" : "/usr/bin/python", 
 50      "bash" : "/bin/bash", 
 51      }, 
 52      "ClusterJob": { 
 53      "useFoamMPI":'["1.5"]', 
 54      "path":"/opt/openmpi/bin", 
 55      "ldpath":"/opt/openmpi/lib", 
 56      }, 
 57      "Debug": { 
 58  #    "ParallelExecution":"True", 
 59      }, 
 60      "Execution":{ 
 61      "controlDictRestoreWait":"60.", 
 62      }, 
 63      "CaseBuilder":{ 
 64      "descriptionPath": eval('["'+path.curdir+'","'+path.join(userDirectory(),"caseBuilderDescriptions")+'","'+path.join(globalDirectory(),"caseBuilderDescriptions")+'"]'), 
 65      }, 
 66      "Formats":{ 
 67      "error"       : "bold,red,standout", 
 68      "warning"     : "under", 
 69      "source"      : "red,bold", 
 70      "destination" : "blue,bold", 
 71      "difference"  : "green,back_black,bold", 
 72      "question"    : "green,standout", 
 73      "input"       : "cyan,under", 
 74      }, 
 75      "CommandOptionDefaults":{ 
 76      "sortListCases":"mtime", 
 77      }, 
 78      "Plotting":{ 
 79      "preferredImplementation":"gnuplot", 
 80      }, 
 81      "OutfileCollection": { 
 82      "maximumOpenFiles":"100", 
 83      }, 
 84      "SolverOutput": { 
 85      "timeRegExp": "^(Time =|Iteration:) (.+)$", 
 86      }, 
 87      } 
 88   
89 -class Configuration(ConfigParser):
90 """Reads the settings from files (if existing). Otherwise uses hardcoded 91 defaults""" 92
93 - def __init__(self):
94 """Constructs the ConfigParser and fills it with the hardcoded defaults""" 95 ConfigParser.__init__(self) 96 97 for section,content in _defaults.iteritems(): 98 self.add_section(section) 99 for key,value in content.iteritems(): 100 self.set(section,key,value) 101 102 self.read(self.configFiles()) 103 104 self.validSections={} 105 for s in self.sections(): 106 minusPos=s.find('-') 107 if minusPos<0: 108 name=s 109 else: 110 name=s[:minusPos] 111 try: 112 self.validSections[name].append(s) 113 except KeyError: 114 self.validSections[name]=[s] 115 116 for name,sections in self.validSections.iteritems(): 117 if not name in sections: 118 print "Invalid configuration for",name,"there is no default section for it in",sections
119
120 - def bestSection(self,section,option):
121 """Get the best-fitting section that has that option""" 122 123 from PyFoam import foamVersionString 124 125 try: 126 if len(self.validSections[section])==1 or foamVersionString()=="": 127 return section 128 except KeyError: 129 return section 130 131 result=section 132 fullName=section+"-"+foamVersionString() 133 134 for s in self.validSections[section]: 135 if fullName.find(s)==0 and len(s)>len(result): 136 if self.has_option(s,option): 137 result=s 138 139 return result
140
141 - def configSearchPath(self):
142 """Defines a search path for the configuration files as a pare of type/name 143 pairs""" 144 files=[("file",globalConfigFile()), 145 ("directory",globalConfigDir()), 146 ("file",userConfigFile()), 147 ("directory",userConfigDir())] 148 return files
149
150 - def configFiles(self):
151 """Return a list with the configurationfiles that are going to be used""" 152 files=[] 153 154 for t,f in self.configSearchPath(): 155 if path.exists(f): 156 if t=="file": 157 files.append(f) 158 elif t=="directory": 159 for ff in glob.glob(path.join(f,"*.cfg")): 160 files.append(ff) 161 else: 162 error("Unknown type",t,"for the search entry",f) 163 164 return files
165
166 - def addFile(self,filename,silent=False):
167 """Add another file to the configuration (if it exists)""" 168 if not path.exists(filename): 169 if not silent: 170 print "The configuration file",filename,"is not there" 171 else: 172 self.read([filename])
173
174 - def dump(self):
175 """Dumps the contents in INI-Form 176 @return: a string with the contents""" 177 result="" 178 for section in self.sections(): 179 result+="[%s]\n" % (section) 180 for key,value in self.items(section): 181 result+="%s: %s\n" % (key,value) 182 result+="\n" 183 184 return result
185
186 - def getboolean(self,section,option,default=None):
187 """Overrides the original implementation from ConfigParser 188 @param section: the section 189 @param option: the option 190 @param default: if set and the option is not found, then this value is used""" 191 192 try: 193 return ConfigParser.getboolean(self, 194 self.bestSection(section,option), 195 option) 196 except NoOptionError: 197 if default!=None: 198 return default 199 else: 200 raise
201
202 - def getfloat(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.getfloat(self, 210 self.bestSection(section,option), 211 option) 212 except (NoOptionError,ValueError): 213 if default!=None: 214 return default 215 else: 216 raise
217
218 - def get(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.get(self, 226 self.bestSection(section,option), 227 option) 228 except NoOptionError: 229 if default!=None: 230 return default 231 else: 232 raise
233
234 - def getdebug(self,name):
235 """Gets a debug switch""" 236 237 return self.getboolean("Debug",name,default=False)
238