annotate engine/extensions/fife_utils.py @ 273:815354ba295e

* Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS. * Made rio_de_hola save its settings in the proper location.
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 16 Jun 2009 14:24:26 +0000
parents 9a1529f9625e
children cf77afb273c4
rev   line source
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
1 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
2
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
3 __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
4
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
5 _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
6
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
7 """ 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
8
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
9 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
10 """ 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
11 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
12 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
13 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
14 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
15 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
16 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
17 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
18 """
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
19 ret = False
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
20 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
21 if m:
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
22 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
23 ret = True
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24 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
25
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 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
27 """ 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
28
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 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
30 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
31 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
32
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 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
34 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
35 """
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 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
37
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 # 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
39 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
40
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 # 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
42 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
43 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
44
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
45 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
46 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
47 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
48 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
49
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 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
51 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
52 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
53 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
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 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
56 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
57
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 # 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
59 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
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 # 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
62 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
63 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
64 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
65 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
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 # 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
68 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
69 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
70 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
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 # 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
73 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
74 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
75
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 print 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
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 return dir