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

Source Code for Module PyFoam.Applications.UpgradeDictionariesTo17

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