Ready for mentors
[cgmail.git] / cGmail / lib / dicthelper.py
blobf3cee5d8b187541832d412e9cd532e27a7415401
2 # Copyright (C) 2007 Marco Ferragina <marco.ferragina@gmail.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 from iniman import IniMan
20 class NoSuchDictException(Exception): pass
21 class CannotSaveError(Exception): pass
22 class CorruptedConfiguration(Exception): pass
24 class DictHelper(IniMan):
25 def __init__(self, handler):
26 IniMan.__init__(self, handler)
27 self.__dicts = self.__retrieve_dicts()
29 def __retrieve_dicts(self):
30 dicts = []
31 sections = self.get_sections()
32 for sec in sections:
33 dic = self.get_by_section(sec)
34 dic["id"] = sec
35 dicts.append(dic)
36 return dicts
38 def get_dicts(self):
39 self.__dicts = []
40 self.__dicts = self.__retrieve_dicts()
41 return self.__dicts
43 def remove_dict(self, id):
44 dic = None
45 for tmp in self.__dicts:
46 if tmp["id"] == id:
47 dic = tmp
48 if dic is None:
49 raise NoSuchDictException
50 for key, value in dic.iteritems():
51 self.remove(id + "." + key)
53 self.__retrieve_dicts()
55 def add_dict(self, dic, force_id = None):
56 if force_id is None:
57 sections = self.get_sections()
58 maxid = 0
59 for sec in sections:
60 if int(sec) > maxid:
61 maxid = int(sec)
62 id = maxid + 1
63 else:
64 id = force_id
65 id = str(id)
66 self.__dicts.append(dic)
67 for key, value in dic.iteritems():
68 self.add(id + "." + key, value)
70 return id
72 def reset(self):
73 sections = self.get_sections()
74 for sect in sections:
75 self.remove_dict(sect)