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