wobbl_tools/settings.py

49 lines
1.1 KiB
Python

#!/usr/bin/python3
class Settings:
def __init__(self, path: str=None):
self.path = path
if path is None:
self.settings = {}
else:
self.load_from_file(path)
def load_from_file(self, path):
file = open(path, "r")
self.settings = eval(file.read())
file.close()
def save(self, path):
file = open(path, "w")
file.write(str(self))
file.close()
def __str__(self):
new_settings_str = "{\n"
for key in self.settings:
new_settings_str += "\t" + repr(key) + ": " + repr(self.settings[key]) + ",\n"
new_settings_str = new_settings_str[:-2] + "\n}"
return new_settings_str
def __repr__(self):
return repr(self.settings)
def __getitem__(self, key):
return self.settings[key]
def __setitem__(self, key, value):
self.settings[key] = value
def __delitem__(self, key):
self.settings.pop(key)
def __iter__(self):
return self.settings
def __len__(self):
return len(self.settings)