comparison clients/editor/scripts/settings.py @ 271:987a4ea829c1

Save editor settings to proper directory (~/.fife on linux, %APPDATA%\fife on Win32)
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 16 Jun 2009 02:33:35 +0000
parents 51cc05d862f2
children 815354ba295e
comparison
equal deleted inserted replaced
270:4625a0a5eb3a 271:987a4ea829c1
17 def __init__(self, *args, **kwargs): 17 def __init__(self, *args, **kwargs):
18 if Settings.instance is not None: 18 if Settings.instance is not None:
19 raise RuntimeWarning("Settings instance has already been initialized! Use Editor.getSettings instead") 19 raise RuntimeWarning("Settings instance has already been initialized! Use Editor.getSettings instead")
20 20
21 Settings.instance = self 21 Settings.instance = self
22 22
23 if os.path.exists('settings.xml') is False: 23 self._appdata = GetUserDataDirectory("fife", "editor")
24 shutil.copyfile('settings-dist.xml', 'settings.xml') 24
25 25 if os.path.exists(self._appdata+'/settings.xml') is False:
26 self.tree = ET.parse('settings.xml') 26 shutil.copyfile('settings-dist.xml', self._appdata+'/settings.xml')
27
28 self.tree = ET.parse(self._appdata+'/settings.xml')
27 self.root_element = self.tree.getroot() 29 self.root_element = self.tree.getroot()
28 self.validateTree() 30 self.validateTree()
29 31
30 def validateTree(self): 32 def validateTree(self):
31 """ Iterates the settings tree and prints warning when an invalid tag is found """ 33 """ Iterates the settings tree and prints warning when an invalid tag is found """
162 elm.text = value 164 elm.text = value
163 165
164 def saveSettings(self): 166 def saveSettings(self):
165 """ Save settings into settings.xml """ 167 """ Save settings into settings.xml """
166 self._indent(self.root_element) 168 self._indent(self.root_element)
167 self.tree.write('settings.xml', 'UTF-8') 169 self.tree.write(self._appdata+'/settings.xml', 'UTF-8')
168 170
169 def _indent(self, elem, level=0): 171 def _indent(self, elem, level=0):
170 """ 172 """
171 Adds whitespace, so the resulting XML-file is properly indented. 173 Adds whitespace, so the resulting XML-file is properly indented.
172 Shamelessly stolen from http://effbot.org/zone/element-lib.htm 174 Shamelessly stolen from http://effbot.org/zone/element-lib.htm
214 items = serial.split(" ; ") 216 items = serial.split(" ; ")
215 for i in items: 217 for i in items:
216 kv_pair = i.split(" : ") 218 kv_pair = i.split(" : ")
217 dict[kv_pair[0]] = kv_pair[1] 219 dict[kv_pair[0]] = kv_pair[1]
218 return dict 220 return dict
221
222 def GetUserDataDirectory(vendor, appname):
223 """ Gets location to store the settings file, depending on OS.
224 See:
225 Brian Vanderburg II @ http://mail.python.org/pipermail/python-list/2008-May/660779.html
226 """
227 dir = ""
228
229 # WINDOWS
230 if os.name == "nt":
231
232 # Try env APPDATA or USERPROFILE or HOMEDRIVE/HOMEPATH
233 if "APPDATA" in os.environ:
234 dir = os.environ["APPDATA"]
235
236 if ((dir is None) or (not os.path.isdir(dir))) and ("USERPROFILE" in os.environ):
237 dir = os.environ["USERPROFILE"]
238 if os.path.isdir(os.path.join(dir, "Application Data")):
239 dir = os.path.join(dir, "Application Data")
240
241 if ((dir is None) or (not os.path.isdir(dir))) and ("HOMEDRIVE" in os.environ) and ("HOMEPATH" in os.environ):
242 dir = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
243 if os.path.isdir(os.path.join(dir, "Application Data")):
244 dir = os.path.join(dir, "Application Data")
245
246 if (dir is None) or (not os.path.isdir(dir)):
247 dir = os.path.expanduser("~")
248
249 # On windows, add vendor and app name
250 dir = os.path.join(dir, vendor, appname)
251
252 # Mac
253 elif os.name == "mac": # ?? may not be entirely correct
254 dir = os.path.expanduser("~")
255 dir = os.path.join(dir, "Library", "Application Support")
256 dir = os.path.join(dir, vendor, appname)
257
258 # Unix/Linux/all others
259 if dir is None:
260 dir = os.path.expanduser("~")
261 dir = os.path.join(dir, "."+vendor, appname)
262
263 # Create vendor/appname folder if it doesn't exist
264 if not os.path.isdir(dir):
265 os.makedirs(dir)
266
267 print dir
268
269 return dir
270