Skip to content

Commit

Permalink
don't lose all the data if you write without a get
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Jan 6, 2016
1 parent a52f395 commit ef2fb81
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions nikola/state.py
Expand Up @@ -51,22 +51,26 @@ def __init__(self, path):

def get(self, key):
"""Get data stored in key."""
if os.path.isfile(path):
with open(path) as inf:
self.data = json.load(inf)
return self.data.get(key)

def set(self, key, value):
"""Store value in key."""
self._read()
self.data[key] = value
self._save()

def delete(self, key):
"""Delete key and the value it contains."""
self._read()
if key in self.data:
self.data.pop(key)
self._save()

def _read(self)
if os.path.isfile(self._path):
with open(self._path) as inf:
self.data = json.load(inf)

def _save(self):
tpath = self._path + '.tmp'
with open(tpath, 'w') as outf:
Expand Down

0 comments on commit ef2fb81

Please sign in to comment.