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

Source Code for Module PyFoam.Applications.UpgradeDictionariesTo17

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Applications/UpgradeDictionariesTo17.py 7660 2012-01-07T16:44:40.128256Z bgschaid  $  
  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 -class DictionaryUpgradeInfo(object):
19 """This class knows how to detect old versions and how to upgrade them"""
20 - def __init__(self):
21 self.case=None 22 self.enabled=True
23
24 - def path(self):
25 return path.join(self.case,self.location())
26
27 - def disable(self):
28 self.enabled=False
29
30 - def disableCallback(self, opt, value, parser, *args, **kwargs):
31 self.disable()
32
33 - def needsUpgrade(self):
34 f=ParsedParameterFile(self.path()) 35 return self.checkUpgrade(f.content)
36
37 - def upgrade(self,force,print_):
38 backup=self.path()+".upgradeBackup" 39 if not print_: 40 if path.exists(backup): 41 if not force: 42 error("The backup-file",backup,"does already exist") 43 44 copyfile(self.path(),backup) 45 f=ParsedParameterFile(self.path()) 46 self.manipulate(f.content) 47 if not print_: 48 f.writeFile() 49 else: 50 print f
51
52 - def makeComment(self,data):
53 s=makeString(data) 54 s="\n old Value: "+s 55 s=s.replace("\n","\n//") 56 return s
57
58 -class FvSolutionUpgradeInfo(DictionaryUpgradeInfo):
59 - def __init__(self):
61
62 - def location(self):
63 return path.join("system","fvSolution")
64
65 - def checkUpgrade(self,content):
66 if "solvers" not in content: 67 return False 68 69 for s in content["solvers"]: 70 if type(content["solvers"][s]) not in [dict,DictProxy]: 71 return True 72 return False
73
74 - def manipulate(self,content):
75 for s in content["solvers"]: 76 comment=self.makeComment(content["solvers"][s]) 77 alg,rest=content["solvers"][s] 78 rest["solver"]=alg 79 content["solvers"][s]=rest 80 content["solvers"].addDecoration(s,comment)
81
82 -class UpgradeDictionariesTo17(PyFoamApplication):
83 - def __init__(self, 84 args=None, 85 description=None):
86 if not description: 87 description="""\ 88 Examines dictionaries in a case and tries to upgrade them to a form 89 that is compatible with OpenFOAM 1.7 90 """ 91 92 self.dicts=[] 93 94 self.addDicts() 95 96 PyFoamApplication.__init__(self, 97 args=args, 98 description=description, 99 usage="%prog [options] <case>", 100 changeVersion=False, 101 nr=1, 102 interspersed=True)
103
104 - def addDicts(self):
105 self.dicts.append(FvSolutionUpgradeInfo())
106
107 - def addOptions(self):
108 behaveGroup=OptionGroup(self.parser, 109 "Behaviour", 110 "General behaviour of the program") 111 112 behaveGroup.add_option("--apply-changes", 113 action="store_true", 114 dest="applyChanges", 115 default=False, 116 help="Apply changes to the dictionaries in question. Without this option only the results of the analysis will be shown") 117 118 behaveGroup.add_option("--print", 119 action="store_true", 120 dest="print_", 121 default=False, 122 help="Only print the modified dictionaries to the screen") 123 124 behaveGroup.add_option("--verbose", 125 action="store_true", 126 dest="verbose", 127 default=False, 128 help="Speak out aloud which decisions are made") 129 130 behaveGroup.add_option("--force", 131 action="store_true", 132 dest="force", 133 default=False, 134 help="Force even if backup-files exist") 135 136 self.parser.add_option_group(behaveGroup) 137 138 self.dictGroup=OptionGroup(self.parser, 139 "Dictionaries", 140 "Dictionaries that should be updated") 141 142 for d in self.dicts: 143 self.dictGroup.add_option("--disable-"+"-".join(reversed(d.location().split(path.sep))), 144 action="callback", 145 callback=d.disableCallback, 146 help="Disable the modification of "+d.location()) 147 148 self.parser.add_option_group(self.dictGroup)
149
150 - def run(self):
151 case=self.parser.getArgs()[0] 152 self.checkCase(case) 153 154 if self.opts.verbose: 155 print "Working on case",case 156 157 for d in self.dicts: 158 d.case=case 159 if self.opts.verbose: 160 print " Checking",d.location() 161 162 if not d.enabled: 163 if self.opts.verbose: 164 print " Disabled" 165 continue 166 167 if not path.exists(d.path()): 168 d.disable() 169 if self.opts.verbose: 170 print " Does not exist - disabling" 171 continue 172 173 if not d.needsUpgrade(): 174 d.disable() 175 if self.opts.verbose: 176 print " Does not need an upgrade - disbling" 177 continue 178 179 print d.location(),"needs an upgrade" 180 181 if self.opts.applyChanges or self.opts.print_: 182 print 183 if self.opts.applyChanges: 184 print "Doing the upgrades" 185 for d in self.dicts: 186 if d.enabled: 187 if self.opts.verbose: 188 print "Upgrading",d.location() 189 d.upgrade(self.opts.force,self.opts.print_)
190