annotate orpg/tools/settings.py @ 242:72e0cce81a47 beta

Traipse Beta 'OpenRPG' {100817-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 (Closing/Closed) New Features: New to Map, can re-order Grid, Miniatures, and Whiteboard layer draw order New to Server GUI, can now clear log New Earthdawn Dieroller Updates: Update to Warhammer PC Sheet. Rollers set as macros. Should work with little maintanence. Update to Browser Server window. Display rooms with ' " & cleaner Update to Server. Handles ' " & cleaner Update to Dieroller. Cleaner, more effecient expression system Update to Hidden Die plugin, allows for non standard dice rolls 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 Fix to Single and Double quotes in Whiteboard text Fix to Background images not showing when using the Image Server Fix to Duplicate chat names appearing Fix to Server GUI's logging output Fix to FNB.COLORFUL_TABS bug Fix to Gametree for XSLT Sheets Fix to Gametree for locating gametree files Fix to Send to Chat from Gametree
author sirebral
date Tue, 17 Aug 2010 14:53:04 -0500
parents 9230a33defd9
children
rev   line source
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
1 import os
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
2
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
3 from orpg.tools.orpg_log import logger
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
4 from orpg.tools.validate import validate
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
5 from xml.etree.ElementTree import ElementTree, Element, parse
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
6 from xml.etree.ElementTree import fromstring, tostring
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
7 from orpg.orpgCore import component
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
8 from orpg.dirpath import dir_struct
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
9
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
10 class Settings:
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
11 def __init__(self):
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
12 self.changes = []
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
13 validate.config_file("settings.xml","default_settings.xml")
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
14 self.filename = dir_struct["user"] + "settings.xml"
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
15 xml_dom = parse(dir_struct["user"] + "settings.xml")
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
16 if xml_dom == None: self.rebuildSettings()
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
17 else: self.xml_dom = xml_dom.getroot()
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
18
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
19 def rebuildSettings(self):
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
20 logger.info("Settings file has be corrupted, rebuilding settings.", True)
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
21 try: os.remove(self.filename)
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
22 except: pass
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
23 validate.config_file("settings.xml","default_settings.xml")
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
24 self.xml_dom = parse(self.filename).getroot()
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
25
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
26 def get_setting(self, name): ##Depricated
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
27 return self.get(name)
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
28
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
29 def get(self, name):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
30 try: return self.xml_dom.getiterator(name)[0].get("value")
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
31 except: return 0
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
32
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
33 def get_setting_keys(self): ##Depricated
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
34 return self.get_keys()
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
35
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
36 def get_keys(self):
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
37 keys = []
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
38 tabs = self.xml_dom.getiterator("tab")
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
39 for i in xrange(0, len(tabs)):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
40 if tabs[i].get("type") == 'grid':
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
41 children = tabs[i].getchildren()
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
42 for c in children: keys.append(c.tag)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
43 return keys
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
44
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
45 def set_setting(self, name, value): ##Depricated
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
46 self.change(name, value)
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
47
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
48 def change(self, name, value):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
49 self.xml_dom.getiterator(name)[0].set("value", value)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
50
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
51 def add_setting(self, tab, setting, value, options, help): ##Depricated
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
52 return self.add(tab, setting, value, options, help)
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
53
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
54 def add(self, tab, setting, value, options, help):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
55 if len(self.xml_dom.getiterator(setting)) > 0: return False
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
56 tabs = self.xml_dom.getiterator("tab")
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
57 newsetting = fromstring('<' + setting + ' value="' + value + '" options="' +
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
58 options + '" help="' + help + '" />')
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
59 for i in xrange(0, len(tabs)):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
60 if tabs[i].get("name") == tab and tabs[i].get("type") == 'grid':
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
61 tabs[i].append(newsetting)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
62 return True
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
63 return False
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
64
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
65 def add_tab(self, parent, tabname, tabtype):
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
66 tab_xml = '<tab '
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
67 if tabtype == 'text': tab_xml += 'name="' + tabname + '" type="text" />'
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
68 else: tab_xml += 'name="' + tabname + '" type="' + tabtype + '"></tab>'
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
69 newtab = fromstring(tab_xml)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
70 if parent != None:
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
71 tabs = self.xml_dom.getiterator("tab")
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
72 for i in xrange(0, len(tabs)):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
73 if tabs[i].get("name") == parent and tabs[i].get("type") == 'tab':
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
74 children = tabs[i].getchildren()
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
75 for c in children:
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
76 if c.get("name") == tabname: return False
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
77 tabs[i].append(newtab)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
78 return True
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
79 else:
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
80 children = self.xml_dom.getchildren()
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
81 for c in children:
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
82 if c.get("name") == tabname: return False
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
83 self.xml_dom.append(newtab)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
84 return True
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
85 return False
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
86
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
87 def updateIni(self):
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
88 defaultFile = orpg.dirpath.dir_struct['template'] + 'default_settings.xml'
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
89 default_dom = parse(defaultfile)
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
90 for child in default_dom.getchildren():
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
91 if child.tag == 'tab': self.proccessChildren(child)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
92
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
93 def proccessChildren(self, dom, parent=None):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
94 if dom.tag == 'tab': self.add_tab(parent, dom.get("name"), dom.get("type"))
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
95
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
96 for child in dom.getchildren():
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
97 if child.tag == 'tab': self.proccessChildren(child, dom.get("name"))
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
98 else:
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
99 self.add_setting(dom.get("name"), child.tag,
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
100 child.get("value"), child.get("options"),
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
101 child.get("help"))
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
102
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
103 def save(self):
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
104 #self.xml_dom.write(self.filename)
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
105 temp_file = open(self.filename, "w")
236
9230a33defd9 Traipse Beta 'OpenRPG' {100616-00}
sirebral
parents: 135
diff changeset
106 temp_file.write(tostring(self.xml_dom))
135
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
107 temp_file.close()
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
108
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
109 settings = Settings()
dcf4fbe09b70 Traipse Beta 'OpenRPG' {091010-00}
sirebral
parents:
diff changeset
110 component.add('settings', settings)