Package PyFoam :: Package Paraview :: Module StateFile
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Paraview.StateFile

  1  """ 
  2  Represents a Paraview State-fime (pvsm) and manipulates it 
  3  """ 
  4   
  5  from xml.dom.minidom import parse 
  6  import xml.dom 
  7  from os import path 
  8  import os 
  9  import shutil 
 10  import glob 
 11   
 12  from PyFoam.Error import error 
 13  from PyFoam import configuration as config 
 14  from tempfile import mkstemp 
 15   
16 -class StateFile(object):
17 """The actual PVSM-file 18 19 Stores the actual file as an xml-file"""
20 - def __init__(self,fName):
21 """@param fName: the XML-file that represents the Paraview-state""" 22 23 dom=parse(fName) 24 self.doc=dom.documentElement
25
26 - def readerType(self):
27 """Returns the type of the used OF-Reader as a string""" 28 return self.getReader().data.getAttribute("type")
29
30 - def setCase(self,case):
31 """Rewrite the state-file so that it uses another case than the one 32 predefined in the state-file 33 @param case: The path to the new case-file""" 34 reader=self.getReader() 35 typ=reader.data.getAttribute("type") 36 if typ=="PV3FoamReader": 37 reader.setProperty("FileName",case) 38 elif typ=="OpenFOAMReader": 39 oldFile=reader.getProperty("FileName") 40 fName=path.basename(oldFile) 41 newFile=path.join(path.dirname(case),fName) 42 if not path.exists(newFile): 43 open(newFile,"w") 44 reader.setProperty("FileName",newFile) 45 else: 46 error("Reader type",typ,"not implemented for state-file rewritting")
47
48 - def __str__(self):
49 """Write the file as a string""" 50 return self.doc.toxml()
51
52 - def writeTemp(self):
53 """Write the state to a temporary file and return the name of that file""" 54 fd,fn=mkstemp(suffix=".pvsm",text=True) 55 56 fh=os.fdopen(fd,"w") 57 fh.write(str(self)) 58 fh.close() 59 60 return fn
61
62 - def serverState(self):
63 tmp=self.doc.getElementsByTagName("ServerManagerState") 64 if len(tmp)!=1: 65 error("Wrong number of ServerManagerStates:",len(tmp)) 66 67 return tmp[0]
68
69 - def getProxy(self,type_):
70 """Return a list of Prxy-elements that fit a specific type""" 71 result=[] 72 73 for p in self.serverState().getElementsByTagName("Proxy"): 74 tp=p.getAttribute("type") 75 if type_==tp: 76 result.append(Proxy(p)) 77 78 return result
79
80 - def getReader(self):
81 """Return the Proxy-Element with the reader""" 82 tmp=self.getProxy("PV3FoamReader")+self.getProxy("OpenFOAMReader") 83 if len(tmp)!=1: 84 error("Wrong number of Readers in State-File. Need 1 but got",len(tmp)) 85 86 return tmp[0]
87
88 - def rewriteTexts(self,values):
89 """Rewrite all Text-Objects so that strings of the form %%(key)s get replaced 90 @param values: dictionary with the values""" 91 tmp=self.getProxy("TextSource") 92 for t in tmp: 93 t.rewriteProperty("Text",values)
94
95 -class Proxy(object):
96 """Convenience class for handling proxies"""
97 - def __init__(self,xml):
98 self.data=xml
99
100 - def setProperty(self,name,value,index=None):
101 """Set a property in a proxy 102 103 @param name: name of the property 104 @param value: the new value 105 @param index: Index. If not specified all elements are changed""" 106 107 for p in self.data.getElementsByTagName("Property"): 108 if p.getAttribute("name")==name: 109 for e in p.getElementsByTagName("Element"): 110 if index==None or index==int(e.getAttribute("index")): 111 e.setAttribute("value",str(value))
112
113 - def getProperty(self,name,index=None):
114 """Get a property in a proxy 115 116 @param name: name of the property 117 @param index: Index. If not specified all elements are changed""" 118 119 for p in self.data.getElementsByTagName("Property"): 120 if p.getAttribute("name")==name: 121 for e in p.getElementsByTagName("Element"): 122 if index==None or index==int(e.getAttribute("index")): 123 return e.getAttribute("value") 124 return None
125
126 - def rewriteProperty(self,name,values,index=None):
127 """Rewrites a property by replacing all strings of the form %%(key)s 128 (Python-notation for dictionary-replacement) with a corresponding value 129 130 @param name: name of the property 131 @param values: Dictionary with the keys and the corresponding values 132 @param index: Index. If not specified all elements are changed""" 133 134 for p in self.data.getElementsByTagName("Property"): 135 if p.getAttribute("name")==name: 136 for e in p.getElementsByTagName("Element"): 137 if index==None or index==int(e.getAttribute("index")): 138 old = e.getAttribute("value") 139 new = old % values 140 if new!=old: 141 # print "Replacing",old,"with",new 142 e.setAttribute("value",new)
143