From 39fa811374b7119173120175e70fb4f197bc0aba Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 18 Jun 2013 20:37:04 +0200 Subject: [PATCH] Trigger config_change handlers when the config has changed using a USR1 signal --- src/config.py | 17 ++++++++++++++--- src/core.py | 13 ++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/config.py b/src/config.py index 92fa503f..6b7e3864 100644 --- a/src/config.py +++ b/src/config.py @@ -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 diff --git a/src/core.py b/src/core.py index 9e12ba03..9476bd42 100644 --- a/src/core.py +++ b/src/core.py @@ -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()