73129490cf7903e4796543968f13248495c97353
[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 load_game(parser, slot_num):
13     '''
14     Load a save game and store it in parser.values.game_state.
15     '''
16     from naja.scenes.load_save import SaveGameSlot
17     if not (0 <= slot_num <= 7):
18         parser.error("--load accepts a slot number from 0 to 7.")
19     state = SaveGameSlot(slot_num).load()
20     if state is None:
21         raise parser.error(
22             "Could not load game from slot %s" % (slot_num,))
23     options.game_state = state
24
25
26 def load_deck(parser, deck):
27     '''
28     Create a new game for a specific deck  and store it in
29     parser.values.game_state.
30     '''
31     from naja.gamestate import GameState
32     try:
33         state = GameState.new(deck=deck, max_health=4, wins_required=4)
34     except:
35         if options.debug:
36             raise
37         parser.error("Could not load deck %r" % (deck,))
38     options.game_state = state
39
40
41 def parse_args(args):
42     '''
43     Parse arguments and store them in the options dictionary.
44
45     Note: If you add arguments, you need to add an appropriate default to the
46     DEFAULTS dict.
47     '''
48     options.update(DEFAULTS)
49
50     options.debug = 'DEBUG' in os.environ
51
52     parser = optparse.OptionParser()
53     parser.add_option('--no-sound',
54                       dest='sound', action='store_false', default=True,
55                       help='Disable all sound, including music')
56
57     parser.add_option('--no-music',
58                       dest='music', action='store_false', default=True,
59                       help='Disable music (but not sound)')
60
61     parser.add_option("--save-location", default=_get_default_save_location(),
62                       dest="save_location", help="Saved game location")
63
64     parser.add_option('--deck', default=None,
65                       help='Start with a new game for a specific deck'
66                       ' (bypassing the menu).')
67
68     parser.add_option('--load', default=None, type=int,
69                       help='Start with a specific save game loaded'
70                       ' (bypassing the menu). Slot values are 0-7.')
71
72     if options.debug:
73         parser.add_option('--initial-bits', type=int,
74                           help='Initial player bits')
75         parser.add_option('--cheat-enabled', default=False,
76                           action='store_true',
77                           help='For those too lazy to type the KONAMI code')
78
79     opts, _ = parser.parse_args(args)
80
81     for k in DEFAULTS:
82         if getattr(opts, k, None) is not None:
83             options[k] = getattr(opts, k)
84
85     if opts.load is not None:
86         load_game(parser, opts.load)
87     if opts.deck is not None:
88         load_deck(parser, opts.deck)
89
90
91 def _get_default_save_location():
92     """Return a default save game location."""
93     app = "naja"
94     if sys.platform.startswith("win"):
95         if "APPDATA" in os.environ:
96             return os.path.join(os.environ["APPDATA"], app)
97         return os.path.join(os.path.expanduser("~"), "." + app)
98     elif 'XDG_DATA_HOME' in os.environ:
99         return os.path.join(os.environ["XDG_DATA_HOME"], app)
100     return os.path.join(os.path.expanduser("~"), ".local", "share", app)