diff orpg/tools/settings.py @ 227:81d0bfd5e800 alpha

Traipse Alpha 'OpenRPG' {100612-00} Traipse is a distribution of OpenRPG that is designed to be easy to setup and go. Traipse also makes it easy for developers to work on code without fear of sacrifice. 'Ornery-Orc' continues the trend of 'Grumpy' and adds fixes to the code. 'Ornery-Orc's main goal is to offer more advanced features and enhance the productivity of the user. Update Summary (Preparing to close updates) New Features: New to Map, can re-order Grid, Miniatures, and Whiteboard layer draw order Fixes: Fix to InterParse that was causing an Infernal Loop with Namespace Internal Fix to XML data, removed old Minidom and switched to Element Tree Fix to Server that was causing eternal attempt to find a Server ID, in Register Rooms thread Fix to metaservers.xml file not being created
author sirebral
date Sat, 12 Jun 2010 03:50:37 -0500
parents 37d26a98883f
children
line wrap: on
line diff
--- a/orpg/tools/settings.py	Fri Jan 15 22:45:51 2010 -0600
+++ b/orpg/tools/settings.py	Sat Jun 12 03:50:37 2010 -0500
@@ -2,41 +2,32 @@
 
 from orpg.tools.orpg_log import logger
 from orpg.tools.validate import validate
-from orpg.orpg_xml import xml
+from xml.etree.ElementTree import ElementTree, Element, parse
+from xml.etree.ElementTree import fromstring, tostring
 from orpg.orpgCore import component
 from orpg.dirpath import dir_struct
 
 class Settings:
     def __init__(self):
-        self.xml = component.get("xml")
         self.changes = []
         validate.config_file("settings.xml","default_settings.xml")
         self.filename = dir_struct["user"] + "settings.xml"
-        temp_file = open(self.filename)
-        txt = temp_file.read()
-        temp_file.close()
-
-        self.xml_dom = xml.parseXml(txt)
-
-        if self.xml_dom is None: self.rebuildSettings()
-        self.xml_dom = self.xml_dom._get_documentElement()
+        xml_dom = parse(dir_struct["user"] + "settings.xml")
+        if xml_dom == None: self.rebuildSettings()
+        else: self.xml_dom = xml_dom.getroot()
 
     def rebuildSettings(self):
         logger.info("Settings file has be corrupted, rebuilding settings.", True)
         try: os.remove(self.filename)
         except: pass
-
         validate.config_file("settings.xml","default_settings.xml")
-        temp_file = open(self.filename)
-        txt = temp_file.read()
-        temp_file.close()
-        self.xml_dom = xml.parseXml(txt)
+        self.xml_dom = parse(self.filename).getroot()
 
     def get_setting(self, name): ##Depricated
         return self.get(name)
 
     def get(self, name): 
-        try: return self.xml_dom.getElementsByTagName(name)[0].getAttribute("value")
+        try: return self.xml_dom.getiterator(name)[0].get("value")
         except: return 0
 
     def get_setting_keys(self): ##Depricated
@@ -44,30 +35,30 @@
 
     def get_keys(self):
         keys = []
-        tabs = self.xml_dom.getElementsByTagName("tab")
+        tabs = self.xml_dom.getiterator("tab")
         for i in xrange(0, len(tabs)):
-            if tabs[i].getAttribute("type") == 'grid':
-                children = tabs[i]._get_childNodes()
-                for c in children: keys.append(c._get_tagName())
+            if tabs[i].get("type") == 'grid':
+                children = tabs[i].getchildren()
+                for c in children: keys.append(c.tag)
         return keys
 
     def set_setting(self, name, value): ##Depricated
         self.change(name, value)
 
     def change(self, name, value):
-        self.xml_dom.getElementsByTagName(name)[0].setAttribute("value", value)
+        self.xml_dom.getiterator(name)[0].set("value", value)
 
     def add_setting(self, tab, setting, value, options, help): ##Depricated
         return self.add(tab, setting, value, options, help)
 
     def add(self, tab, setting, value, options, help):
