Fix #187 set a 5 second timer to save the config file after a config

value has been changed.
This commit is contained in:
Andrew Resch 2008-09-25 02:03:58 +00:00
parent e32ce5b265
commit b47c4ad760
1 changed files with 13 additions and 0 deletions

View File

@ -65,6 +65,10 @@ class Config:
# Save # Save
self.save() self.save()
# This will get set with a gobject.timeout_add whenever a config option
# is set.
self.save_timer = None
def __del__(self): def __del__(self):
self.save() self.save()
@ -99,16 +103,20 @@ class Config:
pkl_file.close() pkl_file.close()
if filedump == self.config: if filedump == self.config:
# The config has not changed so lets just return # The config has not changed so lets just return
self.save_timer = None
return return
except (EOFError, IOError): except (EOFError, IOError):
log.warning("IOError: Unable to open file: '%s'", filename) log.warning("IOError: Unable to open file: '%s'", filename)
try: try:
log.debug("Saving config file %s..", filename)
pkl_file = open(filename, "wb") pkl_file = open(filename, "wb")
cPickle.dump(self.config, pkl_file) cPickle.dump(self.config, pkl_file)
pkl_file.close() pkl_file.close()
except IOError: except IOError:
log.warning("IOError: Unable to save file '%s'", filename) log.warning("IOError: Unable to save file '%s'", filename)
self.save_timer = None
def set(self, key, value): def set(self, key, value):
"""Set the 'key' with 'value'.""" """Set the 'key' with 'value'."""
@ -140,6 +148,11 @@ class Config:
except: except:
pass pass
# We set the save_timer for 5 seconds if not already set
log.debug("save_timer: %s", self.save_timer)
if not self.save_timer:
self.save_timer = gobject.timeout_add(5000, self.save)
def get(self, key): def get(self, key):
"""Get the value of 'key'. If it is an invalid key then get() will """Get the value of 'key'. If it is an invalid key then get() will
return None.""" return None."""