Package PyFoam :: Package Basics :: Module FoamFileGenerator
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.FoamFileGenerator

  1  #  ICE Revision: $Id: FoamFileGenerator.py 8217 2007-11-22 18:17:47Z bgschaid $  
  2  """Transform a Python data-structure into a OpenFOAM-File-Representation""" 
  3   
  4  from PyFoam.Error import error 
  5  from PyFoam.Basics.DataStructures import Vector,Field,Dimension,DictProxy,Tensor,SymmTensor 
  6   
  7  import string 
  8   
9 -class FoamFileGenerator(object):
10 """Class that generates a OpenFOAM-compatible representation of a 11 data-structure""" 12
13 - def __init__(self,data,header=None):
14 """@param data: data structure that will be turned into a 15 Foam-compatible file 16 @param header: header information that is to be prepended 17 """ 18 19 self.data=data 20 self.header=header
21
22 - def __str__(self):
23 """turns the data into a string""" 24 result="" 25 if self.header: 26 result+="FoamFile\n{\n"+self.strDict(self.header,indent=1)+"}\n\n" 27 28 if type(self.data) in [dict,DictProxy]: 29 result+=self.strDict(self.data) 30 elif type(self.data)==tuple: 31 result+=self.strTuple(self.data) 32 elif type(self.data)==list: 33 result+=self.strList(self.data) 34 elif self.data==None: 35 raise "GeneratorError","<None> found" 36 else: 37 result+=self.strPrimitive(self.data) 38 39 return result
40
41 - def strPrimitive(self,pri):
42 if type(pri) in [int,float,long,str]: 43 return str(pri) 44 elif type(pri)==bool: 45 if pri: 46 return "yes" 47 else: 48 return "no" 49 elif pri.__class__ in [SymmTensor,Tensor,Vector,Dimension,Field]: 50 return str(pri) 51 else: 52 error("List, Dict or valid primitve expected,",type(pri),"found in",pri)
53
54 - def strDict(self,dic,indent=0):
55 s="" 56 if type(dic)==DictProxy: 57 order=dic._order 58 else: 59 order=dic.keys() 60 order.sort() 61 62 for k in order: 63 v=dic[k] 64 if k.find("anonymValue")==0: 65 k="" 66 67 s+=(" "*indent)+k 68 if type(v)==str: 69 s+=" "+v+";\n" 70 elif type(v) in [dict,DictProxy]: 71 s+="\n"+(" "*indent)+"{\n" 72 s+=self.strDict(v,indent+2) 73 s+=(" "*indent)+"}\n" 74 elif type(v)==list: 75 s+="\n" 76 s+=self.strList(v,indent+2)+";\n" 77 elif type(v)==tuple: 78 s+=" "+self.strTuple(v,indent+2)+";\n" 79 elif type(v) in [int,float,long]: 80 s+=" "+str(v)+";\n" 81 elif v.__class__ in [SymmTensor,Tensor,Vector,Dimension,Field]: 82 s+=" "+str(v)+";\n" 83 elif v==None: 84 s+=" /* empty */ ;\n" 85 else: 86 error("Unhandled type",type(v)," for",v) 87 return s
88
89 - def strList(self,lst,indent=0):
90 s="" 91 92 theLen=len(lst) 93 94 if len(lst)>2 and len(lst)%2==0: 95 if type(lst[0])==str and (type(lst[1]) in [dict,DictProxy]): 96 theLen=len(lst)/2 97 98 isFixedType=False 99 if len(lst)==3 or len(lst)==9 or len(lst)==6: 100 isFixedType=True 101 for l in lst: 102 try: 103 val=float(l) 104 except (ValueError,TypeError): 105 isFixedType=False 106 107 if isFixedType: 108 s+="("+string.join(map(lambda v:"%g"%v,lst))+")" 109 else: 110 s+=(" "*indent)+str(theLen)+"\n" 111 s+=(" "*indent)+"(\n" 112 for v in lst: 113 if type(v)==str: 114 s+=(" "*(indent+2))+v+"\n" 115 elif type(v) in [dict,DictProxy]: 116 s+="\n"+(" "*(indent+2))+"{\n" 117 s+=self.strDict(v,indent+4) 118 s+="\n"+(" "*(indent+2))+"}\n" 119 elif type(v)==list: 120 s+="\n" 121 s+=self.strList(v,indent+2) 122 elif type(v)==tuple: 123 s+=" "+self.strTuple(v,indent+2)+" " 124 else: 125 s+=(" "*(indent+2))+str(v)+"\n" 126 127 s+=(" "*indent)+")\n" 128 129 return s
130
131 - def strTuple(self,lst,indent=0):
132 s="" 133 134 for v in lst: 135 if type(v)==str: 136 s+=v+" " 137 elif type(v) in [dict,DictProxy]: 138 s+="{\n" 139 s+=self.strDict(v,indent+4) 140 s+=(" "*(indent+2))+"} " 141 elif type(v)==list: 142 s+=" " 143 s+=self.strList(v,indent+2) 144 else: 145 s+=(" "*(indent+2))+str(v)+" " 146 147 return s
148
149 -def makeString(data):
150 return str(FoamFileGenerator(data))
151