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