Mercurial > touhou
comparison pytouhou/options.py @ 568:e7a4731a278b
Add a GTK+ main menu, mimicking the original EoSD one.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 01 Jul 2014 23:18:15 +0200 |
parents | b2269b9c6119 |
children | 0768122da817 |
comparison
equal
deleted
inserted
replaced
567:b2269b9c6119 | 568:e7a4731a278b |
---|---|
13 ## | 13 ## |
14 | 14 |
15 import os | 15 import os |
16 from ConfigParser import RawConfigParser, NoOptionError | 16 from ConfigParser import RawConfigParser, NoOptionError |
17 | 17 |
18 from pytouhou.utils.xdg import load_config_paths | 18 from pytouhou.utils.xdg import load_config_paths, save_config_path |
19 | 19 |
20 | 20 |
21 class Options(object): | 21 class Options(object): |
22 def __init__(self, name, defaults): | 22 def __init__(self, name, defaults): |
23 load_paths = list(reversed([os.path.join(directory, '%s.cfg' % name) | 23 load_paths = list(reversed([os.path.join(directory, '%s.cfg' % name) |
24 for directory | 24 for directory |
25 in load_config_paths(name)])) | 25 in load_config_paths(name)])) |
26 self.save_path = os.path.join(save_config_path(name), '%s.cfg' % name) | |
26 | 27 |
27 self.config = RawConfigParser(defaults) | 28 self.config = RawConfigParser(defaults) |
28 self.paths = self.config.read(load_paths) | 29 self.paths = self.config.read(load_paths) |
29 self.section = name if self.config.has_section(name) else 'DEFAULT' | 30 self.section = name if self.config.has_section(name) else 'DEFAULT' |
30 | 31 |
31 def get(self, option): | 32 def get(self, option): |
32 try: | 33 try: |
33 return self.config.get(self.section, option) | 34 return self.config.get(self.section, option) |
34 except NoOptionError: | 35 except NoOptionError: |
35 return None | 36 return None |
37 | |
38 def set(self, option, value): | |
39 if value is not None: | |
40 self.config.set(self.section, option, value) | |
41 else: | |
42 self.config.remove_option(self.section, option) | |
43 | |
44 defaults = self.config._defaults | |
45 self.config._defaults = None | |
46 with open(self.save_path, 'w') as save_file: | |
47 self.config.write(save_file) | |
48 self.config._defaults = defaults | |
36 | 49 |
37 | 50 |
38 def patch_argument_parser(): | 51 def patch_argument_parser(): |
39 from argparse import ArgumentParser, _ActionsContainer | 52 from argparse import ArgumentParser, _ActionsContainer |
40 | 53 |
103 | 116 |
104 parser.add_argument('data', metavar='DAT', nargs='*', help='Game’s data files') | 117 parser.add_argument('data', metavar='DAT', nargs='*', help='Game’s data files') |
105 parser.add_argument('-p', '--path', metavar='DIRECTORY', help='Game directory path.') | 118 parser.add_argument('-p', '--path', metavar='DIRECTORY', help='Game directory path.') |
106 parser.add_argument('--debug', action='store_true', help='Set unlimited continues, and perhaps other debug features.') | 119 parser.add_argument('--debug', action='store_true', help='Set unlimited continues, and perhaps other debug features.') |
107 parser.add_argument('--verbosity', metavar='VERBOSITY', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Select the wanted logging level.') | 120 parser.add_argument('--verbosity', metavar='VERBOSITY', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help='Select the wanted logging level.') |
121 parser.add_argument('--no-menu', action='store_true', help='Disable the menu.') | |
108 | 122 |
109 game_group = parser.add_argument_group('Game options') | 123 game_group = parser.add_argument_group('Game options') |
110 game_group.add_argument('-s', '--stage', metavar='STAGE', type=int, help='Stage, 1 to 7 (Extra), nothing means story mode.') | 124 game_group.add_argument('-s', '--stage', metavar='STAGE', type=int, help='Stage, 1 to 7 (Extra), nothing means story mode.') |
111 game_group.add_argument('-r', '--rank', metavar='RANK', type=int, help='Rank, from 0 (Easy, default) to 3 (Lunatic).') | 125 game_group.add_argument('-r', '--rank', metavar='RANK', type=int, help='Rank, from 0 (Easy, default) to 3 (Lunatic).') |
112 game_group.add_argument('-c', '--character', metavar='CHARACTER', type=int, help='Select the character to use, from 0 (ReimuA, default) to 3 (MarisaB).') | 126 game_group.add_argument('-c', '--character', metavar='CHARACTER', type=int, help='Select the character to use, from 0 (ReimuA, default) to 3 (MarisaB).') |