Normalise file paths in data.py
[erdslangetjie.git] / erdslangetjie / data.py
1 '''Simple data loader module.
2
3 Loads data files from the "data" directory shipped with a game.
4
5 Enhancing this to handle caching etc. is left as an exercise for the reader.
6
7 Note that pyglet users should probably just add the data directory to the
8 pyglet.resource search path.
9 '''
10
11 import os
12
13 data_py = os.path.abspath(os.path.dirname(__file__))
14 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
15
16
17 def filepath(filename):
18     '''Determine the path to a file in the data directory.
19     '''
20     filename = os.path.join(*filename.split('/'))
21     return os.path.join(data_dir, filename)
22
23
24 def load(filename, mode='rb'):
25     '''Open a file in the data directory.
26
27     "mode" is passed as the second arg to open().
28     '''
29     return open(filepath(filename), mode)