From f352ad0e32bc7de6fbbf917f9757f7f10f302977 Mon Sep 17 00:00:00 2001 From: Nick80835 Date: Tue, 9 Jun 2020 20:07:21 -0400 Subject: [PATCH] rewrite settings a tiny bit, add list support --- ubot/settings.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) 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("|")