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

Source Code for Module PyFoam.Applications.UpdateDictionary

  1  #  ICE Revision: $Id: ReadDictionary.py 7581 2007-06-27 15:29:14Z bgschaid $  
  2  """ 
  3  Application class that implements pyFoamUpdateDictionary.py 
  4  """ 
  5   
  6  import re 
  7  from os import path 
  8   
  9  from PyFoamApplication import PyFoamApplication 
 10   
 11  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
 12  from PyFoam.Basics.DataStructures import DictProxy 
 13   
 14  from PyFoam.Error import error,warning 
 15   
16 -class UpdateDictionary(PyFoamApplication):
17 - def __init__(self,args=None):
18 description=""" 19 Takes two dictionary and modifies the second one after the example of 20 the first. If the dictionaries do not have the same name, it looks for 21 the destination file by searching the equivalent place in the destination 22 case 23 """ 24 25 PyFoamApplication.__init__(self,args=args,description=description,usage="%prog [options] <source> <destination-case>",nr=2,interspersed=True)
26
27 - def addOptions(self):
28 self.parser.add_option("--clear-unused", 29 action="store_true", 30 default=False, 31 dest="clear", 32 help="Removes all the dictionary entries that are not in the source") 33 34 self.parser.add_option("--add-missing", 35 action="store_true", 36 default=False, 37 dest="add", 38 help="Add all the dictionary entries that are not in the destination") 39 40 self.parser.add_option("--test", 41 action="store_true", 42 default=False, 43 dest="test", 44 help="Does not write the file but only prints it to the screen") 45 46 self.parser.add_option("--not-equal", 47 action="store_true", 48 default=False, 49 dest="notequal", 50 help="Allow source and destination to have different names") 51 52 self.parser.add_option("--verbose", 53 action="store_true", 54 default=False, 55 dest="verbose", 56 help="Print every change that is being made") 57 58 self.parser.add_option("--min-recursion", 59 action="store", 60 default=0, 61 type="int", 62 dest="min", 63 help="Minimum depth of the recursive decent into dictionaries at which 'editing' should start (default: %default)") 64 65 self.parser.add_option("--max-recursion", 66 action="store", 67 default=1, 68 type="int", 69 dest="max", 70 help="Maximum depth of the recursive decent into dictionaries (default: %default)")
71
72 - def workList(self,source,dest,depth):
73 if depth>self.opts.max: 74 if self.opts.verbose: 75 print "- "*depth,"Recursion ended" 76 return 77 78 for i in range(min(len(source),len(dest))): 79 if type(source[i])==type(dest[i]): 80 if type(source[i]) in [dict,DictProxy]: 81 if self.opts.verbose: 82 print "- "*depth,"Entering dict nr.",i 83 self.workDict(source[i],dest[i],depth+1) 84 elif type(source[i])==tuple: 85 if self.opts.verbose: 86 print "- "*depth,"Entering tuple nr.",i 87 self.workList(source[i],dest[i],depth+1)
88
89 - def workDict(self,source,dest,depth):
90 if depth>self.opts.max: 91 if self.opts.verbose: 92 print "- "*depth,"Recursion ended" 93 return 94 95 if depth>=self.opts.min: 96 doIt=True 97 else: 98 doIt=False 99 100 for name in source: 101 if name not in dest: 102 if self.opts.add and doIt: 103 if self.opts.verbose: 104 print "- "*depth,"Adding",name 105 dest[name]=source[name] 106 elif type(source[name]) in [dict,DictProxy]: 107 if type(dest[name]) not in [dict,DictProxy]: 108 error("Entry",name,"is not a dictionary in destination (but in source)") 109 if self.opts.verbose: 110 print "- "*depth,"Entering dict ",name 111 self.workDict(source[name],dest[name],depth+1) 112 elif type(source[name])==type(dest[name]) and type(dest[name])==tuple: 113 if self.opts.verbose: 114 print "- "*depth,"Entering tuple ",name 115 self.workList(source[name],dest[name],depth+1) 116 117 if self.opts.clear and doIt: 118 weg=[] 119 for name in dest: 120 if name not in source: 121 weg.append(name) 122 123 for name in weg: 124 if self.opts.verbose: 125 print "- "*depth,"Removing",name 126 del dest[name]
127
128 - def run(self):
129 sName=path.abspath(self.parser.getArgs()[0]) 130 dName=path.abspath(self.parser.getArgs()[1]) 131 132 try: 133 source=ParsedParameterFile(sName,backup=False) 134 except IOError,e: 135 self.error("Problem with file",sName,":",e) 136 137 if not self.opts.notequal and path.basename(sName)!=path.basename(dName): 138 found=False 139 parts=sName.split(path.sep) 140 for i in range(len(parts)): 141 tmp=apply(path.join,[dName]+parts[-(i+1):]) 142 143 if path.exists(tmp): 144 found=True 145 dName=tmp 146 warning("Found",dName,"and using this") 147 break 148 149 if not found: 150 error("Could not find a file named",path.basename(sName),"in",dName) 151 152 if path.samefile(sName,dName): 153 error("Source",sName,"and destination",dName,"are the same") 154 155 try: 156 dest=ParsedParameterFile(dName,backup=False) 157 except IOError,e: 158 self.error("Problem with file",dName,":",e) 159 160 self.workDict(source.content,dest.content,1) 161 162 if self.opts.test: 163 print str(dest) 164 else: 165 dest.writeFile()
166