Save game.
[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 parse_args(args):
13     '''
14     Parse arguments and store them in the options dictionary.
15
16     Note: If you add arguments, you need to add an appropriate default to the
17     DEFAULTS dict.
18     '''
19     options.update(DEFAULTS)
20
21     options.debug = 'DEBUG' in os.environ
22
23     parser = optparse.OptionParser()
24     parser.add_option('--no-sound',
25                       dest='sound', action='store_false', default=True,
26                       help='Disable all sound, including music')
27
28     parser.add_option('--no-music',
29                       dest='music', action='store_false', default=True,
30                       help='Disable music (but not sound)')
31
32     parser.add_option("--save-location", default=_get_default_save_location(),
33                       dest="save_location", help="Saved game location")
34
35     opts, _ = parser.parse_args(args)
36
37     for k in DEFAULTS:
38         if getattr(opts, k, None) is not None:
39             options[k] = getattr(opts, k)
40     options['save_location'] = opts.save_location
41
42
43 def _get_default_save_location():
44     """Return a default save game location."""
45     app = "naja"
46     if sys.platform.startswith("win"):
47         if "APPDATA" in os.environ:
48             return os.path.join(os.environ["APPDATA"], app)
49         return os.path.join(os.path.expanduser("~"), "." + app)
50     elif 'XDG_DATA_HOME' in os.environ:
51         return os.path.join(os.environ["XDG_DATA_HOME"], app)
52     return os.path.join(os.path.expanduser("~"), ".local", "share", app)