#This class offers a set of methods that allow the reading and writing of xml files using the ElementTree python module.
#It can be used to create xml file suitable to initialize object using the initializer Component::InitFromXmlFile . In the ISCE package each object will be defined with the following elements
#Each paramenter of the object named "NameOfTheObject" will be defined by a "property element". Inside the property element there are other elements
#that characterize the specific paramenter. The element "name" defines the name of the variable and is the same as the key in
#the dictionaryOfVariables of the object at hand (see Component::Component). The "value" is the value that the specific variable will be initialized to.
#All the other elements will be part of the descriptionOfVariables dictionary (see Component::Component).
#@see http://effbot.org/zone/element-index.htm
#@see Component::Component
classXmlUtil:
##
# Reads an xml file and turns it into an ElementTree object.
#@param file either a file name or a file object
#@return an ElementTree object
defreadFile(self,file):
tree=ET.parse(file)
returntree
##
# Writes a dictionary into an indented xml file
# @param file \c string filename to be used.
# @param dict \c dictionary to be saved in xml format
# @param name \c string the name to be set in the 'name' field
# Writes an ElementTree object or a root element of a ElementTree object into an indented xml file
defwriteFile(self,file,object):
root=None
try:
root=object.getroot()
exceptException:
root=object
self.indent(root)
etObj=ET.ElementTree(root)
etObj.write(file)
#if the string contained in obj is an actual object, when is exec there is no problem. if it was supposed to be a string, the name will not be defined aand an exception is thrown. put in a function to reduce chance that the name is actually defined
defisStr(self,obj):
retVal=False
try:
exec(obj)
returnFalse
except:
returnTrue
##
#Given an ElementTree object it creates a dictionary of dictionaries where each entry corresponds to a "property" element. For instance in the example
#since description.text is a string by doing exec it will put the actual value into the dictionary.
if(notself.isStr(description.text)):# if value is a string when execed it will see it as a name that has not been defined -> exception so try and catch