Fixed the issues with backup files that were the exact same as the original.

This commit is contained in:
Jeremy Fincher 2004-09-16 20:00:41 +00:00
parent b19a2bb051
commit 8fd64a6325
1 changed files with 48 additions and 31 deletions

View File

@ -528,6 +528,7 @@ class IrcChannelCreator(Creator):
class UsersDictionary(utils.IterableMap): class UsersDictionary(utils.IterableMap):
"""A simple serialized-to-file User Database.""" """A simple serialized-to-file User Database."""
def __init__(self): def __init__(self):
self.noFlush = False
self.filename = None self.filename = None
self.users = {} self.users = {}
self.nextId = 0 self.nextId = 0
@ -539,10 +540,16 @@ class UsersDictionary(utils.IterableMap):
self.filename = filename self.filename = filename
reader = unpreserve.Reader(IrcUserCreator, self) reader = unpreserve.Reader(IrcUserCreator, self)
try: try:
reader.readFile(filename) self.noFlush = True
except Exception, e: try:
log.error('Invalid user dictionary file, starting from scratch.') reader.readFile(filename)
log.error('Exact error: %s', utils.exnToString(e)) self.noFlush = False
self.flush()
except Exception, e:
log.error('Invalid user dictionary file, resetting to empty.')
log.error('Exact error: %s', utils.exnToString(e))
finally:
self.noFlush = False
def reload(self): def reload(self):
"""Reloads the database from its file.""" """Reloads the database from its file."""
@ -560,17 +567,18 @@ class UsersDictionary(utils.IterableMap):
def flush(self): def flush(self):
"""Flushes the database to its file.""" """Flushes the database to its file."""
if self.filename is not None: if not self.noFlush:
L = self.users.items() if self.filename is not None:
L.sort() L = self.users.items()
fd = utils.transactionalFile(self.filename) L.sort()
for (id, u) in L: fd = utils.transactionalFile(self.filename)
fd.write('user %s' % id) for (id, u) in L:
fd.write(os.linesep) fd.write('user %s' % id)
u.preserve(fd, indent=' ') fd.write(os.linesep)
fd.close() u.preserve(fd, indent=' ')
else: fd.close()
log.warning('UsersDictionary.flush called with no filename.') else:
log.warning('UsersDictionary.flush called with no filename.')
def close(self): def close(self):
self.flush() self.flush()
@ -682,7 +690,8 @@ class UsersDictionary(utils.IterableMap):
raise ValueError, s raise ValueError, s
self.invalidateCache(id) self.invalidateCache(id)
self.users[id] = user self.users[id] = user
self.flush() # This shouldn't happen; we flush automatically every once in awhile.
#self.flush()
def delUser(self, id): def delUser(self, id):
"""Removes a user from the database.""" """Removes a user from the database."""
@ -709,29 +718,37 @@ class UsersDictionary(utils.IterableMap):
class ChannelsDictionary(utils.IterableMap): class ChannelsDictionary(utils.IterableMap):
def __init__(self): def __init__(self):
self.noFlush = False
self.filename = None self.filename = None
self.channels = ircutils.IrcDict() self.channels = ircutils.IrcDict()
def open(self, filename): def open(self, filename):
self.filename = filename self.noFlush = True
reader = unpreserve.Reader(IrcChannelCreator, self)
try: try:
reader.readFile(filename) self.filename = filename
except Exception, e: reader = unpreserve.Reader(IrcChannelCreator, self)
log.error('Invalid channel database, starting from scratch.') try:
log.error('Exact error: %s', utils.exnToString(e)) reader.readFile(filename)
self.noFlush = False
self.flush()
except Exception, e:
log.error('Invalid channel database, resetting to empty.')
log.error('Exact error: %s', utils.exnToString(e))
finally:
self.noFlush = False
def flush(self): def flush(self):
"""Flushes the channel database to its file.""" """Flushes the channel database to its file."""
if self.filename is not None: if not self.noFlush:
fd = utils.transactionalFile(self.filename) if self.filename is not None:
for (channel, c) in self.channels.iteritems(): fd = utils.transactionalFile(self.filename)
fd.write('channel %s' % channel) for (channel, c) in self.channels.iteritems():
fd.write(os.linesep) fd.write('channel %s' % channel)
c.preserve(fd, indent=' ') fd.write(os.linesep)
fd.close() c.preserve(fd, indent=' ')
else: fd.close()
log.warning('ChannelsDictionary.flush without self.filename.') else:
log.warning('ChannelsDictionary.flush without self.filename.')
def close(self): def close(self):
self.flush() self.flush()