annotate engine/extensions/fife_utils.py @ 374:1115f7cae9a3

Editor: * The editor will now force filenames to be lowercase, as VFS does not like uppercase path names. * If a map filename does not have a .xml extension on save, it will be automatically added. * Log modules set to "all" by default. Only new users will be affected by this without having to modify or remove their configuration file. * Log level set to LOGLEVEL_WARN
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 21 Nov 2009 13:11:56 +0000
parents dfd48d49c044
children 91e352f81f63
rev   line source
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
1 # -*- coding: utf-8 -*-
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
2
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
3 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
4 # Copyright (C) 2005-2009 by the FIFE team
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
5 # http://www.fifengine.de
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
6 # This file is part of FIFE.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
7 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
8 # FIFE is free software; you can redistribute it and/or
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
9 # modify it under the terms of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
10 # License as published by the Free Software Foundation; either
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
11 # version 2.1 of the License, or (at your option) any later version.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
12 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
13 # This library is distributed in the hope that it will be useful,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
16 # Lesser General Public License for more details.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
17 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
18 # You should have received a copy of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
19 # License along with this library; if not, write to the
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
20 # Free Software Foundation, Inc.,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
22 # ####################################################################
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
23 import fife, re, sys, os
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
25 __all__ = ['is_fife_exc', 'getUserDataDirectory']
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
26
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
27 _exc_re = re.compile(r'_\[(\w+)\]_')
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
29 """ This file contains some functions that may be useful """
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
30
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31 def is_fife_exc(type, original_exc):
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
32 """ Checks if an exception is of given type.
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
33 Example:
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
34 try:
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
35 obj = self.model.createObject(str(id), str(nspace), parent)
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
36 except RuntimeError, e:
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
37 if is_fife_exc(fife.NameClash, e):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
38 raise NameClash('Tried to create already existing object, ignoring')
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
39 raise
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
40 """
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 ret = False
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 m = _exc_re.search(str(original_exc))
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 if m:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 if m.group(1) == type('').getTypeStr():
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 ret = True
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46 return ret
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
47
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
48 def getUserDataDirectory(vendor, appname):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
49 """ Gets the proper location to save configuration and data files, depending on depending on OS.
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
50
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
51 Windows: %APPDATA%\vendor\appname
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
52 Mac: ~/Library/Application Support/vendor/appname
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
53 Linux/Unix/Other: ~/.vendor/appname
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
54
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
55 See:
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
56 Brian Vanderburg II @ http://mail.python.org/pipermail/python-list/2008-May/660779.html
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
57 """
274
cf77afb273c4 Fixed a small bug in getUserDataDirectory
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 273
diff changeset
58 dir = None
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
59
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
60 # WINDOWS
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
61 if os.name == "nt":
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
62
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
63 # Try env APPDATA or USERPROFILE or HOMEDRIVE/HOMEPATH
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
64 if "APPDATA" in os.environ:
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
65 dir = os.environ["APPDATA"]
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
66
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
67 if ((dir is None) or (not os.path.isdir(dir))) and ("USERPROFILE" in os.environ):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
68 dir = os.environ["USERPROFILE"]
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
69 if os.path.isdir(os.path.join(dir, "Application Data")):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
70 dir = os.path.join(dir, "Application Data")
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
71
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
72 if ((dir is None) or (not os.path.isdir(dir))) and ("HOMEDRIVE" in os.environ) and ("HOMEPATH" in os.environ):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
73 dir = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
74 if os.path.isdir(os.path.join(dir, "Application Data")):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
75 dir = os.path.join(dir, "Application Data")
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
76
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
77 if (dir is None) or (not os.path.isdir(dir)):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
78 dir = os.path.expanduser("~")
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
79
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
80 # On windows, add vendor and app name
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
81 dir = os.path.join(dir, vendor, appname)
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
82
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
83 # Mac
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
84 elif os.name == "mac": # ?? may not be entirely correct
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
85 dir = os.path.expanduser("~")
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
86 dir = os.path.join(dir, "Library", "Application Support")
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
87 dir = os.path.join(dir, vendor, appname)
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
88
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
89 # Unix/Linux/all others
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
90 if dir is None:
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
91 dir = os.path.expanduser("~")
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
92 dir = os.path.join(dir, "."+vendor, appname)
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
93
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
94 # Create vendor/appname folder if it doesn't exist
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
95 if not os.path.isdir(dir):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
96 os.makedirs(dir)
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
97
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 129
diff changeset
98 return dir