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