Trigger config_change handlers when the config has changed using a USR1 signal

This commit is contained in:
Florent Le Coz 2013-06-18 20:37:04 +02:00
parent ef9672c0fd
commit 39fa811374
2 changed files with 26 additions and 4 deletions

View file

@ -30,10 +30,13 @@ class Config(RawConfigParser):
load/save the config to a file
"""
def __init__(self, file_name):
self.file_name = file_name
RawConfigParser.__init__(self, None)
# make the options case sensitive
self.optionxform = str
self.read_file(file_name)
def read_file(self, file_name):
self.file_name = file_name
try:
RawConfigParser.read(self, file_name, encoding='utf-8')
except TypeError: # python < 3.2 sucks
@ -175,8 +178,6 @@ class Config(RawConfigParser):
result_lines.append('%s = %s' % (option, value))
elif not written:
result_lines.append('%s = %s' % (option, value))
try:
df = open(self.file_name, 'w', encoding='utf-8')
for line in result_lines:
@ -233,6 +234,16 @@ class Config(RawConfigParser):
except NoSectionError:
pass
def to_dict(self):
"""
Returns a dict of the form {section: {option: value, option: value}, }
"""
res = {}
for section in self.sections():
res[section] = {}
for option in self.options(section):
res[section][option] = self.get(option, "", section)
return res
firstrun = False

View file

@ -370,7 +370,18 @@ class Core(object):
log.debug("Theme reloaded.")
# reload the config from the disk
log.debug("Reloading the config…")
config.__init__(config.file_name)
# Copy the old config in a dict
old_config = config.to_dict()
config.read_file(config.file_name)
# Compare old and current config, to trigger the callbacks of all
# modified options
for section in config.sections():
old_section = old_config.get(section, {})
for option in config.options(section):
old_value = old_section.get(option)
new_value = config.get(option, "", section)
if new_value != old_value:
self.trigger_configuration_change(option, new_value)
log.debug("Config reloaded.")
# in case some roster options have changed
roster.modified()