diff --git a/ubot/settings.py b/ubot/settings.py index a23ea81..0398455 100644 --- a/ubot/settings.py +++ b/ubot/settings.py @@ -8,6 +8,11 @@ class Settings(): self.config = SafeConfigParser() self.config.read("settings.ini") + def write_changes(self): + with open('settings.ini', 'w') as config_file: + self.config.write(config_file) + config_file.close() + def get_config(self, key, default=None): return self.config.get("DEFAULT", key, fallback=default) @@ -15,8 +20,40 @@ class Settings(): return bool(self.config.get("DEFAULT", key, fallback=default) == "True") def set_config(self, key, value): + value = str(value) self.config.set("DEFAULT", key, value) + self.write_changes() - with open('settings.ini', 'w') as config_file: - self.config.write(config_file) - config_file.close() + def add_to_list(self, key, value): + config_value = self.config.get("DEFAULT", key, fallback=None) + value = str(value) + + if config_value: + config_list = config_value.split("|") + else: + config_list = [] + + config_list.append(value) + + self.config.set("DEFAULT", key, "|".join(config_list)) + self.write_changes() + + def remove_from_list(self, key, value): + config_value = self.config.get("DEFAULT", key, fallback=None) + value = str(value) + + if config_value: + config_list = config_value.split("|") + else: + return + + if value in config_list: + config_list.remove(value) + else: + return + + self.config.set("DEFAULT", key, "|".join(config_list)) + self.write_changes() + + def get_list(self, key): + return self.config.get("DEFAULT", key, fallback="").split("|")