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
20 from ConfigParser
import MissingSectionHeaderError
22 from dicthelper
import DictHelper
, CannotSaveError
, CorruptedConfiguration
23 from storagehandler
import StorageHandler
25 class FileHandler(StorageHandler
):
27 self
.__file
_path
= os
.path
.expanduser("~/.config/cgmail/accounts.ini")
28 if not os
.path
.exists(self
.__file
_path
):
30 os
.makedirs(os
.path
.expanduser("~/.config/cgmail"))
34 self
.__data
= open(self
.__file
_path
).read()
35 self
.__lines
= self
.__data
.split('\n')
41 if self
.__count
< len(self
.__lines
):
42 line
= self
.__lines
[self
.__count
]
50 def write(self
, data
):
54 def get_new_writer(self
):
55 self
.__f
= open(self
.__file
_path
, "w")
56 os
.chmod(self
.__file
_path
, 0600)
63 class FileDictHelper(DictHelper
):
67 DictHelper
.__init
__(self
, fh
)
68 except MissingSectionHeaderError
:
69 fh
= fh
.get_new_writer()
71 raise CorruptedConfiguration
76 if __name__
== "__main__":
79 f
.add_dict({"user": "prova"})