1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """ This file contains some functions that may be useful """
25
26 import fife, re, sys, os
27
28 __all__ = ['is_fife_exc', 'getUserDataDirectory']
29
30 _exc_re = re.compile(r'_\[(\w+)\]_')
31
33 """ Checks if an exception is of given type.
34 Example:
35 try:
36 obj = self.model.createObject(str(id), str(nspace), parent)
37 except RuntimeError, e:
38 if is_fife_exc(fife.NameClash, e):
39 raise NameClash('Tried to create already existing object, ignoring')
40 raise
41 """
42 ret = False
43 m = _exc_re.search(str(original_exc))
44 if m:
45 if m.group(1) == type('').getTypeStr():
46 ret = True
47 return ret
48
50 """ Gets the proper location to save configuration and data files, depending on depending on OS.
51
52 Windows: %APPDATA%\vendor\appname
53 Mac: ~/Library/Application Support/vendor/appname
54 Linux/Unix/Other: ~/.vendor/appname
55
56 See:
57 Brian Vanderburg II @ http://mail.python.org/pipermail/python-list/2008-May/660779.html
58 """
59 dir = None
60
61
62 if os.name == "nt":
63
64
65 if "APPDATA" in os.environ:
66 dir = os.environ["APPDATA"]
67
68 if ((dir is None) or (not os.path.isdir(dir))) and ("USERPROFILE" in os.environ):
69 dir = os.environ["USERPROFILE"]
70 if os.path.isdir(os.path.join(dir, "Application Data")):
71 dir = os.path.join(dir, "Application Data")
72
73 if ((dir is None) or (not os.path.isdir(dir))) and ("HOMEDRIVE" in os.environ) and ("HOMEPATH" in os.environ):
74 dir = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
75 if os.path.isdir(os.path.join(dir, "Application Data")):
76 dir = os.path.join(dir, "Application Data")
77
78 if (dir is None) or (not os.path.isdir(dir)):
79 dir = os.path.expanduser("~")
80
81
82 dir = os.path.join(dir, vendor, appname)
83
84
85 elif os.name == "mac":
86 dir = os.path.expanduser("~")
87 dir = os.path.join(dir, "Library", "Application Support")
88 dir = os.path.join(dir, vendor, appname)
89
90
91 if dir is None:
92 dir = os.path.expanduser("~")
93 dir = os.path.join(dir, "."+vendor, appname)
94
95
96 if not os.path.isdir(dir):
97 os.makedirs(dir)
98
99 return dir
100