Properly update existing episodes (bug 211)
[gpodder.git] / src / gpodder / dumbshelve.py
blob059d50c7938ec93a5888907b464abb4e7d1d6377
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2008 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # dumbshelve.py - Temporary implementation of a shelve replacement
21 # 2008-02-27 Thomas Perl <thpinfo.com>
23 from gpodder.liblogger import log
25 import UserDict
26 import cPickle
27 import os.path
29 class DumbShelve(UserDict.UserDict):
30 """
31 Simply tries to act like a "shelve" object..
32 """
33 def __init__(self, filename=None):
34 UserDict.UserDict.__init__(self)
35 self.__filename = filename
36 self.__dirty = False
38 def sync(self, filename=None):
39 if not self.__dirty:
40 return True
42 if filename is not None:
43 self.__filename = filename
44 try:
45 self.__dirty = False
46 cPickle.dump(self, open(self.__filename, 'w'))
47 return True
48 except:
49 log('Cannot pickle me to %s', self.__filename, sender=self, traceback=True)
50 return False
52 def __setitem__(self, key, item):
53 self.__dirty = True
54 UserDict.UserDict.__setitem__(self, key, item)
56 def __delitem__(self, key):
57 self.__dirty = True
58 UserDict.UserDict.__delitem__(self, key)
60 def open_shelve(filename):
61 if not os.path.exists(filename):
62 return DumbShelve(filename)
63 else:
64 try:
65 return cPickle.load(open(filename, 'r'))
66 except:
67 log('Error loading %s. Creating new DumbShelve.', filename, traceback=True)
68 return DumbShelve(filename)