Add save-game loading.
[naja.git] / naja / options.py
1 import optparse
2 import os
3 import sys
4
5 from naja.attrdict import AttrDict
6 from naja.constants import DEFAULTS
7
8
9 options = AttrDict()
10
11
12 def check_min_max(option, value, min_value, max_value):
13     '''
14     Check value lies between min and max and raise OptionValueError if it does
15     not.
16     '''
17     if not (min_value <= value <= max_value):
18         raise optparse.OptionValueError(
19             "Value of %s should be between %s and %s but got: %r"
20             % (option.dest, min, max, value))
21
22
23 def load_game(option, opt_str, value, parser):
24     '''
25     Load a save game and store it in parser.values.game_state.
26     '''
27     check_min_max(option, value, 0, 7)
28     # madness takes its toll ...
29     options.save_location = parser.values.save_location
30     from naja.scenes.load_save import SaveGameSlot
31     state = SaveGameSlot(value).load()
32     if state is None:
33         raise optparse.OptionValueError(
34             "Could not load game from slot %s" % (value,))
35     parser.values.game_state = state
36
37
38 def load_deck(option, opt_str, value, parser):
39     '''
40     Create a new game for a specific deck  and store it in
41     parser.values.game_state.
42     '''
43     raise optparse.OptionalValueError(
44         "Deck loading not implemented.")
45
46
47 def parse_args(args):
48     '''
49     Parse arguments and store them in the options dictionary.
50
51     Note: If you add arguments, you need to add an appropriate default to the
52     DEFAULTS dict.
53     '''
54     options.update(DEFAULTS)
55
56     options.debug = 'DEBUG' in os.environ
57
58     parser = optparse.OptionParser()
59     parser.add_option('--no-sound',
60                       dest='sound', action='store_false', default=True,
61                       help='Disable all sound, including music')
62
63     parser.add_option('--no-music',
64                       dest='music', action='store_false', default=True,
65                       help='Disable music (but not sound)')
66
67     parser.add_option("--save-location", default=_get_default_save_location(),
68                       dest="save_location", help="Saved game location")
69
70     if options.debug:
71         parser.add_option('--initial-bits', type=int,
72                           help='Initial player bits')
73         parser.add_option('--cheat-enabled', default=False,
74                           action='store_true',
75                           help='For those too lazy to type the KONAMI code')
76         parser.add_option('--deck', default=None, action="callback",
77                           callback=load_deck,
78                           help='Start with a new game for a specific deck')
79         parser.add_option('--load', default=None, type=int, action="callback",
80                           callback=load_game,
81                           help='Start with a specific save game loaded (0-7)')
82
83     opts, _ = parser.parse_args(args)
84
85     for k in DEFAULTS:
86         if getattr(opts, k, None) is not None:
87             options[k] = getattr(opts, k)
88
89
90 def _get_default_save_location():
91     """Return a default save game location."""
92     app = "naja"
93     if sys.platform.startswith("win"):
94         if "APPDATA" in os.environ:
95             return os.path.join(os.environ["APPDATA"], app)
96         return os.path.join(os.path.expanduser("~"), "." + app)
97     elif 'XDG_DATA_HOME' in os.environ:
98         return os.path.join(os.environ["XDG_DATA_HOME"], app)
99     return os.path.join(os.path.expanduser("~"), ".local", "share", app)