1 """Transform a Python data-structure into a OpenFOAM-File-Representation"""
2
3 from PyFoam.Error import error
4
6 """Class that generates a OpenFOAM-compatible representation of a
7 data-structure"""
8
10 """@param data: data structure that will be turned into a
11 Foam-compatible file
12 @param header: header information that is to be prepended
13 """
14
15 self.data=data
16 self.header=header
17
19 """turns the data into a string"""
20 result=""
21 if self.header:
22 result+="FoamFile\n{\n"+self.strDict(self.header,indent=1)+"}\n\n"
23 if type(self.data)==dict:
24 result+=self.strDict(self.data)
25 elif type(self.data)==list:
26 result+=self.strList(self.data)
27 else:
28 error("List or Dict expected,",type(self.data),"found in",self.data)
29
30 return result
31
33 s=""
34 for k,v in dic.iteritems():
35 if k.find("anonymValue")==0:
36 k=""
37
38 s+=(" "*indent)+k
39 if type(v)==str:
40 s+=" "+v+";\n"
41 elif type(v)==dict:
42 s+="\n"+(" "*indent)+"{\n"
43 s+=self.strDict(v,indent+2)
44 s+="\n"+(" "*indent)+"}\n"
45 elif type(v)==list:
46 s+="\n"
47 s+=self.strList(v,indent+2)+";\n"
48 elif type(v)==tuple:
49 s+=" "+self.strTuple(v,indent+2)+";\n"
50 else:
51 s+=" "+str(v)+";\n"
52
53 return s
54
56 s=""
57
58 theLen=len(lst)
59
60 if len(lst)>2 and len(lst)%2==0:
61 if type(lst[0])==str and type(lst[1])==dict:
62 theLen=len(lst)/2
63
64 isVector=False
65 if len(lst)==3:
66 isVector=True
67 for l in lst:
68 try:
69 val=float(l)
70 except ValueError:
71 isVector=False
72
73 if isVector:
74 s+="( %s %s %s )" % tuple(lst)
75 else:
76 s+=(" "*indent)+str(theLen)+"\n"
77 s+=(" "*indent)+"(\n"
78 for v in lst:
79 if type(v)==str:
80 s+=(" "*(indent+2))+v+"\n"
81 elif type(v)==dict:
82 s+="\n"+(" "*(indent+2))+"{\n"
83 s+=self.strDict(v,indent+4)
84 s+="\n"+(" "*(indent+2))+"}\n"
85 elif type(v)==list:
86 s+="\n"
87 s+=self.strList(v,indent+2)
88 elif type(v)==tuple:
89 s+=" "+self.strTuple(v,indent+2)+" "
90 else:
91 s+=(" "*(indent+2))+str(v)+"\n"
92
93 s+=(" "*indent)+")\n"
94
95 return s
96
98 s=""
99
100 for v in lst:
101 if type(v)==str:
102 s+=v+" "
103 elif type(v)==dict:
104 s+="{\n"
105 s+=self.strDict(v,indent+4)
106 s+="\n"+(" "*(indent+2))+"} "
107 elif type(v)==list:
108 s+=" "
109 s+=self.strList(v,indent+2)
110 else:
111 s+=(" "*(indent+2))+str(v)+" "
112
113 return s
114