Fix storage wouldn't actually load back saved items

This commit is contained in:
Lonami Exo 2018-04-23 13:39:57 +02:00
parent 50d83c6538
commit 9c3b67f503
1 changed files with 9 additions and 2 deletions

View File

@ -5,6 +5,9 @@ import os
import json import json
FILE_NAME = 'data.json'
class Storage: class Storage:
class _Guard: class _Guard:
def __init__(self, storage): def __init__(self, storage):
@ -21,6 +24,10 @@ class Storage:
self._root = root self._root = root
self._autosave = True self._autosave = True
self._guard = self._Guard(self) self._guard = self._Guard(self)
if os.path.isfile(os.path.join(self._root, FILE_NAME)):
with open(os.path.join(self._root, FILE_NAME)) as fp:
self._data = json.load(fp)
else:
self._data = {} self._data = {}
def bulk_save(self): def bulk_save(self):
@ -42,5 +49,5 @@ class Storage:
def _save(self): def _save(self):
if not os.path.isdir(self._root): if not os.path.isdir(self._root):
os.makedirs(self._root, exist_ok=True) os.makedirs(self._root, exist_ok=True)
with open(os.path.join(self._root, 'data.json'), 'w') as fp: with open(os.path.join(self._root, FILE_NAME), 'w') as fp:
json.dump(self._data, fp) json.dump(self._data, fp)