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 setCase(self,case):
27 """Rewrite the state-file so that it uses another case than the one 28 predefined in the state-file 29 @param case: The path to the new case-file""" 30 reader=self.getReader() 31 reader.setProperty("FileName",case)
32
33 - def __str__(self):
34 """Write the file as a string""" 35 return self.doc.toxml()
36
37 - def writeTemp(self):
38 """Write the state to a temporary file and return the name of that file""" 39 fd,fn=mkstemp(suffix=".pvsm",text=True) 40 41 fh=os.fdopen(fd,"w") 42 fh.write(str(self)) 43 fh.close() 44 45 return fn
46
47 - def serverState(self):
48 tmp=self.doc.getElementsByTagName("ServerManagerState") 49 if len(tmp)!=1: 50 error("Wrong number of ServerManagerStates:",len(tmp)) 51 52 return tmp[0]
53
54 - def getProxy(self,type_):
55 """Return a list of Prxy-elements that fit a specific type""" 56 result=[] 57 58 for p in self.serverState().getElementsByTagName("Proxy"): 59 tp=p.getAttribute("type") 60 if type_==tp: 61 result.append(Proxy(p)) 62 63 return result
64
65 - def getReader(self):
66 """Return the Proxy-Element with the reader""" 67 tmp=self.getProxy("PV3FoamReader") 68 if len(tmp)!=1: 69 error("Wrong number of Readers in State-File. Need 1 but got",len(tmp)) 70 71 return tmp[0]
72
73 - def rewriteTexts(self,values):
74 """Rewrite all Text-Objects so that strings of the form %%(key)s get replaced 75 @param values: dictionary with the values""" 76 tmp=self.getProxy("TextSource") 77 for t in tmp: 78 t.rewriteProperty("Text",values)
79
80 -class Proxy(object):
81 """Convenience class for handling proxies"""
82 - def __init__(self,xml):
83 self.data=xml
84
85 - def setProperty(self,name,value,index=None):
86 """Set a property in a proxy 87 88 @param name: name of the property 89 @param value: the new value 90 @param index: Index. If not specified all elements are changed""" 91 92 for p in self.data.getElementsByTagName("Property"): 93 if p.getAttribute("name")==name: 94 for e in p.getElementsByTagName("Element"): 95 if index==None or index==int(e.getAttribute("index")): 96 e.setAttribute("value",str(value))
97
98 - def rewriteProperty(self,name,values,index=None):
99 """Rewrites a property by replacing all strings of the form %%(key)s 100 (Python-notation for dictionary-replacement) with a corresponding value 101 102 @param name: name of the property 103 @param values: Dictionary with the keys and the corresponding values 104 @param index: Index. If not specified all elements are changed""" 105 106 for p in self.data.getElementsByTagName("Property"): 107 if p.getAttribute("name")==name: 108 for e in p.getElementsByTagName("Element"): 109 if index==None or index==int(e.getAttribute("index")): 110 old = e.getAttribute("value") 111 new = old % values 112 if new!=old: 113 # print "Replacing",old,"with",new 114 e.setAttribute("value",new)
115