1 """ wrapper for ini-file-based settings"""
6 """ Reads and writes settings to a config files based on the INI format.
8 For example, a typical mEveMon config file (at '~/.mevemon/mevemon.cfg')will look like this:
20 # this is just a fake example, we don't store any general
22 super_cow_powers = True
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).
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()
32 def get_accounts(self):
33 """ Returns a dictionary containing uid:api_key pairs gathered from the config file
37 cfg_accounts = self.config['accounts'].values()
41 for account in cfg_accounts:
42 account_dict[account['uid']] = account['apikey']
46 def get_api_key(self, uid):
47 """ Returns the api key associated with the given uid.
50 api_key = self.get_accounts()[uid]
53 raise Exception("UID '%s' is not in settings")
55 def add_account(self, uid, api_key):
56 """ Adds the provided uid:api_key pair to the config file.
58 if 'accounts' not in self.config.sections:
59 self.config['accounts'] = {}
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
66 def remove_account(self, uid):
67 """ Removes the provided uid key from the config file
69 for key in self.config['accounts']:
70 if self.config['accounts'][key]['uid'] == uid:
71 del self.config['accounts'][key]
75 """ write out the settings into the config file """
76 if isinstance(self.cfg_file, str):
78 else: # cfg_file is a file-like object (StringIO, etc..)
79 self.config.write(self.cfg_file)
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.
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)