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

Source Code for Module PyFoam.Applications.CompareDictionary

  1  #  ICE Revision: $Id: ReadDictionary.py 7581 2007-06-27 15:29:14Z bgschaid $  
  2  """ 
  3  Application class that implements pyFoamCompareDictionary.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,Dimension,Tensor,SymmTensor,Vector,Field 
 13  from PyFoam.Basics.FoamFileGenerator import makeString 
 14   
 15  from PyFoam.Error import error,warning 
 16   
17 -class CompareDictionary(PyFoamApplication):
18 - def __init__(self,args=None):
19 description=""" 20 Takes two dictionary and compares them semantically (by looking at the 21 structure, not the textual representation. If the dictionaries do not 22 have the same name, it looks for the destination file by searching the 23 equivalent place in the destination case 24 """ 25 26 PyFoamApplication.__init__(self,args=args,description=description,usage="%prog [options] <source> <destination-case>",nr=2,interspersed=True)
27
28 - def addOptions(self):
29 self.parser.add_option("--not-equal", 30 action="store_true", 31 default=False, 32 dest="notequal", 33 help="Allow source and destination to have different names") 34 self.parser.add_option("--debug", 35 action="store_true", 36 default=False, 37 dest="debug", 38 help="Debug the comparing process")
39 40 41
42 - def run(self):
43 sName=path.abspath(self.parser.getArgs()[0]) 44 dName=path.abspath(self.parser.getArgs()[1]) 45 46 try: 47 source=ParsedParameterFile(sName,backup=False) 48 except IOError,e: 49 self.error("Problem with file",sName,":",e) 50 51 if not self.opts.notequal and path.basename(sName)!=path.basename(dName): 52 found=False 53 parts=sName.split(path.sep) 54 for i in range(len(parts)): 55 tmp=apply(path.join,[dName]+parts[-(i+1):]) 56 57 if path.exists(tmp): 58 found=True 59 dName=tmp 60 warning("Found",dName,"and using this") 61 break 62 63 if not found: 64 error("Could not find a file named",path.basename(sName),"in",dName) 65 66 if path.samefile(sName,dName): 67 error("Source",sName,"and destination",dName,"are the same") 68 69 try: 70 dest=ParsedParameterFile(dName,backup=False) 71 except IOError,e: 72 self.error("Problem with file",dName,":",e) 73 74 self.pling=False 75 76 self.compareDict(source.content,dest.content,1,path.basename(sName)) 77 78 if not self.pling: 79 print "\nNo differences found"
80
81 - def dictString(self,path,name):
82 return "%s[%s]" % (path,name)
83
84 - def iterString(self,path,index):
85 return "%s[%d]" % (path,index)
86
87 - def compare(self,src,dst,depth,name):
88 if type(src)!=type(dst): 89 print ">><<",name,": Types differ\n>>Source:\n",makeString(src),"\n<<Destination:\n",makeString(dst) 90 self.pling=True 91 elif type(src) in [tuple,list]: 92 self.compareIterable(src,dst,depth,name) 93 elif type(src) in [str,float,int,long,type(None)]: 94 self.comparePrimitive(src,dst,depth,name) 95 elif src.__class__ in [Dimension,Tensor,SymmTensor,Vector]: 96 self.comparePrimitive(src,dst,depth,name) 97 elif src.__class__==Field: 98 self.compareField(src,dst,depth,name) 99 elif type(src) in [DictProxy,dict]: 100 self.compareDict(src,dst,depth,name) 101 else: 102 warning("Type of",name,"=",type(src),"unknown") 103 if self.opts.debug: 104 try: 105 print "Class of",name,"=",src.__class__,"unknown" 106 except: 107 pass
108
109 - def compareField(self,src,dst,depth,name):
110 if src!=dst: 111 self.pling=True 112 print ">><< Field",name,": Differs\n>>Source:\n", 113 if src.uniform: 114 print src 115 else: 116 print "nonuniform - field not printed" 117 print "<<Destination:\n", 118 if dst.uniform: 119 print dst 120 else: 121 print "nonuniform - field not printed"
122
123 - def comparePrimitive(self,src,dst,depth,name):
124 if src!=dst: 125 print ">><<",name,": Differs\n>>Source:\n",src,"\n<<Destination:\n",dst 126 self.pling=True
127
128 - def compareIterable(self,src,dst,depth,name):
129 nr=min(len(src),len(dst)) 130 131 for i in range(nr): 132 if self.opts.debug: 133 print "Comparing",self.iterString(name,i) 134 self.compare(src[i],dst[i],depth+1,self.iterString(name,i)) 135 136 if nr<len(src): 137 print ">>>>",self.iterString(name,nr),"to",self.iterString(name,len(src)-1),"missing from destination\n",makeString(src[nr:]) 138 self.pling=True 139 elif nr<len(dst): 140 print "<<<<",self.iterString(name,nr),"to",self.iterString(name,len(dst)-1),"missing from source\n",makeString(dst[nr:]) 141 self.pling=True
142
143 - def compareDict(self,src,dst,depth,name):
144 for n in src: 145 if not n in dst: 146 print ">>>>",self.dictString(name,n),": Missing from destination\n",makeString(src[n]) 147 self.pling=True 148 else: 149 if self.opts.debug: 150 print "Comparing",self.dictString(name,n) 151 self.compare(src[n],dst[n],depth+1,self.dictString(name,n)) 152 153 for n in dst: 154 if not n in src: 155 print "<<<<",self.dictString(name,n),": Missing from source\n",makeString(dst[n]) 156 self.pling=True
157