comparison engine/python/fife/extensions/fife_utils.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children ae9f5383f5b1
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23 import fife, re, sys, os
24
25 __all__ = ['is_fife_exc', 'getUserDataDirectory']
26
27 _exc_re = re.compile(r'_\[(\w+)\]_')
28
29 """ This file contains some functions that may be useful """
30
31 def is_fife_exc(type, original_exc):
32 """ Checks if an exception is of given type.
33 Example:
34 try:
35 obj = self.model.createObject(str(id), str(nspace), parent)
36 except RuntimeError, e:
37 if is_fife_exc(fife.NameClash, e):
38 raise NameClash('Tried to create already existing object, ignoring')
39 raise
40 """
41 ret = False
42 m = _exc_re.search(str(original_exc))
43 if m:
44 if m.group(1) == type('').getTypeStr():
45 ret = True
46 return ret
47
48 def getUserDataDirectory(vendor, appname):
49 """ Gets the proper location to save configuration and data files, depending on depending on OS.
50
51 Windows: %APPDATA%\vendor\appname
52 Mac: ~/Library/Application Support/vendor/appname
53 Linux/Unix/Other: ~/.vendor/appname
54
55 See:
56 Brian Vanderburg II @ http://mail.python.org/pipermail/python-list/2008-May/660779.html
57 """
58 dir = None
59
60 # WINDOWS
61 if os.name == "nt":
62
63 # Try env APPDATA or USERPROFILE or HOMEDRIVE/HOMEPATH
64 if "APPDATA" in os.environ:
65 dir = os.environ["APPDATA"]
66
67 if ((dir is None) or (not os.path.isdir(dir))) and ("USERPROFILE" in os.environ):
68 dir = os.environ["USERPROFILE"]
69 if os.path.isdir(os.path.join(dir, "Application Data")):
70 dir = os.path.join(dir, "Application Data")
71
72 if ((dir is None) or (not os.path.isdir(dir))) and ("HOMEDRIVE" in os.environ) and ("HOMEPATH" in os.environ):
73 dir = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
74 if os.path.isdir(os.path.join(dir, "Application Data")):
75 dir = os.path.join(dir, "Application Data")
76
77 if (dir is None) or (not os.path.isdir(dir)):
78 dir = os.path.expanduser("~")
79
80 # On windows, add vendor and app name
81 dir = os.path.join(dir, vendor, appname)
82
83 # Mac
84 elif os.name == "mac": # ?? may not be entirely correct
85 dir = os.path.expanduser("~")
86 dir = os.path.join(dir, "Library", "Application Support")
87 dir = os.path.join(dir, vendor, appname)
88
89 # Unix/Linux/all others
90 if dir is None:
91 dir = os.path.expanduser("~")
92 dir = os.path.join(dir, "."+vendor, appname)
93
94 # Create vendor/appname folder if it doesn't exist
95 if not os.path.isdir(dir):
96 os.makedirs(dir)
97
98 return dir