annotate engine/extensions/fife_utils.py @ 350:7ea98190a75e

Allow objects with an __call__ attribute used as event callbacks. Tested in pychan demo.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Fri, 04 Sep 2009 06:40:50 +0000
parents 48c99636453e
children dfd48d49c044
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 #!/usr/bin/env python
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
2 # -*- coding: utf-8 -*-
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 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
5 # 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
6 # http://www.fifengine.de
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
7 # 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
8 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
9 # 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
10 # 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
11 # 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
12 # 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
13 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
14 # 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
15 # 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
16 # 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
17 # 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
18 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
19 # 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
20 # 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
21 # Free Software Foundation, Inc.,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 274
diff changeset
22 # 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
23 # ####################################################################
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
24 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
25
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
26 __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
27
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28 _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
29
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
30 """ 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
31
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32 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
33 """ 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 """
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 ret = False
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 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
44 if m:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 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
46 ret = True
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47 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
48
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 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
50 """ 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
51
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 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
53 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
54 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
55
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 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
57 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
58 """
274
cf77afb273c4 Fixed a small bug in getUserDataDirectory
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 273
diff changeset
59 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
60
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 # 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
62 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
63
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 # 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
65 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
66 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
67
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 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
69 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
70 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
71 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
72
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 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
74 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
75 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
76 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
77
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 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
79 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
80
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 # 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
82 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
83
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 # 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
85 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
86 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
87 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
88 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
89
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 # 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
91 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
92 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
93 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
94
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 # 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
96 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
97 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
98
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
99 return dir