28
|
1 import os
|
|
2
|
|
3 from orpg.tools.orpg_log import logger
|
|
4 from orpg.tools.validate import validate
|
36
|
5 from xml.etree.ElementTree import ElementTree, Element, parse
|
|
6 from xml.etree.ElementTree import fromstring, tostring
|
28
|
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"
|
36
|
15 xml_dom = parse(dir_struct["user"] + "settings.xml")
|
|
16 if xml_dom == None: self.rebuildSettings()
|
|
17 else: self.xml_dom = xml_dom.getroot()
|
28
|
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")
|
36
|
24 self.xml_dom = parse(self.filename).getroot()
|
28
|
25
|
|
26 def get_setting(self, name): ##Depricated
|
|
27 return self.get(name)
|
|
28
|
|
29 def get(self, name):
|
36
|
30 try: return self.xml_dom.getiterator(name)[0].get("value")
|
28
|
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 = []
|
36
|
38 tabs = self.xml_dom.getiterator("tab")
|
28
|
39 for i in xrange(0, len(tabs)):
|
36
|
40 if tabs[i].get("type") == 'grid':
|
|
41 children = tabs[i].getchildren()
|
|
42 for c in children: keys.append(c.tag)
|
28
|
43 return keys
|
|
44
|
|
45 def set_setting(self, name, value): ##Depricated
|
|
46 self.change(name, value)
|
|
47
|
|
48 def change(self, name, value):
|
36
|
49 self.xml_dom.getiterator(name)[0].set("value", value)
|
28
|
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):
|
36
|
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 + '" />')
|
28
|
59 for i in xrange(0, len(tabs)):
|
36
|
60 if tabs[i].get("name") == tab and tabs[i].get("type") == 'grid':
|
|
61 tabs[i].append(newsetting)
|
28
|
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>'
|
36
|
69 newtab = fromstring(tab_xml)
|
28
|
70 if parent != None:
|
36
|
71 tabs = self.xml_dom.getiterator("tab")
|
28
|
72 for i in xrange(0, len(tabs)):
|
36
|
73 if tabs[i].get("name") == parent and tabs[i].get("type") == 'tab':
|
|
74 children = tabs[i].getchildren()
|
28
|
75 for c in children:
|
36
|
76 if c.get("name") == tabname: return False
|
|
77 tabs[i].append(newtab)
|
28
|
78 return True
|
|
79 else:
|
36
|
80 children = self.xml_dom.getchildren()
|
28
|
81 for c in children:
|
36
|
82 if c.get("name") == tabname: return False
|
|
83 self.xml_dom.append(newtab)
|
28
|
84 return True
|
|
85 return False
|
|
86
|
|
87 def updateIni(self):
|
|
88 defaultFile = orpg.dirpath.dir_struct['template'] + 'default_settings.xml'
|
36
|
89 default_dom = parse(defaultfile)
|
|
90 for child in default_dom.getchildren():
|
|
91 if child.tag == 'tab': self.proccessChildren(child)
|
28
|
92
|
|
93 def proccessChildren(self, dom, parent=None):
|
36
|
94 if dom.tag == 'tab': self.add_tab(parent, dom.get("name"), dom.get("type"))
|
28
|
95
|
36
|
96 for child in dom.getchildren():
|
|
97 if child.tag == 'tab': self.proccessChildren(child, dom.get("name"))
|
28
|
98 else:
|
36
|
99 self.add_setting(dom.get("name"), child.tag,
|
|
100 child.get("value"), child.get("options"),
|
|
101 child.get("help"))
|
28
|
102
|
|
103 def save(self):
|
36
|
104 #self.xml_dom.write(self.filename)
|
28
|
105 temp_file = open(self.filename, "w")
|
36
|
106 temp_file.write(tostring(self.xml_dom))
|
28
|
107 temp_file.close()
|
|
108
|
|
109 settings = Settings()
|
|
110 component.add('settings', settings)
|