comparison engine/extensions/fife_utils.py @ 273:815354ba295e

* Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS. * Made rio_de_hola save its settings in the proper location.
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 16 Jun 2009 14:24:26 +0000
parents 9a1529f9625e
children cf77afb273c4
comparison
equal deleted inserted replaced
272:b04a2faf7d86 273:815354ba295e
1 import fife, re 1 import fife, re, sys, os
2 2
3 __all__ = ['is_fife_exc'] 3 __all__ = ['is_fife_exc', 'getUserDataDirectory']
4 4
5 _exc_re = re.compile(r'_\[(\w+)\]_') 5 _exc_re = re.compile(r'_\[(\w+)\]_')
6 6
7 """ This file contains some functions that may be useful """
8
7 def is_fife_exc(type, original_exc): 9 def is_fife_exc(type, original_exc):
10 """ Checks if an exception is of given type.
11 Example:
12 try:
13 obj = self.model.createObject(str(id), str(nspace), parent)
14 except RuntimeError, e:
15 if is_fife_exc(fife.NameClash, e):
16 raise NameClash('Tried to create already existing object, ignoring')
17 raise
18 """
8 ret = False 19 ret = False
9 m = _exc_re.search(str(original_exc)) 20 m = _exc_re.search(str(original_exc))
10 if m: 21 if m:
11 if m.group(1) == type('').getTypeStr(): 22 if m.group(1) == type('').getTypeStr():
12 ret = True 23 ret = True
13 return ret 24 return ret
25
26 def getUserDataDirectory(vendor, appname):
27 """ Gets the proper location to save configuration and data files, depending on depending on OS.
28
29 Windows: %APPDATA%\vendor\appname
30 Mac: ~/Library/Application Support/vendor/appname
31 Linux/Unix/Other: ~/.vendor/appname
32
33 See:
34 Brian Vanderburg II @ http://mail.python.org/pipermail/python-list/2008-May/660779.html
35 """
36 dir = ""
37
38 # WINDOWS
39 if os.name == "nt":
40
41 # Try env APPDATA or USERPROFILE or HOMEDRIVE/HOMEPATH
42 if "APPDATA" in os.environ:
43 dir = os.environ["APPDATA"]
44
45 if ((dir is None) or (not os.path.isdir(dir))) and ("USERPROFILE" in os.environ):
46 dir = os.environ["USERPROFILE"]
47 if os.path.isdir(os.path.join(dir, "Application Data")):
48 dir = os.path.join(dir, "Application Data")
49
50 if ((dir is None) or (not os.path.isdir(dir))) and ("HOMEDRIVE" in os.environ) and ("HOMEPATH" in os.environ):
51 dir = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
52 if os.path.isdir(os.path.join(dir, "Application Data")):
53 dir = os.path.join(dir, "Application Data")
54
55 if (dir is None) or (not os.path.isdir(dir)):
56 dir = os.path.expanduser("~")
57
58 # On windows, add vendor and app name
59 dir = os.path.join(dir, vendor, appname)
60
61 # Mac
62 elif os.name == "mac": # ?? may not be entirely correct
63 dir = os.path.expanduser("~")
64 dir = os.path.join(dir, "Library", "Application Support")
65 dir = os.path.join(dir, vendor, appname)
66
67 # Unix/Linux/all others
68 if dir is None:
69 dir = os.path.expanduser("~")
70 dir = os.path.join(dir, "."+vendor, appname)
71
72 # Create vendor/appname folder if it doesn't exist
73 if not os.path.isdir(dir):
74 os.makedirs(dir)
75
76 print dir
77
78 return dir