from xml.dom import minidom from os.path import join, exists from os import mkdir import re class Settings: """ Settings Class For reading and writting XML Files to hold the settings for scripts Coded by Donno getSett, saveXMLfromArray are designed for outside access. """ settingVer = 1.0 scriptTitle = "" filename = "" settings = {} def __init__(self,filename,scriptTitle): self.scriptTitle = scriptTitle self.filename = filename self.load() def getSett(self,settingName, defaultValue): """ Returns the value of the setting If the setting doesn't exist it will use default Value """ if self.settings.has_key(settingName): return self.settings[settingName].strip() else: return defaultValue def load(self): filepath = join("P:\\","script_settings", self.filename) if exists(filepath): try: xmldoc = minidom.parse(filepath) xmlsettings = xmldoc.getElementsByTagName("settings")[0] except: self.create() return """ For this to be great xml would need to be in form hello xmldoc.getElementsByTagName("setting") would get every setting then could use getAttribute to get the name of the setting 'username' Current from here on it does regexp to take the settings from the xml and put them in an array So solution is change the formatting, or hack it get donno working """ data = xmlsettings.toxml() regp = '<([^<]*)>([^ base element settingElem = xmldoc.createElement("settings") settingElem.setAttribute("setting-version", str(self.settingVer)) settingElem.setAttribute("name", self.scriptTitle) xmldoc.appendChild(settingElem) # Create a element for each setting with the textnode as the value for key in self.settings: newElem = xmldoc.createElement(key) newElem.appendChild(xmldoc.createTextNode(self.settings[key])) settingElem.appendChild(newElem) self.save(xmldoc) def save(self,xmldoc): """ This function takes a xml Document object and saves it to XML """ filepath = join("P:\\","script_settings", self.filename) if not exists(join("P:\\","script_settings")): mkdir(join("P:\\","script_settings")) fsock = open(filepath,"w") fsock.write(xmldoc.toprettyxml(indent=" ")) fsock.close()