2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-30 22:16:05 +00:00

rewrite settings a tiny bit, add list support

This commit is contained in:
Nick80835
2020-06-09 20:07:21 -04:00
parent bdaea0b97c
commit f352ad0e32

View File

@@ -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("|")