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