annotate gamelib/data.py @ 536:1a224ba50edf

Correctly set the position of newly hatched chickens in buildings
author Neil Muller <drnlmuller@gmail.com>
date Fri, 27 Nov 2009 23:29:32 +0000
parents e12d99215b74
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
1 '''Simple data loader module.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3 Loads data files from the "data" directory shipped with a game.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5 Enhancing this to handle caching etc. is left as an exercise for the reader.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6 '''
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
8 import os
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
9
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
10 data_py = os.path.abspath(os.path.dirname(__file__))
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
11 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
18 '''Determine the path to a file in the data directory.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
22
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
23 def load(filename, mode='rb'):
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
24 '''Open a file in the data directory.
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
25
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
26 "mode" is passed as the second arg to open().
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
30 return open(os.path.join(data_dir, filename), mode)
e057e9483488 Added pyweek skellington.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
31