1
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,Codestream
6
7 import string
8
10 """Class that generates a OpenFOAM-compatible representation of a
11 data-structure"""
12
13 primitiveTypes=[SymmTensor,Tensor,Vector,Dimension,Field,Unparsed]
14
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
26
28 """turns the data into a string"""
29 result=""
30 if self.header:
31 result+="FoamFile\n{\n"+self.strDict(self.header,indent=1)+"}\n\n"
32
33 if type(self.data) in [dict,DictProxy]:
34 result+=self.strDict(self.data,firstLevel=firstLevel)
35 elif type(self.data) in [tuple,TupleProxy]:
36 result+=self.strTuple(self.data)
37 elif type(self.data) in [list,UnparsedList]:
38 result+=self.strList(self.data)
39 elif self.data==None:
40 raise FoamFileGeneratorError("<None> found")
41 else:
42 result+=self.strPrimitive(self.data)
43
44 return result
45
47 if type(pri) in [int,float,long,str,unicode]:
48 return str(pri)
49 elif type(pri)==bool:
50 if pri:
51 return "yes"
52 else:
53 return "no"
54 elif pri.__class__ in self.primitiveTypes:
55 return str(pri)
56 else:
57 error("List, Dict or valid primitve expected,",type(pri),"found in",pri)
58
59 - def strDict(self,dic,indent=0,firstLevel=False):
60 s=""
61 if type(dic)==DictProxy:
62 order=dic._order
63 else:
64 order=dic.keys()
65 order.sort()
66
67 for k in order:
68 try:
69 v=dic[k]
70 except KeyError:
71 v=dic.getRegexpValue(k)
72
73 end="\n"
74 if type(dic)==DictProxy:
75 end=dic.getDecoration(k)+"\n"
76
77 if firstLevel:
78 end+="\n"
79
80 if type(k)==int:
81 s+=v
82 continue
83
84 if k.find("anonymValue")==0:
85 k=""
86
87 s+=(" "*indent)+k
88 if type(v)in [unicode,str]:
89 s+=" "+v+";"+end
90 elif type(v) in [dict,DictProxy]:
91 s+="\n"+(" "*indent)+"{\n"
92 s+=self.strDict(v,indent+2)
93 s+=(" "*indent)+"}"+end
94 elif type(v)==Codestream:
95 s+="\n"
96 s+=" "*indent
97 s+=str(v)
98 s+=";"+end
99 elif type(v) in [list,UnparsedList]:
100 s+="\n"
101 s+=self.strList(v,indent+2)
102 if s[-1]=="\n":
103 s=s[:-1]
104 s+=";"+end
105 elif type(v) in [tuple,TupleProxy]:
106 s+=" "+self.strTuple(v,indent+2)+";"+end
107 elif type(v) in [int,float,long]:
108 s+=" "+str(v)+";"+end
109 elif type(v)==bool:
110 if v:
111 s+=" yes;\n"
112 else:
113 s+=" no;\n"
114 elif v.__class__ in self.primitiveTypes:
115 s+=" "+str(v)+";"+end
116 elif v==None:
117 s+=" /* empty */ ;"+end
118 else:
119 error("Unhandled type",type(v)," for",v)
120 return s
121
123 s=""
124
125 if type(lst)==UnparsedList:
126 s+=(" "*indent)+str(len(lst))+" ("
127 s+=lst.data
128 if lst.data[-1]!="\n":
129 s+="\n"
130 s+=(" "*indent)+")\n"
131 return s
132
133 theLen=len(lst)
134
135 if len(lst)>2 and len(lst)%2==0:
136 if type(lst[0])in [unicode,str] and (type(lst[1]) in [dict,DictProxy]):
137 theLen=len(lst)/2
138
139 isFixedType=False
140 if len(lst)==3 or len(lst)==9 or len(lst)==6:
141 isFixedType=True
142 for l in lst:
143 try:
144 float(l)
145 except (ValueError,TypeError):
146 isFixedType=False
147
148 if isFixedType:
149 s+="("+string.join(map(lambda v:"%g"%v,lst))+")"
150 else:
151 if theLen>20:
152 s+=(" "*indent)+str(theLen)+"\n"
153 s+=(" "*indent)+"(\n"
154 for v in lst:
155 if type(v)in [unicode,str]:
156 s+=(" "*(indent+2))+v+"\n"
157 elif type(v) in [dict,DictProxy]:
158 s+="\n"+(" "*(indent+2))+"{\n"
159 s+=self.strDict(v,indent+4)
160 s+="\n"+(" "*(indent+2))+"}\n"
161 elif type(v) in [list,UnparsedList]:
162 s+="\n"
163 s+=self.strList(v,indent+2)
164 elif type(v)==tuple:
165 s+=" "+self.strTuple(v,indent+2)+" "
166 else:
167 s+=(" "*(indent+2))+str(v)+"\n"
168
169 s+=(" "*indent)+")\n"
170
171 return s
172
174 s=""
175
176 for v in lst:
177 if type(v)in [unicode,str]:
178 s+=v+" "
179 elif type(v) in [dict,DictProxy]:
180 s+="{\n"
181 s+=self.strDict(v,indent+4)
182 s+=(" "*(indent+2))+"} "
183 elif type(v) in [list,UnparsedList]:
184 s+=" "
185 s+=self.strList(v,indent+2)
186 else:
187 s+=(" "*(indent+2))+str(v)+" "
188
189 return s
190
193
197