uniborg/uniborg/storage.py

54 lines
1.5 KiB
Python

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import json
FILE_NAME = 'data.json'
class Storage:
class _Guard:
def __init__(self, storage):
self._storage = storage
def __enter__(self):
self._storage._autosave = False
def __exit__(self, *args):
self._storage._autosave = True
self._storage._save()
def __init__(self, root):
self._root = root
self._autosave = True
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 = {}
def bulk_save(self):
return self._guard
def __getattr__(self, name):
if name.startswith('_'):
raise ValueError('You can only access existing private members')
return self._data.get(name, None)
def __setattr__(self, name, value):
if name.startswith('_'):
self.__dict__[name] = value
else:
self._data[name] = value
if self._autosave:
self._save()
def _save(self):
if not os.path.isdir(self._root):
os.makedirs(self._root, exist_ok=True)
with open(os.path.join(self._root, FILE_NAME), 'w') as fp:
json.dump(self._data, fp)