Only store settings object after new write

pull/692/head
Åke Forslund 2017-04-24 12:08:47 +02:00
parent 40010e90f0
commit e79dee92e9
1 changed files with 12 additions and 5 deletions

View File

@ -19,22 +19,29 @@ import json
import sys import sys
from os.path import isfile from os.path import isfile
class SkillSettings(dict): class SkillSettings(dict):
def __init__(self, settings_file): def __init__(self, settings_file):
super(SkillSettings, self).__init__() super(SkillSettings, self).__init__()
self.path = settings_file self._path = settings_file
if isfile(self.path):
with open(self.path) as f: # if file exist, open and read stored values into self
if isfile(self._path):
with open(self._path) as f:
json_data = json.load(f) json_data = json.load(f)
for key in json_data: for key in json_data:
self.__setitem__(key, json_data[key]) self.__setitem__(key, json_data[key])
self._is_stored = True
def __getitem__(self, key): def __getitem__(self, key):
return super(SkillSettings, self).__getitem__(key) return super(SkillSettings, self).__getitem__(key)
def __setitem__(self, key, value): def __setitem__(self, key, value):
self._is_stored = False
return super(SkillSettings, self).__setitem__(key, value) return super(SkillSettings, self).__setitem__(key, value)
def store(self): def store(self):
with open(self.path, 'w')as f: if not self._is_stored:
with open(self._path, 'w')as f:
json.dump(self, f) json.dump(self, f)