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