settings inherit from dict

This commit is contained in:
Андрей Владимирович 2016-02-18 00:16:44 +03:00
parent ddc89d1664
commit 05e5948d3f

View File

@ -3,20 +3,21 @@ import json
import os import os
class Settings(object): class Settings(dict):
def __init__(self): def __init__(self):
self.path = Settings.get_default_path() + 'toxygen.json' self.path = Settings.get_default_path() + 'toxygen.json'
if os.path.exists(self.path): if os.path.exists(self.path):
with open(self.path) as fl: with open(self.path) as fl:
data = fl.read() data = fl.read()
self.data = json.loads(data) super(self.__class__, self).__init__(json.loads(data))
else: else:
self.create_default_settings() super(self.__class__, self).__init__(Settings.get_default_settings())
self.save() self.save()
def create_default_settings(self): @staticmethod
self.data = { def get_default_settings():
return {
"theme": "default", "theme": "default",
"ipv6_enabled": True, "ipv6_enabled": True,
"udp_enabled": True, "udp_enabled": True,
@ -37,14 +38,8 @@ class Settings(object):
"typing_notifications": True "typing_notifications": True
} }
def __get__(self, attr):
return self.data[attr]
def __set__(self, attr, value):
self.data[attr] = value
def save(self): def save(self):
text = json.dumps(self.data) text = json.dumps(self)
with open(self.path, 'w') as fl: with open(self.path, 'w') as fl:
fl.write(text) fl.write(text)