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 load/save the config to a file
""" """
def __init__(self, file_name): def __init__(self, file_name):
self.file_name = file_name
RawConfigParser.__init__(self, None) RawConfigParser.__init__(self, None)
# make the options case sensitive # make the options case sensitive
self.optionxform = str self.optionxform = str
self.read_file(file_name)
def read_file(self, file_name):
self.file_name = file_name
try: try:
RawConfigParser.read(self, file_name, encoding='utf-8') RawConfigParser.read(self, file_name, encoding='utf-8')
except TypeError: # python < 3.2 sucks except TypeError: # python < 3.2 sucks
@ -175,8 +178,6 @@ class Config(RawConfigParser):
result_lines.append('%s = %s' % (option, value)) result_lines.append('%s = %s' % (option, value))
elif not written: elif not written:
result_lines.append('%s = %s' % (option, value)) result_lines.append('%s = %s' % (option, value))
try: try:
df = open(self.file_name, 'w', encoding='utf-8') df = open(self.file_name, 'w', encoding='utf-8')
for line in result_lines: for line in result_lines:
@ -233,6 +234,16 @@ class Config(RawConfigParser):
except NoSectionError: except NoSectionError:
pass 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 firstrun = False

View file

@ -370,7 +370,18 @@ class Core(object):
log.debug("Theme reloaded.") log.debug("Theme reloaded.")
# reload the config from the disk # reload the config from the disk
log.debug("Reloading the config…") 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.") log.debug("Config reloaded.")
# in case some roster options have changed # in case some roster options have changed
roster.modified() roster.modified()