annotate gamelib/data.py @ 0:d0de8832774b

Import skellington-1.9 into the repo
author Neil Muller <drnlmuller+bitbucket@gmail.com>
date Sat, 05 May 2012 13:52:29 +0200
parents
children c90a6586cd66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
1 '''Simple data loader module.
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
2
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
3 Loads data files from the "data" directory shipped with a game.
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
4
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
5 Enhancing this to handle caching etc. is left as an exercise for the reader.
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
6 '''
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
7
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
8 import os
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
9
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
10 data_py = os.path.abspath(os.path.dirname(__file__))
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
11 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
12
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
13 def filepath(filename):
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
14 '''Determine the path to a file in the data directory.
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
15 '''
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
16 return os.path.join(data_dir, filename)
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
17
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
18 def load(filename, mode='rb'):
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
19 '''Open a file in the data directory.
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
20
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
21 "mode" is passed as the second arg to open().
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
22 '''
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
23 return open(os.path.join(data_dir, filename), mode)
d0de8832774b Import skellington-1.9 into the repo
Neil Muller <drnlmuller+bitbucket@gmail.com>
parents:
diff changeset
24