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 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) in [list,UnparsedList]:
95 s+="\n"
96 s+=self.strList(v,indent+2)
97 if s[-1]=="\n":
98 s=s[:-1]
99 s+=";"+end
100 elif type(v) in [tuple,TupleProxy]:
101 s+=" "+self.strTuple(v,indent+2)+";"+end
102 elif type(v) in [int,float,long]:
103 s+=" "+str(v)+";"+end
104 elif type(v)==bool:
105 if v:
106 s+=" yes;\n"
107 else:
108 s+=" no;\n"
109 elif v.__class__ in self.primitiveTypes:
110 s+=" "+str(v)+";"+end
111 elif v==None:
112 s+=" /* empty */ ;"+end
113 else:
114 error("Unhandled type",type(v)," for",v)
115 return s
116
118 s=""
119
120 if type(lst)==UnparsedList:
121 s+=(" "*indent)+str(len(lst))+" ("
122 s+=lst.data
123 if lst.data[-1]!="\n":
124 s+="\n"
125 s+=(" "*indent)+")\n"
126 return s
127
128 theLen=len(lst)
129
130 if len(lst)>2 and len(lst)%2==0:
131 if type(lst[0])in [unicode,str] and (type(lst[1]) in [dict,DictProxy]):
132 theLen=len(lst)/2
133
134 isFixedType=False
135 if len(lst)==3 or len(lst)==9 or len(lst)==6:
136 isFixedType=True
137 for l in lst:
138 try:
139 float(l)
140 except (ValueError,TypeError):
141 isFixedType=False
142
143 if isFixedType:
144 s+="("+string.join(map(lambda v:"%g"%v,lst))+")"
145 else:
146 if theLen>20:
147 s+=(" "*indent)+str(theLen)+"\n"
148 s+=(" "*indent)+"(\n"
149 for v in lst:
150 if type(v)in [unicode,str]:
151 s+=(" "*(indent+2))+v+"\n"
152 elif type(v) in [dict,DictProxy]:
153 s+="\n"+(" "*(indent+2))+"{\n"
154 s+=self.strDict(v,indent+4)
155 s+="\n"+(" "*(indent+2))+"}\n"
156 elif type(v) in [list,UnparsedList]:
157 s+="\n"
158 s+=self.strList(v,indent+2)
159 elif type(v)==tuple:
160 s+=" "+self.strTuple(v,indent+2)+" "
161 else:
162 s+=(" "*(indent+2))+str(v)+"\n"
163
164 s+=(" "*indent)+")\n"
165
166 return s
167
169 s=""
170
171 for v in lst:
172 if type(v)in [unicode,str]:
173 s+=v+" "
174 elif type(v) in [dict,DictProxy]:
175 s+="{\n"
176 s+=self.strDict(v,indent+4)
177 s+=(" "*(indent+2))+"} "
178 elif type(v) in [list,UnparsedList]:
179 s+=" "
180 s+=self.strList(v,indent+2)
181 else:
182 s+=(" "*(indent+2))+str(v)+" "
183
184 return s
185
188
192