Package PyFoam :: Package Applications :: Module UpgradeDictionariesTo17
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.UpgradeDictionariesTo17

  1  #  ICE Revision: $Id$ 
  2  """ 
  3  Application class that implements pyFoamUpgradeDictionariesTo17 
  4  """ 
  5   
  6  import sys,re 
  7  from optparse import OptionGroup 
  8  from os import path 
  9   
 10  from .PyFoamApplication import PyFoamApplication 
 11   
 12  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
 13  from PyFoam.Basics.Utilities import copyfile 
 14  from PyFoam.Basics.DataStructures import DictProxy 
 15  from PyFoam.Basics.FoamFileGenerator import makeString 
 16  from PyFoam.Error import error 
 17   
 18  from PyFoam.ThirdParty.six import print_ 
 19   
20 -class DictionaryUpgradeInfo(object):
21 """This class knows how to detect old versions and how to upgrade them"""
22 - def __init__(self):
23 self.case=None 24 self.enabled=True 25 self.fName=None 26 self.noHeader=False 27 self.listDict=False
28
29 - def setFile(self,fName):
30 self.fName=fName
31
32 - def path(self):
33 if self.fName: 34 return self.fName 35 else: 36 return path.join(self.case,self.location())
37
38 - def disable(self):
39 self.enabled=False
40
41 - def disableCallback(self, opt, value, parser, *args, **kwargs):
42 self.disable()
43
44 - def needsUpgrade(self):
45 f=ParsedParameterFile(self.path(), 46 listDict=self.listDict, 47 noHeader=self.noHeader) 48 return self.checkUpgrade(f.content)
49
50 - def upgrade(self,force,printIt):
51 backup=self.path()+".upgradeBackup" 52 if not printIt: 53 if path.exists(backup): 54 if not force: 55 error("The backup-file",backup,"does already exist") 56 57 copyfile(self.path(),backup) 58 f=ParsedParameterFile(self.path(), 59 listDict=self.listDict, 60 noHeader=self.noHeader) 61 r=self.manipulate(f.content) 62 if r: 63 f.content=r 64 if not printIt: 65 f.writeFile() 66 else: 67 print_(f)
68
69 - def makeComment(self,data):
70 s=makeString(data) 71 s="\n old Value: "+s 72 s=s.replace("\n","\n//") 73 return s
74
75 -class FvSolutionUpgradeInfo(DictionaryUpgradeInfo):
76 - def __init__(self):
78
79 - def location(self):
80 return path.join("system","fvSolution")
81
82 - def name(self):
83 return "fvSolution17"
84
85 - def checkUpgrade(self,content):
86 if "solvers" not in content: 87 return False 88 89 for s in content["solvers"]: 90 if type(content["solvers"][s]) not in [dict,DictProxy]: 91 return True 92 return False
93
94 - def manipulate(self,content):
95 for s in content["solvers"]: 96 comment=self.makeComment(content["solvers"][s]) 97 alg,rest=content["solvers"][s] 98 rest["solver"]=alg 99 content["solvers"][s]=rest 100 content["solvers"].addDecoration(s,comment)
101
102 -class UpgradeDictionariesTo17(PyFoamApplication):
103 - def __init__(self, 104 args=None, 105 description=None):
106 if not description: 107 description="""\ 108 Examines dictionaries in a case and tries to upgrade them to a form 109 that is compatible with OpenFOAM 1.7 110 111 If only a file is specified then the mode of that file has to be specified 112 """ 113 114 self.dicts=[] 115 116 self.addDicts() 117 118 PyFoamApplication.__init__(self, 119 args=args, 120 description=description, 121 usage="%prog [options] <case>", 122 changeVersion=False, 123 nr=1, 124 interspersed=True)
125
126 - def addDicts(self):
127 self.dicts.append(FvSolutionUpgradeInfo())
128
129 - def addOptions(self):
130 behaveGroup=OptionGroup(self.parser, 131 "Behaviour", 132 "General behaviour of the program") 133 134 behaveGroup.add_option("--apply-changes", 135 action="store_true", 136 dest="applyChanges", 137 default=False, 138 help="Apply changes to the dictionaries in question. Without this option only the results of the analysis will be shown") 139 140 behaveGroup.add_option("--print", 141 action="store_true", 142 dest="print_", 143 default=False, 144 help="Only print the modified dictionaries to the screen") 145 146 behaveGroup.add_option("--verbose", 147 action="store_true", 148 dest="verbose", 149 default=False, 150 help="Speak out aloud which decisions are made") 151 152 behaveGroup.add_option("--force", 153 action="store_true", 154 dest="force", 155 default=False, 156 help="Force even if backup-files exist") 157 158 self.parser.add_option_group(behaveGroup) 159 160 self.dictGroup=OptionGroup(self.parser, 161 "Dictionaries", 162 "Dictionaries that should be updated") 163 164 for d in self.dicts: 165 self.dictGroup.add_option("--disable-"+"-".join(reversed(d.location().split(path.sep))), 166 action="callback", 167 callback=d.disableCallback, 168 help="Disable the modification of "+d.location()) 169 170 self.parser.add_option_group(self.dictGroup) 171 172 self.modes={} 173 174 for d in self.dicts: 175 self.modes[d.name()]=d 176 177 choices=self.modes.keys() 178 choices.sort() 179 180 behaveGroup.add_option("--file-mode", 181 action="store", 182 type="choice", 183 dest="fileMode", 184 default=None, 185 choices=choices, 186 help="The file should be treated as what while upgrading. Possible modes: "+", ".join(choices))
187
188 - def run(self):
189 fName=self.parser.getArgs()[0] 190 if path.isdir(fName): 191 if self.opts.fileMode: 192 self.error("Filemode",self.opts.fileMode,"specified. But", 193 fName,"is a directory") 194 case=fName 195 self.checkCase(case) 196 197 if self.opts.verbose: 198 print_("Working on case",case) 199 200 for d in self.dicts: 201 d.case=case 202 if self.opts.verbose: 203 print_(" Checking",d.location()) 204 205 if not d.enabled: 206 if self.opts.verbose: 207 print_(" Disabled") 208 continue 209 210 if not path.exists(d.path()): 211 d.disable() 212 if self.opts.verbose: 213 print_(" Does not exist - disabling") 214 continue 215 216 if not d.needsUpgrade(): 217 d.disable() 218 if self.opts.verbose: 219 print_(" Does not need an upgrade - disabling") 220 continue 221 222 print_(d.location(),"needs an upgrade") 223 224 if self.opts.applyChanges or self.opts.print_: 225 print_() 226 if self.opts.applyChanges: 227 print_("Doing the upgrades") 228 for d in self.dicts: 229 if d.enabled: 230 if self.opts.verbose: 231 print_("Upgrading",d.location()) 232 d.upgrade(self.opts.force,self.opts.print_) 233 else: 234 if not self.opts.fileMode: 235 self.error(fName,"is a file, but no --file-mode specified") 236 d=self.modes[self.opts.fileMode] 237 d.setFile(fName) 238 if self.opts.verbose: 239 print_("Handling",fName,"as",self.opts.fileMode) 240 if not d.needsUpgrade(): 241 d.disable() 242 if self.opts.verbose: 243 print_(" Does not need an upgrade") 244 else: 245 if self.opts.verbose: 246 print_(" Needs an upgrade") 247 if self.opts.applyChanges or self.opts.print_: 248 print_() 249 if self.opts.verbose: 250 print_("Upgrading",fName) 251 d.upgrade(self.opts.force,self.opts.print_)
252 253 # Should work with Python3 and Python2 254