-        if len(self.xml_dom.getElementsByTagName(setting)) > 0: return False
-        tabs = self.xml_dom.getElementsByTagName("tab")
-        newsetting = xml.parseXml('<' + setting + ' value="' + value + '" options="' + 
-                                        options + '" help="' + help + '" />')._get_documentElement()
+        if len(self.xml_dom.getiterator(setting)) > 0: return False
+        tabs = self.xml_dom.getiterator("tab")
+        newsetting = fromstring('<' + setting + ' value="' + value + '" options="' + 
+                                        options + '" help="' + help + '" />')
         for i in xrange(0, len(tabs)):
-            if tabs[i].getAttribute("name") == tab and tabs[i].getAttribute("type") == 'grid':
-                tabs[i].appendChild(newsetting)
+            if tabs[i].get("name") == tab and tabs[i].get("type") == 'grid':
+                tabs[i].append(newsetting)
                 return True
         return False
 
@@ -75,49 +66,44 @@
         tab_xml = '<tab '
         if tabtype == 'text': tab_xml += 'name="' + tabname + '" type="text" />'
         else: tab_xml += 'name="' + tabname + '" type="' + tabtype + '"></tab>'
-        newtab = xml.parseXml(tab_xml)._get_documentElement()
+        newtab = fromstring(tab_xml)
         if parent != None:
-            tabs = self.xml_dom.getElementsByTagName("tab")
+            tabs = self.xml_dom.getiterator("tab")
             for i in xrange(0, len(tabs)):
-                if tabs[i].getAttribute("name") == parent and tabs[i].getAttribute("type") == 'tab':
-                    children = tabs[i]._get_childNodes()
+                if tabs[i].get("name") == parent and tabs[i].get("type") == 'tab':
+                    children = tabs[i].getchildren()
                     for c in children:
-                        if c.getAttribute("name") == tabname: return False
-                    tabs[i].appendChild(newtab)
+                        if c.get("name") == tabname: return False
+                    tabs[i].append(newtab)
                     return True
         else:
-            children = self.xml_dom._get_childNodes()
+            children = self.xml_dom.getchildren()
             for c in children:
-                if c.getAttribute("name") == tabname: return False
-            self.xml_dom.appendChild(newtab)
+                if c.get("name") == tabname: return False
+            self.xml_dom.append(newtab)
             return True
         return False
 
     def updateIni(self):
         defaultFile = orpg.dirpath.dir_struct['template'] + 'default_settings.xml'
-        temp_file = open(defaultFile)
-        txt = temp_file.read()
-        temp_file.close()
-        default_dom = xml.parseXml(txt)._get_documentElement()
-        for child in default_dom.getChildren():
-            if child._get_tagName() == 'tab' and child.hasChildNodes(): self.proccessChildren(child)
-        default_dom.unlink()
+        default_dom = parse(defaultfile)
+        for child in default_dom.getchildren():
+            if child.tag == 'tab': self.proccessChildren(child)
 
     def proccessChildren(self, dom, parent=None):
-        if dom._get_tagName() == 'tab':
-            self.add_tab(parent, dom.getAttribute("name"), dom.getAttribute("type"))
+        if dom.tag == 'tab': self.add_tab(parent, dom.get("name"), dom.get("type"))
 
-        for child in dom.getChildren():
-            if child._get_tagName() == 'tab' and child.hasChildNodes():
-                self.proccessChildren(child, dom.getAttribute("name"))
+        for child in dom.getchildren():
+            if child.tag == 'tab': self.proccessChildren(child, dom.get("name"))
             else:
-                self.add_setting(dom.getAttribute("name"), child._get_tagName(), 
-                                child.getAttribute("value"), child.getAttribute("options"), 
-                                child.getAttribute("help"))
+                self.add_setting(dom.get("name"), child.tag, 
+                                child.get("value"), child.get("options"), 
+                                child.get("help"))
 
     def save(self):
+        #self.xml_dom.write(self.filename)
         temp_file = open(self.filename, "w")
-        temp_file.write(xml.toxml(self.xml_dom,1))
+        temp_file.write(tostring(self.xml_dom))
         temp_file.close()
 
 settings = Settings()