annotate orpg/tools/settings.py @ 39:ed322725b928 ornery-orc tip

Traipse 'OpenRPG' {110114-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 (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 New IronClaw roller, sheet, and image New ShapeShifter PC Sheet 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 Update to location.py, allows for more portable references when starting Traipse Update to the Features node 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 Server, removing wxPython dependencies where not needed 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 Fix to Gametree, renaming and remapping operates correctly Fix to aliaslib, prevents error caused when SafeHTML is sent None
author sirebral
date Fri, 14 Jan 2011 05:24:52 -0600
parents d02e9197c066
children
rev   line source
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
1 import os
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
2
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
3 from orpg.tools.orpg_log import logger
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
4 from orpg.tools.validate import validate
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
5 from xml.etree.ElementTree import ElementTree, Element, parse
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
6 from xml.etree.ElementTree import fromstring, tostring
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
7 from orpg.orpgCore import component
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
8 from orpg.dirpath import dir_struct
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
9
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
10 class Settings:
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
11 def __init__(self):
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
12 self.changes = []
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
13 validate.config_file("settings.xml","default_settings.xml")
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
14 self.filename = dir_struct["user"] + "settings.xml"
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
15 xml_dom = parse(dir_struct["user"] + "settings.xml")
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
16 if xml_dom == None: self.rebuildSettings()
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
17 else: self.xml_dom = xml_dom.getroot()
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
18
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
19 def rebuildSettings(self):
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
20 logger.info("Settings file has be corrupted, rebuilding settings.", True)
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
21 try: os.remove(self.filename)
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
22 except: pass
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
23 validate.config_file("settings.xml","default_settings.xml")
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
24 self.xml_dom = parse(self.filename).getroot()
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
25
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
26 def get_setting(self, name): ##Depricated
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
27 return self.get(name)
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
29 def get(self, name):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
30 try: return self.xml_dom.getiterator(name)[0].get("value")
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
31 except: return 0
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
32
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
33 def get_setting_keys(self): ##Depricated
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
34 return self.get_keys()
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
35
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
36 def get_keys(self):
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
37 keys = []
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
38 tabs = self.xml_dom.getiterator("tab")
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
39 for i in xrange(0, len(tabs)):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
40 if tabs[i].get("type") == 'grid':
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
41 children = tabs[i].getchildren()
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
42 for c in children: keys.append(c.tag)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
43 return keys
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
44
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
45 def set_setting(self, name, value): ##Depricated
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
46 self.change(name, value)
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
47
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
48 def change(self, name, value):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
49 self.xml_dom.getiterator(name)[0].set("value", value)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
50
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
51 def add_setting(self, tab, setting, value, options, help): ##Depricated
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
52 return self.add(tab, setting, value, options, help)
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
53
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
54 def add(self, tab, setting, value, options, help):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
55 if len(self.xml_dom.getiterator(setting)) > 0: return False
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
56 tabs = self.xml_dom.getiterator("tab")
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
57 newsetting = fromstring('<' + setting + ' value="' + value + '" options="' +
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
58 options + '" help="' + help + '" />')
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
59 for i in xrange(0, len(tabs)):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
60 if tabs[i].get("name") == tab and tabs[i].get("type") == 'grid':
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
61 tabs[i].append(newsetting)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
62 return True
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
63 return False
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
64
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
65 def add_tab(self, parent, tabname, tabtype):
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
66 tab_xml = '<tab '
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
67 if tabtype == 'text': tab_xml += 'name="' + tabname + '" type="text" />'
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
68 else: tab_xml += 'name="' + tabname + '" type="' + tabtype + '"></tab>'
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
69 newtab = fromstring(tab_xml)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
70 if parent != None:
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
71 tabs = self.xml_dom.getiterator("tab")
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
72 for i in xrange(0, len(tabs)):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
73 if tabs[i].get("name") == parent and tabs[i].get("type") == 'tab':
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
74 children = tabs[i].getchildren()
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
75 for c in children:
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
76 if c.get("name") == tabname: return False
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
77 tabs[i].append(newtab)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
78 return True
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
79 else:
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
80 children = self.xml_dom.getchildren()
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
81 for c in children:
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
82 if c.get("name") == tabname: return False
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
83 self.xml_dom.append(newtab)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
84 return True
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
85 return False
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
86
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
87 def updateIni(self):
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
88 defaultFile = orpg.dirpath.dir_struct['template'] + 'default_settings.xml'
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
89 default_dom = parse(defaultfile)
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
90 for child in default_dom.getchildren():
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
91 if child.tag == 'tab': self.proccessChildren(child)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
92
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
93 def proccessChildren(self, dom, parent=None):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
94 if dom.tag == 'tab': self.add_tab(parent, dom.get("name"), dom.get("type"))
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
95
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
96 for child in dom.getchildren():
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
97 if child.tag == 'tab': self.proccessChildren(child, dom.get("name"))
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
98 else:
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
99 self.add_setting(dom.get("name"), child.tag,
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
100 child.get("value"), child.get("options"),
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
101 child.get("help"))
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
102
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
103 def save(self):
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
104 #self.xml_dom.write(self.filename)
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
105 temp_file = open(self.filename, "w")
36
d02e9197c066 Traipse 'OpenRPG' {101220-00}
sirebral
parents: 28
diff changeset
106 temp_file.write(tostring(self.xml_dom))
28
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
107 temp_file.close()
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
108
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
109 settings = Settings()
ff154cf3350c Traipse 'OpenRPG' {100203-00}
sirebral
parents:
diff changeset
110 component.add('settings', settings)