Poodletooth-iLand/otp/settings/Settings.py
2015-03-03 17:10:12 -05:00

39 lines
845 B
Python

import collections
import json
import os
class Settings(collections.MutableMapping):
def __init__(self, filename):
self.filename = filename
self.store = {}
self.read()
def read(self):
if os.path.exists(self.filename):
with open(self.filename, 'r') as f:
self.store = json.load(f)
else:
self.write()
def write(self):
with open(self.filename, 'w') as f:
json.dump(self.store, f)
def __setitem__(self, key, value):
self.store[key] = value
self.write()
def __delitem__(self, key):
del self.store[key]
self.write()
def __getitem__(self, key):
return self.store[key]
def __iter__(self):
return iter(self.store)
def __len__(self):
return len(self.store)