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
29 """Get the absolute path to the given file- or directory name in the current
30 running application's data directory.
31
32 @type path_parts: list
33 @param path_parts: The path parts that can be joined by os.path.join().
34 """
35 if basedirs is None:
36 basedirs = []
37
38 if isinstance(path_parts, str):
39 path_parts = [path_parts]
40
41 BASE_DIRS = basedirs + [
42 os.path.dirname(unicode(__file__, sys.getfilesystemencoding())),
43 os.path.dirname(unicode(sys.executable, sys.getfilesystemencoding()))
44 ]
45
46
47 if 'XDG_DATA_HOME' in os.environ:
48 BASE_DIRS += [os.environ['XDG_DATA_HOME']]
49 if 'XDG_DATA_DIRS' in os.environ:
50 BASE_DIRS += os.environ['XDG_DATA_DIRS'].split(os.path.pathsep)
51
52
53 if 'RESOURCEPATH' in os.environ:
54 BASE_DIRS += os.environ['RESOURCEPATH'].split(os.path.pathsep)
55
56 DATA_DIRS = [
57 ["..", "share"],
58 ["share"]
59 ]
60
61 for basepath, data_dir in ((x, y) for x in BASE_DIRS for y in DATA_DIRS):
62 dir_and_filename = data_dir + path_parts
63 datafile = os.path.join(basepath or os.path.dirname(__file__), *dir_and_filename)
64 if os.path.exists(datafile):
65 return datafile
66 raise Exception('Could not find "%s"' % (os.path.join(*path_parts)))
67