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