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,DictRedirection
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
15 - def __init__(self,
16 data,
17 header=None,
18 longListThreshold=20):
19 """@param data: data structure that will be turned into a
20 Foam-compatible file
21 @param header: header information that is to be prepended
22 @param longListThreshold: Threshold for lists before they are considered
23 long. This means that they are prefixed with the number of elements. If the
24 threshold is 0 or None then no list is considered long
25 """
26
27 self.data=data
28 self.header=header
29 self.longListThreshold=longListThreshold
30
33
35 """turns the data into a string"""
36 result=""
37 if self.header:
38 result+="FoamFile\n{\n"+self.strDict(self.header,indent=1)+"}\n\n"
39
40 if type(self.data) in [dict,DictProxy]:
41 result+=self.strDict(self.data,firstLevel=firstLevel)
42 elif type(self.data) in [tuple,TupleProxy]:
43 result+=self.strTuple(self.data)
44 elif type(self.data) in [list,UnparsedList]:
45 result+=self.strList(self.data)
46 elif self.data==None:
47 raise FoamFileGeneratorError("<None> found")
48 else:
49 result+=self.strPrimitive(self.data)
50
51 return result
52
54 if type(pri) in [int,float,long,str,unicode]:
55 return str(pri)
56 elif type(pri)==bool:
57 if pri:
58 return "yes"
59 else:
60 return "no"
61 elif pri.__class__ in self.primitiveTypes:
62 return str(pri)
63 else:
64 error("List, Dict or valid primitve expected,",type(pri),"found in",pri)
65
66 - def strDict(self,dic,indent=0,firstLevel=False):
67 s=""
68 if type(dic)==DictProxy:
69 order=dic._order
70 else:
71 order=dic.keys()
72 order.sort()
73
74 for k in order:
75 if type(k)==DictRedirection:
76 v=k
77 else:
78 try:
79 v=dic[k]
80 except KeyError:
81 v=dic.getRegexpValue(k)
82
83 end="\n"
84 if type(dic)==DictProxy:
85 end=dic.getDecoration(k)+"\n"
86
87 if firstLevel:
88 end+="\n"
89
90 if type(k)==int:
91 s+=v
92 continue
93
94 if str(k).find("anonymValue")==0:
95 k=""
96
97 s+=(" "*indent)+str(k)
98 if type(v)in [unicode,str]:
99 s+=" "+v+";"+end
100 elif type(v) in [dict,DictProxy]:
101 s+="\n"+(" "*indent)+"{\n"
102 s+=self.strDict(v,indent+2)
103 s+=(" "*indent)+"}"+end
104 elif type(v)==Codestream:
105 s+="\n"
106 s+=" "*indent
107 s+=str(v)
108 s+=";"+end
109 elif type(v) in [list,UnparsedList]:
110 s+="\n"
111 s+=self.strList(v,indent+2)
112 if s[-1]=="\n":
113 s=s[:-1]
114 s+=";"+end
115 elif type(v) in [tuple,TupleProxy]:
116 s+=" "+self.strTuple(v,indent+2)+";"+end
117 elif type(v) in [int,float,long]:
118 s+=" "+str(v)+";"+end
119 elif type(v)==bool:
120 if v:
121 s+=" yes;\n"
122 else:
123 s+=" no;\n"
124 elif v.__class__ in self.primitiveTypes:
125 s+=" "+str(v)+";"+end
126 elif v==None:
127 s+=" /* empty */ ;"+end
128 elif type(v)==DictRedirection:
129 s+=";"+end
130 else:
131 error("Unhandled type",type(v)," for",v)
132 return s
133
135 s=""
136
137 if type(lst)==UnparsedList:
138 s+=(" "*indent)+str(len(lst))+" ("
139 s+=lst.data
140 if lst.data[-1]!="\n":
141 s+="\n"
142 s+=(" "*indent)+")\n"
143 return s
144
145 theLen=len(lst)
146
147 if len(lst)>2 and len(lst)%2==0:
148 if type(lst[0])in [unicode,str] and (type(lst[1]) in [dict,DictProxy]):
149 theLen=len(lst)/2
150
151 isFixedType=False
152 if len(lst)==3 or len(lst)==9 or len(lst)==6:
153 isFixedType=True
154 for l in lst:
155 try:
156 float(l)
157 except (ValueError,TypeError):
158 isFixedType=False
159
160 if isFixedType:
161 s+="("+string.join(map(lambda v:"%g"%v,lst))+")"
162 else:
163 if self.longListThreshold:
164 if theLen>self.longListThreshold:
165 s+=(" "*indent)+str(theLen)+"\n"
166 s+=(" "*indent)+"(\n"
167 for v in lst:
168 if type(v)in [unicode,str]:
169 s+=(" "*(indent+2))+v+"\n"
170 elif type(v) in [dict,DictProxy]:
171 s+="\n"+(" "*(indent+2))+"{\n"
172 s+=self.strDict(v,indent+4)
173 s+="\n"+(" "*(indent+2))+"}\n"
174 elif type(v) in [list,UnparsedList]:
175 s+="\n"
176 s+=self.strList(v,indent+2)
177 elif type(v)==tuple:
178 s+=" "+self.strTuple(v,indent+2)+" "
179 else:
180 s+=(" "*(indent+2))+str(v)+"\n"
181
182 s+=(" "*indent)+")\n"
183
184 return s
185
187 s=""
188
189 for v in lst:
190 if type(v)in [unicode,str]:
191 s+=v+" "
192 elif type(v) in [dict,DictProxy]:
193 s+="{\n"
194 s+=self.strDict(v,indent+4)
195 s+=(" "*(indent+2))+"} "
196 elif type(v) in [list,UnparsedList]:
197 s+=" "
198 s+=self.strList(v,indent+2)
199 else:
200 s+=(" "*(indent+2))+str(v)+" "
201
202 return s
203
206
210