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

Source Code for Module PyFoam.Basics.FoamFileGenerator

  1  #  ICE Revision: $Id: FoamFileGenerator.py 9163 2008-08-04 08:01:10Z 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 if k.find("anonymValue")==0: 67 k="" 68 69 s+=(" "*indent)+k 70 if type(v)in [unicode,str]: 71 s+=" "+v+";\n" 72 elif type(v) in [dict,DictProxy]: 73 s+="\n"+(" "*indent)+"{\n" 74 s+=self.strDict(v,indent+2) 75 s+=(" "*indent)+"}\n" 76 elif type(v) in [list,UnparsedList]: 77 s+="\n" 78 s+=self.strList(v,indent+2) 79 if s[-1]=="\n": 80 s=s[:-1] 81 s+=";\n" 82 elif type(v) in [tuple,TupleProxy]: 83 s+=" "+self.strTuple(v,indent+2)+";\n" 84 elif type(v) in [int,float,long]: 85 s+=" "+str(v)+";\n" 86 elif v.__class__ in self.primitiveTypes: 87 s+=" "+str(v)+";\n" 88 elif v==None: 89 s+=" /* empty */ ;\n" 90 else: 91 error("Unhandled type",type(v)," for",v) 92 return s
93
94 - def strList(self,lst,indent=0):
95 s="" 96 97 if type(lst)==UnparsedList: 98 s+=(" "*indent)+str(len(lst))+" (" 99 s+=lst.data 100 if lst.data[-1]!="\n": 101 s+="\n" 102 s+=(" "*indent)+")\n" 103 return s 104 105 theLen=len(lst) 106 107 if len(lst)>2 and len(lst)%2==0: 108 if type(lst[0])in [unicode,str] and (type(lst[1]) in [dict,DictProxy]): 109 theLen=len(lst)/2 110 111 isFixedType=False 112 if len(lst)==3 or len(lst)==9 or len(lst)==6: 113 isFixedType=True 114 for l in lst: 115 try: 116 val=float(l) 117 except (ValueError,TypeError): 118 isFixedType=False 119 120 if isFixedType: 121 s+="("+string.join(map(lambda v:"%g"%v,lst))+")" 122 else: 123 if theLen>20: 124 s+=(" "*indent)+str(theLen)+"\n" 125 s+=(" "*indent)+"(\n" 126 for v in lst: 127 if type(v)in [unicode,str]: 128 s+=(" "*(indent+2))+v+"\n" 129 elif type(v) in [dict,DictProxy]: 130 s+="\n"+(" "*(indent+2))+"{\n" 131 s+=self.strDict(v,indent+4) 132 s+="\n"+(" "*(indent+2))+"}\n" 133 elif type(v) in [list,UnparsedList]: 134 s+="\n" 135 s+=self.strList(v,indent+2) 136 elif type(v)==tuple: 137 s+=" "+self.strTuple(v,indent+2)+" " 138 else: 139 s+=(" "*(indent+2))+str(v)+"\n" 140 141 s+=(" "*indent)+")\n" 142 143 return s
144
145 - def strTuple(self,lst,indent=0):
146 s="" 147 148 for v in lst: 149 if type(v)in [unicode,str]: 150 s+=v+" " 151 elif type(v) in [dict,DictProxy]: 152 s+="{\n" 153 s+=self.strDict(v,indent+4) 154 s+=(" "*(indent+2))+"} " 155 elif type(v) in [list,UnparsedList]: 156 s+=" " 157 s+=self.strList(v,indent+2) 158 else: 159 s+=(" "*(indent+2))+str(v)+" " 160 161 return s
162
163 -def makeString(data):
164 return str(FoamFileGenerator(data))
165
166 -class FoamFileGeneratorError(PyFoamException):
167 - def __init__(self,descr):
168 PyFoamException.__init__(self,descr)
169