annotate gamelib/config.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 8897a436a8cb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
1 # level.py
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
3 from ConfigParser import RawConfigParser
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4 from optparse import OptionParser
486
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
5 import sys
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
6 import os
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
8 class Config(object):
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
9 """Container for various global configuration knobs and levers."""
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
10
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
11 valid_options = {
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
12 'sound': {'type': 'boolean', 'default': 'true'},
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
13 'level_name': {'type': 'string', 'default': 'two_weeks'},
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
14 }
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
15
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
16 def configure(self, params=None):
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17 self._config = RawConfigParser(dict(
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
18 [(k, v['default']) for k, v in self.valid_options.items() if 'default' in v]
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
19 ))
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
20 self._config.add_section('Options')
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
21 self._set_up_params(params)
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
22 self._config.read(self.config_filename)
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
23 self._process_params()
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
24
486
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
25 def ensure_dir_exists(self, folder):
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
26 """Ensure the given folder exists."""
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
27 if os.path.exists(folder):
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
28 assert os.path.isdir(folder)
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
29 else:
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
30 os.makedirs(folder)
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
31
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
32 def _set_up_params(self, params):
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
33 parser = OptionParser()
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
34 parser.add_option("-c", "--config", metavar="FILE", dest="config_filename",
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
35 help="read configuration from FILE")
486
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
36 parser.add_option("-p", "--prefs-folder", metavar="PREFS_FOLDER", dest="prefs_folder",
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
37 help="store preferences and save games in PREFS_FOLDER")
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
38 parser.add_option("-l", "--level", metavar="LEVEL", dest="level_name",
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
39 help="select level LEVEL")
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
40 parser.add_option("--sound", action="store_const", const="on", dest="sound",
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
41 help="enable sound")
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
42 parser.add_option("--no-sound", action="store_const", const="off", dest="sound",
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
43 help="disable sound")
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
44 (self._opts, _) = parser.parse_args(params or [])
486
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
45 self.prefs_folder = self._opts.prefs_folder or self._default_prefs_dir()
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
46 self.ensure_dir_exists(self.prefs_folder)
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
47 self.save_folder = os.path.join(self.prefs_folder, "savegames")
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
48 self.ensure_dir_exists(self.save_folder)
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
49 self.config_filename = self._opts.config_filename or os.path.join(self.prefs_folder, "config.ini")
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
50
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
51 def _default_prefs_dir(self):
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
52 """Return a default preference folder name."""
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
53 app = "foxassault"
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
54 if sys.platform.startswith("win") and "APPDATA" in os.environ:
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
55 return os.path.join(os.environ["APPDATA"], app)
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
56 else:
8897a436a8cb Factor out save game logic and (new, simplified) dialogs into their own module. Add preferences folder to concept to config. Save games under preferences folder.
Simon Cross <hodgestar@gmail.com>
parents: 411
diff changeset
57 return os.path.join(os.path.expanduser("~"), ".%s" % app)
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
58
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
59 def _process_params(self):
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
60 for name in self.valid_options:
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
61 opt = getattr(self._opts, name)
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
62 if opt is not None:
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
63 self._config.set('Options', name, opt)
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
64
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
65 def __getattr__(self, name):
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
66 if name not in self.valid_options:
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
67 raise AttributeError(name)
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
68 get_methods = {
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
69 'string': lambda n: self._config.get('Options', n),
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
70 'boolean': lambda n: self._config.getboolean('Options', n),
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
71 }
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
72 return get_methods[self.valid_options[name].get('type', 'string')](name)
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
73
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
74 # Here's a global variable. Don't try this at home, kids!
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
75 config = Config()