Removed legacy key support, added custom key support
[mevemon] / package / src / file_settings.py
1 """ wrapper for ini-file-based settings"""
2 import os
3 import configobj
4 import constants
5
6 class Settings:
7     """ Reads and writes settings to a config files based on the INI format.
8         
9         For example, a typical mEveMon config file (at '~/.mevemon/mevemon.cfg')will look like this:
10
11         [accounts]
12             [[account.<key_id1>]]
13             key_id = <key_id1>
14             ver_code = <ver_code1>
15
16             [[account.<key_id2>]]
17             key_id = <key_id2>
18             ver_code = <ver_code2>
19
20         [general]
21         # this is just a fake example, we don't store any general
22         # settings yet...
23         super_cow_powers = True
24
25         More information on the format/syntax of the config file can be found
26         on the ConfigObj site (http://www.voidspace.org.uk/python/configobj.html#the-config-file-format).
27     """
28     def __init__(self, cfg_file=constants.CONFIG_PATH):
29         self.cfg_file = cfg_file
30         self._convert_gconf_to_cfgfile()
31         self._detect_and_backup_old_cfg()
32         self.config = configobj.ConfigObj(self.cfg_file)
33         
34     def get_accounts(self):
35         """ Returns a dictionary containing key_id:ver_code pairs gathered from the config file
36         """
37         account_dict = {}
38         try:
39             cfg_accounts = self.config['accounts'].values()
40         except KeyError:
41             return account_dict
42         
43         for account in cfg_accounts:
44             account_dict[account['key_id']] = account['ver_code']
45
46         return account_dict
47
48     def get_ver_code(self, key_id):
49         """ Returns the verification code associated with the given key_id.
50         """
51         try:
52             ver_code = self.get_accounts()[key_id]
53             return ver_code
54         except KeyError:
55             raise Exception("KEY_ID '%s' is not in settings") 
56
57     def add_account(self, key_id, ver_code):
58         """ Adds the provided key_id:ver_code pair to the config file.
59         """
60         if 'accounts' not in self.config.sections:
61             self.config['accounts'] = {}
62
63         self.config['accounts']['account.%s' % key_id] = {}
64         self.config['accounts']['account.%s' % key_id]['key_id'] = key_id
65         self.config['accounts']['account.%s' % key_id]['ver_code'] = ver_code
66         self.write()
67
68     def remove_account(self, key_id):
69         """ Removes the provided key_id key from the config file
70         """
71         for key in self.config['accounts']:
72             if self.config['accounts'][key]['key_id'] == key_id:
73                 del self.config['accounts'][key]
74                 self.write()
75         
76     def write(self):
77         """ write out the settings into the config file """
78         if isinstance(self.cfg_file, str):
79             self.config.write()
80         else: # cfg_file is a file-like object (StringIO, etc..)
81             self.config.write(self.cfg_file)
82
83     def _convert_gconf_to_cfgfile(self):
84         """ A temporary method to convert from the old 0.4-9 gconf-based
85             settings to the new file-based settings.
86         """
87         import gconf_settings
88         gsettings = gconf_settings.Settings()
89         for key_id, ver_code in gsettings.get_accounts().items():
90             self.add_account(key_id, ver_code)
91             gsettings.remove_account(key_id)
92
93     def _detect_and_backup_old_cfg(self):
94         """ Searches the config file for the string 'apikey', which
95         would only be present in the old legacy config file. If it's
96         found, it backs up the file and we start over.
97         """
98         try:
99             temp = open(self.cfg_file, "r")
100         except IOError:
101             # if it's not here, forget about it - mission accomplished
102             return
103         config_contents = temp.read()
104         temp.close()
105         if config_contents.find('apikey') > 0:
106             # move the file then create a new one
107             os.rename(self.cfg_file, self.cfg_file + '.old')
108         else:
109             # we've got an updated cfg file
110             pass
111