Package PyFoam :: Package Basics :: Module FoamFileGenerator
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.FoamFileGenerator

  1  #  ICE Revision: $Id: FoamFileGenerator.py 9464 2008-09-29 08:07:17Z bgschaid $  
  2  """Transform a Python data-structure into a OpenFOAM-File-Representation""" 
  3   
  4  from PyFoam.Error import error,PyFoamException 
  5  from PyFoam.Basics.DataStructures import Vector,Field,Dimension,TupleProxy,DictProxy,Tensor,SymmTensor,Unparsed,UnparsedList 
  6   
  7  import string 
  8   
9 -class FoamFileGenerator(object):
10 """Class that generates a OpenFOAM-compatible representation of a 11 data-structure""" 12 13 primitiveTypes=[SymmTensor,Tensor,Vector,Dimension,Field,Unparsed] 14
15 - def __init__(self,data,header=None):
16 """@param data: data structure that will be turned into a 17 Foam-compatible file 18 @param header: header information that is to be prepended 19 """ 20 21 self.data=data 22 self.header=header
23
24 - def __str__(self):
25 """turns the data into a string""" 26 result="" 27 if self.header: 28 result+="FoamFile\n{\n"+self.strDict(self.header,indent=1)+"}\n\n" 29 30 if type(self.data) in [dict,DictProxy]: 31 result+=self.strDict(self.data) 32 elif type(self.data) in [tuple,TupleProxy]: 33 result+=self.strTuple(self.data) 34 elif type(self.data) in [list,UnparsedList]: 35 result+=self.strList(self.data) 36 elif self.data==None: 37 raise FoamFileGeneratorError("<None> found") 38 else: 39 result+=self.strPrimitive(self.data) 40 41 return result
42
43 - def strPrimitive(self,pri):
44 if type(pri) in [int,float,long,str,unicode]: 45 return str(pri) 46 elif type(pri)==bool: 47 if pri: 48 return "yes" 49 else: 50 return "no" 51 elif pri.__class__ in self.primitiveTypes: 52 return str(pri) 53 else: 54 error("List, Dict or valid primitve expected,",type(pri),"found in",pri)
55
56 - def strDict(self,dic,indent=0):
57 s="" 58 if type(dic)==DictProxy: 59 order=dic._order 60 else: 61 order=dic.keys() 62 order.sort() 63 64 for k in order: 65 v=dic[k] 66 67 if type(k)==int: 68 s+=v 69 continue 70 71 if k.find("anonymValue")==0: 72 k="" 73 74 s+=(" "*indent)+k 75 if type(v)in [unicode,str]: 76 s+=" "+v+";\n" 77 elif type(v) in [dict,DictProxy]: 78 s+="\n"+(" "*indent)+"{\n" 79 s+=self.strDict(v,indent+2) 80 s+=(" "*indent)+"}\n" 81 elif type(v) in [list,UnparsedList]: 82 s+="\n" 83 s+=self.strList(v,indent+2) 84 if s[-1]=="\n": 85 s=s[:-1] 86 s+=";\n" 87 elif type(v) in [tuple,TupleProxy]: 88 s+=" "+self.strTuple(v,indent+2)+";\n" 89 elif type(v) in [int,float,long]: 90 s+=" "+str(v)+";\n" 91 elif v.__class__ in self.primitiveTypes: 92 s+=" "+str(v)+";\n" 93 elif v==None: 94 s+=" /* empty */ ;\n" 95 else: 96 error("Unhandled type",type(v)," for",v) 97 return s
98
99 - def strList(self,lst,indent=0):
100 s="" 101 102 if type(lst)==UnparsedList: 103 s+=(" "*indent)+str(len(lst))+" (" 104 s+=lst.data 105 if lst.data[-1]!="\n": 106 s+="\n" 107 s+=(" "*indent)+")\n" 108 return s 109 110 theLen=len(lst) 111 112 if len(lst)>2 and len(lst)%2==0: 113 if type(lst[0])in [unicode,str] and (type(lst[1]) in [dict,DictProxy]): 114 theLen=len(lst)/2 115 116 isFixedType=False 117 if len(lst)==3 or len(lst)==9 or len(lst)==6: 118 isFixedType=True 119 for l in lst: 120 try: 121 val=float(l) 122 except (ValueError,TypeError): 123 isFixedType=False 124 125 if isFixedType: 126 s+="("+string.join(map(lambda v:"%g"%v,lst))+")" 127 else: 128 if theLen>20: 129 s+=(" "*indent)+str(theLen)+"\n" 130 s+=(" "*indent)+"(\n" 131 for v in lst: 132 if type(v)in [unicode,str]: 133 s+=(" "*(indent+2))+v+"\n" 134 elif type(v) in [dict,DictProxy]: 135 s+="\n"+(" "*(indent+2))+"{\n" 136 s+=self.strDict(v,indent+4) 137 s+="\n"+(" "*(indent+2))+"}\n" 138 elif type(v) in [list,UnparsedList]: 139 s+="\n" 140 s+=self.strList(v,indent+2) 141 elif type(v)==tuple: 142 s+=" "+self.strTuple(v,indent+2)+" " 143 else: 144 s+=(" "*(indent+2))+str(v)+"\n" 145 146 s+=(" "*indent)+")\n" 147 148 return s
149
150 - def strTuple(self,lst,indent=0):
151 s="" 152 153 for v in lst: 154 if type(v)in [unicode,str]: 155 s+=v+" " 156 elif type(v) in [dict,DictProxy]: 157 s+="{\n" 158 s+=self.strDict(v,indent+4) 159 s+=(" "*(indent+2))+"} " 160 elif type(v) in [list,UnparsedList]: 161 s+=" " 162 s+=self.strList(v,indent+2) 163 else: 164 s+=(" "*(indent+2))+str(v)+" " 165 166 return s
167
168 -def makeString(data):
169 return str(FoamFileGenerator(data))
170
171 -class FoamFileGeneratorError(PyFoamException):
172 - def __init__(self,descr):
173 PyFoamException.__init__(self,descr)
174