refresh characters list after exiting settings
[mevemon] / package / src / file_settings.py
1 """ wrapper for ini-file-based settings"""
2 import configobj
3 import constants
4
5 class Settings:
6     """ Reads and writes settings to a config files based on the INI format.
7         
8         For example, a typical mEveMon config file (at '~/.mevemon/mevemon.cfg')will look like this:
9
10         [accounts]
11             [[account.<uid1>]]
12             uid = <uid1>
13             apikey = <apikey1>
14
15             [[account.<uid2>]]
16             uid = <uid2>
17             apikey = <apikey2>
18
19         [general]
20         # this is just a fake example, we don't store any general
21         # settings yet...
22         super_cow_powers = True
23
24         More information on the format/syntax of the config file can be found
25         on the ConfigObj site (http://www.voidspace.org.uk/python/configobj.html#the-config-file-format).
26     """
27     def __init__(self, cfg_file=constants.CONFIG_PATH):
28         self.cfg_file = cfg_file
29         self.config = configobj.ConfigObj(self.cfg_file)
30         self._convert_gconf_to_cfgfile()
31    
32     def get_accounts(self):
33         """ Returns a dictionary containing uid:api_key pairs gathered from the config file
34         """
35         account_dict = {}
36         try:
37             cfg_accounts = self.config['accounts'].values()
38         except KeyError:
39             return account_dict
40         
41         for account in cfg_accounts:
42             account_dict[account['uid']] = account['apikey']
43
44         return account_dict
45
46     def get_api_key(self, uid):
47         """ Returns the api key associated with the given uid.
48         """
49         try:
50             api_key = self.get_accounts()[uid]
51             return api_key
52         except KeyError:
53             raise Exception("UID '%s' is not in settings") 
54
55     def add_account(self, uid, api_key):
56         """ Adds the provided uid:api_key pair to the config file.
57         """
58         if 'accounts' not in self.config.sections:
59             self.config['accounts'] = {}
60
61         self.config['accounts']['account.%s' % uid] = {}
62         self.config['accounts']['account.%s' % uid]['uid'] = uid
63         self.config['accounts']['account.%s' % uid]['apikey'] = api_key
64         self.write()
65
66     def remove_account(self, uid):
67         """ Removes the provided uid key from the config file
68         """
69         for key in self.config['accounts']:
70             if self.config['accounts'][key]['uid'] == uid:
71                 del self.config['accounts'][key]
72                 self.write()
73         
74     def write(self):
75         """ write out the settings into the config file """
76         if isinstance(self.cfg_file, str):
77             self.config.write()
78         else: # cfg_file is a file-like object (StringIO, etc..)
79             self.config.write(self.cfg_file)
80
81     def _convert_gconf_to_cfgfile(self):
82         """ A temporary method to convert from the old 0.4-9 gconf-based
83             settings to the new file-based settings.
84         """
85         import gconf_settings
86         gsettings = gconf_settings.Settings()
87         for uid, apikey in gsettings.get_accounts().items():
88             self.add_account(uid, apikey)
89             gsettings.remove_account(uid)