Mercurial > rinkhals
annotate gamelib/data.py @ 447:f04a2490c35f
The sub-toolbar rewrite, the finally not crashing version
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 19:58:49 +0000 |
parents | e12d99215b74 |
children |
rev | line source |
---|---|
2 | 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 | |
8 import os | |
9 | |
10 data_py = os.path.abspath(os.path.dirname(__file__)) | |
11 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data')) | |
12 | |
303
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
89
diff
changeset
|
13 def unix_to_local(filename): |
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
89
diff
changeset
|
14 '''Convert a relative unix / http filename to a local one.''' |
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
89
diff
changeset
|
15 return os.path.join(*filename.split("/")) |
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
89
diff
changeset
|
16 |
89
c0455e6c99f4
Support multiple arguments using os.path.join - better to pass filepath('a', 'b') than filepath('a/b')
David Fraser <davidf@sjsoft.com>
parents:
42
diff
changeset
|
17 def filepath(*filenames): |
2 | 18 '''Determine the path to a file in the data directory. |
19 ''' | |
303
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
89
diff
changeset
|
20 os_filenames = [unix_to_local(f) for f in filenames] |
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
89
diff
changeset
|
21 return os.path.join(data_dir, *os_filenames) |
2 | 22 |
23 def load(filename, mode='rb'): | |
24 '''Open a file in the data directory. | |
25 | |
26 "mode" is passed as the second arg to open(). | |
27 ''' | |
42
498e4732bc1f
Auto-convert unix / http path separators to platform appropriate ones -- we should now just be able to use slash as the path separator through-out our own code.
Simon Cross <hodgestar@gmail.com>
parents:
2
diff
changeset
|
28 # convert unix path separator to platform appropriate one |
303
e12d99215b74
Fix up data module unix-to-local path fixing. Fix similar problems in tile importing.
Simon Cross <hodgestar@gmail.com>
parents:
89
diff
changeset
|
29 filename = unix_to_local(filename) |
2 | 30 return open(os.path.join(data_dir, filename), mode) |
31 |