1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 __all__ = ['get_abs_data_filename']
24
25 import sys
26 import os
27
28
30 """Get the absolute path to the given file- or directory name in the current
31 running application's data directory.
32
33 @type path_parts: list
34 @param path_parts: The path parts that can be joined by os.path.join().
35 """
36 if basedirs is None:
37 basedirs = []
38
39 if isinstance(path_parts, str):
40 path_parts = [path_parts]
41
42 BASE_DIRS = basedirs + [
43 os.path.dirname(unicode(__file__, sys.getfilesystemencoding())),
44 os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding())),
45 ]
46
47
48 if 'XDG_DATA_HOME' in os.environ:
49 BASE_DIRS += [os.environ['XDG_DATA_HOME']]
50 if 'XDG_DATA_DIRS' in os.environ:
51 BASE_DIRS += os.environ['XDG_DATA_DIRS'].split(os.path.pathsep)
52
53
54 if 'RESOURCEPATH' in os.environ:
55 BASE_DIRS += os.environ['RESOURCEPATH'].split(os.path.pathsep)
56
57 DATA_DIRS = [
58 ["..", "share"],
59 ["share"],
60 ]
61
62 for basepath, data_dir in ((x, y) for x in BASE_DIRS for y in DATA_DIRS):
63 dir_and_filename = data_dir + path_parts
64 datafile = os.path.join(basepath or os.path.dirname(__file__), *dir_and_filename)
65 if os.path.exists(datafile):
66 return datafile
67 raise Exception('Could not find "%s"' % (os.path.join(*path_parts)))
68