comparison orpg/tools/settings.py @ 228:24769389a7ba alpha

Traipse Alpha 'OpenRPG' {100612-01} 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 04:38:29 -0500
parents 37d26a98883f
children
comparison
equal deleted inserted replaced
225:2c6db2043764 228:24769389a7ba
1 import os 1 import os
2 2
3 from orpg.tools.orpg_log import logger 3 from orpg.tools.orpg_log import logger
4 from orpg.tools.validate import validate 4 from orpg.tools.validate import validate
5 from orpg.orpg_xml import xml 5 from xml.etree.ElementTree import ElementTree, Element, parse
6 from xml.etree.ElementTree import fromstring, tostring
6 from orpg.orpgCore import component 7 from orpg.orpgCore import component
7 from orpg.dirpath import dir_struct 8 from orpg.dirpath import dir_struct
8 9
9 class Settings: 10 class Settings:
10 def __init__(self): 11 def __init__(self):
11 self.xml = component.get("xml")
12 self.changes = [] 12 self.changes = []
13 validate.config_file("settings.xml","default_settings.xml") 13 validate.config_file("settings.xml","default_settings.xml")
14 self.filename = dir_struct["user"] + "settings.xml" 14 self.filename = dir_struct["user"] + "settings.xml"
15 temp_file = open(self.filename) 15 xml_dom = parse(dir_struct["user"] + "settings.xml")
16 txt = temp_file.read() 16 if xml_dom == None: self.rebuildSettings()
17 temp_file.close() 17 else: self.xml_dom = xml_dom.getroot()
18
19 self.xml_dom = xml.parseXml(txt)
20
21 if self.xml_dom is None: self.rebuildSettings()
22 self.xml_dom = self.xml_dom._get_documentElement()
23 18
24 def rebuildSettings(self): 19 def rebuildSettings(self):
25 logger.info("Settings file has be corrupted, rebuilding settings.", True) 20 logger.info("Settings file has be corrupted, rebuilding settings.", True)
26 try: os.remove(self.filename) 21 try: os.remove(self.filename)
27 except: pass 22 except: pass
28
29 validate.config_file("settings.xml","default_settings.xml") 23 validate.config_file("settings.xml","default_settings.xml")
30 temp_file = open(self.filename) 24 self.xml_dom = parse(self.filename).getroot()
31 txt = temp_file.read()
32 temp_file.close()
33 self.xml_dom = xml.parseXml(txt)
34 25
35 def get_setting(self, name): ##Depricated 26 def get_setting(self, name): ##Depricated
36 return self.get(name) 27 return self.get(name)
37 28
38 def get(self, name): 29 def get(self, name):
39 try: return self.xml_dom.getElementsByTagName(name)[0].getAttribute("value") 30 try: return self.xml_dom.getiterator(name)[0].get("value")
40 except: return 0 31 except: return 0
41 32
42 def get_setting_keys(self): ##Depricated 33 def get_setting_keys(self): ##Depricated
43 return self.get_keys() 34 return self.get_keys()
44 35
45 def get_keys(self): 36 def get_keys(self):
46 keys = [] 37 keys = []
47 tabs = self.xml_dom.getElementsByTagName("tab") 38 tabs = self.xml_dom.getiterator("tab")
48 for i in xrange(0, len(tabs)): 39 for i in xrange(0, len(tabs)):
49 if tabs[i].getAttribute("type") == 'grid': 40 if tabs[i].get("type") == 'grid':
50 children = tabs[i]._get_childNodes() 41 children = tabs[i].getchildren()
51 for c in children: keys.append(c._get_tagName()) 42 for c in children: keys.append(c.tag)
52 return keys 43 return keys
53 44
54 def set_setting(self, name, value): ##Depricated 45 def set_setting(self, name, value): ##Depricated
55 self.change(name, value) 46 self.change(name, value)
56 47
57 def change(self, name, value): 48 def change(self, name, value):
58 self.xml_dom.getElementsByTagName(name)[0].setAttribute("value", value) 49 self.xml_dom.getiterator(name)[0].set("value", value)
59 50
60 def add_setting(self, tab, setting, value, options, help): ##Depricated 51 def add_setting(self, tab, setting, value, options, help): ##Depricated
61 return self.add(tab, setting, value, options, help) 52 return self.add(tab, setting, value, options, help)
62 53
63 def add(self, tab, setting, value, options, help): 54 def add(self, tab, setting, value, options, help):
64 if len(self.xml_dom.getElementsByTagName(setting)) > 0: return False 55 if len(self.xml_dom.getiterator(setting)) > 0: return False
65 tabs = self.xml_dom.getElementsByTagName("tab") 56 tabs = self.xml_dom.getiterator("tab")
66 newsetting = xml.parseXml('<' + setting + ' value="' + value + '" options="' + 57 newsetting = fromstring('<' + setting + ' value="' + value + '" options="' +
67 options + '" help="' + help + '" />')._get_documentElement() 58 options + '" help="' + help + '" />')
68 for i in xrange(0, len(tabs)): 59 for i in xrange(0, len(tabs)):
69 if tabs[i].getAttribute("name") == tab and tabs[i].getAttribute("type") == 'grid': 60 if tabs[i].get("name") == tab and tabs[i].get("type") == 'grid':
70 tabs[i].appendChild(newsetting) 61 tabs[i].append(newsetting)
71 return True 62 return True
72 return False 63 return False
73 64
74 def add_tab(self, parent, tabname, tabtype): 65 def add_tab(self, parent, tabname, tabtype):
75 tab_xml = '<tab ' 66 tab_xml = '<tab '
76 if tabtype == 'text': tab_xml += 'name="' + tabname + '" type="text" />' 67 if tabtype == 'text': tab_xml += 'name="' + tabname + '" type="text" />'
77 else: tab_xml += 'name="' + tabname + '" type="' + tabtype + '"></tab>' 68 else: tab_xml += 'name="' + tabname + '" type="' + tabtype + '"></tab>'
78 newtab = xml.parseXml(tab_xml)._get_documentElement() 69 newtab = fromstring(tab_xml)
79 if parent != None: 70 if parent != None:
80 tabs = self.xml_dom.getElementsByTagName("tab") 71 tabs = self.xml_dom.getiterator("tab")
81 for i in xrange(0, len(tabs)): 72 for i in xrange(0, len(tabs)):
82 if tabs[i].getAttribute("name") == parent and tabs[i].getAttribute("type") == 'tab': 73 if tabs[i].get("name") == parent and tabs[i].get("type") == 'tab':
83 children = tabs[i]._get_childNodes() 74 children = tabs[i].getchildren()
84 for c in children: 75 for c in children:
85 if c.getAttribute("name") == tabname: return False 76 if c.get("name") == tabname: return False
86 tabs[i].appendChild(newtab) 77 tabs[i].append(newtab)
87 return True 78 return True
88 else: 79 else:
89 children = self.xml_dom._get_childNodes() 80 children = self.xml_dom.getchildren()
90 for c in children: 81 for c in children:
91 if c.getAttribute("name") == tabname: return False 82 if c.get("name") == tabname: return False
92 self.xml_dom.appendChild(newtab) 83 self.xml_dom.append(newtab)
93 return True 84 return True
94 return False 85 return False
95 86
96 def updateIni(self): 87 def updateIni(self):
97 defaultFile = orpg.dirpath.dir_struct['template'] + 'default_settings.xml' 88 defaultFile = orpg.dirpath.dir_struct['template'] + 'default_settings.xml'
98 temp_file = open(defaultFile) 89 default_dom = parse(defaultfile)
99 txt = temp_file.read() 90 for child in default_dom.getchildren():
100 temp_file.close() 91 if child.tag == 'tab': self.proccessChildren(child)
101 default_dom = xml.parseXml(txt)._get_documentElement()
102 for child in default_dom.getChildren():
103 if child._get_tagName() == 'tab' and child.hasChildNodes(): self.proccessChildren(child)
104 default_dom.unlink()
105 92
106 def proccessChildren(self, dom, parent=None): 93 def proccessChildren(self, dom, parent=None):
107 if dom._get_tagName() == 'tab': 94 if dom.tag == 'tab': self.add_tab(parent, dom.get("name"), dom.get("type"))
108 self.add_tab(parent, dom.getAttribute("name"), dom.getAttribute("type"))
109 95
110 for child in dom.getChildren(): 96 for child in dom.getchildren():
111 if child._get_tagName() == 'tab' and child.hasChildNodes(): 97 if child.tag == 'tab': self.proccessChildren(child, dom.get("name"))
112 self.proccessChildren(child, dom.getAttribute("name"))
113 else: 98 else:
114 self.add_setting(dom.getAttribute("name"), child._get_tagName(), 99 self.add_setting(dom.get("name"), child.tag,
115 child.getAttribute("value"), child.getAttribute("options"), 100 child.get("value"), child.get("options"),
116 child.getAttribute("help")) 101 child.get("help"))
117 102
118 def save(self): 103 def save(self):
104 #self.xml_dom.write(self.filename)
119 temp_file = open(self.filename, "w") 105 temp_file = open(self.filename, "w")
120 temp_file.write(xml.toxml(self.xml_dom,1)) 106 temp_file.write(tostring(self.xml_dom))
121 temp_file.close() 107 temp_file.close()
122 108
123 settings = Settings() 109 settings = Settings()
124 component.add('settings', settings) 110 component.add('settings', settings)