wobbl_tools/settings.py

53 lines
1.1 KiB
Python
Raw Normal View History

2023-10-31 19:31:22 +01:00
#!/usr/bin/python3
class Settings:
def __init__(self, path: str=None):
self.path = path
if path is None:
2023-11-05 16:14:16 +01:00
self.settings = {}
2023-10-31 19:31:22 +01:00
else:
self.load_from_file(path)
2023-11-05 16:14:16 +01:00
2023-10-31 19:31:22 +01:00
def load_from_file(self, path):
file = open(path, "r")
self.settings = eval(file.read())
file.close()
def save(self, path: str=None):
if path is None:
path = self.path
2023-11-05 16:14:16 +01:00
file = open(path, "w")
file.write(str(self))
file.close()
2023-10-31 19:31:22 +01:00
2023-11-05 16:14:16 +01:00
def __str__(self):
2023-10-31 19:31:22 +01:00
new_settings_str = "{\n"
2023-11-05 16:14:16 +01:00
for key in self.settings:
2023-10-31 19:31:22 +01:00
new_settings_str += "\t" + repr(key) + ": " + repr(self.settings[key]) + ",\n"
new_settings_str = new_settings_str[:-2] + "\n}"
2023-11-05 16:14:16 +01:00
return new_settings_str
2023-10-31 19:31:22 +01:00
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)